Mercurial > hg > soundsoftware-site
comparison vendor/plugins/embedded/app/controllers/embedded_controller.rb @ 247:73ff0e6a11b1 cannam
* Merge from branch cannam-pre-20110113-merge
author | Chris Cannam |
---|---|
date | Thu, 03 Mar 2011 12:11:53 +0000 |
parents | 317821dd92c9 |
children |
comparison
equal
deleted
inserted
replaced
246:eeebe205a056 | 247:73ff0e6a11b1 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2008 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program; if not, write to the Free Software | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require 'iconv' | |
19 | |
20 class EmbeddedController < ApplicationController | |
21 class EmbeddedControllerError < StandardError; end | |
22 | |
23 unloadable | |
24 layout 'base' | |
25 before_filter :find_project, :authorize | |
26 | |
27 def index | |
28 path = get_real_path(params[:path]) | |
29 if File.directory?(path) | |
30 file = get_index_file(path) | |
31 target = params[:path] || [] | |
32 target << file | |
33 # Forces redirect to the index file when the requested path is a directory | |
34 # so that relative links in embedded html pages work | |
35 redirect_to :path => target | |
36 return | |
37 end | |
38 | |
39 # Check file extension | |
40 raise EmbeddedControllerError.new('This file can not be viewed (invalid extension).') unless Redmine::Plugins::Embedded.valid_extension?(path) | |
41 | |
42 if Redmine::MimeType.is_type?('image', path) | |
43 send_file path, :disposition => 'inline', :type => Redmine::MimeType.of(path) | |
44 else | |
45 embed_file path | |
46 end | |
47 | |
48 rescue Errno::ENOENT => e | |
49 # File was not found | |
50 render_404 | |
51 rescue Errno::EACCES => e | |
52 # Can not read the file | |
53 render_error "Unable to read the file: #{e.message}" | |
54 rescue EmbeddedControllerError => e | |
55 render_error e.message | |
56 end | |
57 | |
58 private | |
59 | |
60 def find_project | |
61 @project = Project.find(params[:id]) | |
62 rescue ActiveRecord::RecordNotFound | |
63 render_404 | |
64 end | |
65 | |
66 # Return the path to the html root directory for the current project | |
67 def get_project_directory | |
68 @project_directory ||= Setting.plugin_embedded['path'].to_s.gsub('{PROJECT}', @project.identifier) | |
69 end | |
70 | |
71 # Returns the absolute path of the requested file | |
72 # Parameter is an Array | |
73 def get_real_path(path) | |
74 real = get_project_directory | |
75 real = File.join(real, path) unless path.nil? || path.empty? | |
76 dir = File.expand_path(get_project_directory) | |
77 real = File.expand_path(real) | |
78 raise Errno::ENOENT unless real.starts_with?(dir) && File.exist?(real) | |
79 real | |
80 end | |
81 | |
82 # Returns the index file in the given directory | |
83 # and raises an exception if none is found | |
84 def get_index_file(dir) | |
85 indexes = Setting.plugin_embedded['index'].to_s.split | |
86 file = indexes.find {|f| File.exist?(File.join(dir, f))} | |
87 raise EmbeddedControllerError.new("No index file found in #{dir} (#{indexes.join(', ')}).") if file.nil? | |
88 file | |
89 end | |
90 | |
91 # Renders a given HTML file | |
92 def embed_file(path) | |
93 @content = File.read(path) | |
94 | |
95 # Extract html title from embedded page | |
96 if @content =~ %r{<title>([^<]*)</title>}mi | |
97 @title = $1.strip | |
98 end | |
99 | |
100 # Keep html body only | |
101 @content.gsub!(%r{^.*<body[^>]*>(.*)</body>.*$}mi, '\\1') | |
102 | |
103 # Re-encode content if needed | |
104 source_encoding = Setting.plugin_embedded['encoding'].to_s | |
105 unless source_encoding.blank? | |
106 begin; @content = Iconv.new('UTF-8', source_encoding).iconv(@content); rescue; end | |
107 end | |
108 | |
109 @doc_template = Redmine::Plugins::Embedded.detect_template_from_path(path) | |
110 render :action => 'index' | |
111 end | |
112 end |