Mercurial > hg > soundsoftware-site
comparison plugins/redmine_embedded/app/controllers/redmine_embedded_controller.rb @ 1119:22d81bd0b62c redmine-2.2-integration
Deleted existing the embedded plugin and replaced it witgh redmine_embedded (same functionality, but upgraded to be compatible with Redmine 2.x).
author | luisf <luis.figueira@eecs.qmul.ac.uk> |
---|---|
date | Tue, 08 Jan 2013 14:43:04 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1118:35686c1667de | 1119:22d81bd0b62c |
---|---|
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 DataFile < ActiveRecord::Base | |
21 def self.save(directory, zipname, upload) | |
22 path = File.join(directory, zipname) | |
23 File.open(path, "wb") { |f| f.write(upload['datafile'].read) } | |
24 end | |
25 end | |
26 | |
27 class RedmineEmbeddedController < ApplicationController | |
28 class RedmineEmbeddedControllerError < StandardError; end | |
29 | |
30 unloadable | |
31 layout 'base' | |
32 before_filter :find_project, :authorize | |
33 | |
34 def index | |
35 file = params[:request_path] | |
36 path = get_real_path(file) | |
37 if File.directory?(path) | |
38 file = get_index_file(path) | |
39 target = file || [] | |
40 #target << file | |
41 # Forces redirect to the index file when the requested path is a directory | |
42 # so that relative links in embedded html pages work | |
43 redirect_to :request_path => target | |
44 return | |
45 end | |
46 | |
47 # Check file extension | |
48 raise RedmineEmbeddedControllerError.new('This file can not be viewed (invalid extension).') unless Redmine::Plugins::RedmineEmbedded.valid_extension?(path) | |
49 | |
50 if Redmine::MimeType.is_type?('image', path) | |
51 send_file path, :disposition => 'inline', :type => Redmine::MimeType.of(path) | |
52 else | |
53 embed_file path | |
54 end | |
55 | |
56 rescue Errno::ENOENT => e | |
57 @content = "No documentation found" | |
58 @title = "" | |
59 render :index | |
60 rescue Errno::EACCES => e | |
61 # Can not read the file | |
62 render_error "Unable to read the file: #{e.message}" | |
63 rescue RedmineEmbeddedControllerError => e | |
64 render_error e.message | |
65 end | |
66 | |
67 def upload | |
68 if params[:upload] | |
69 file = params[:upload] | |
70 zipname = sanitize_filename(params[:upload]['datafile'].original_filename) | |
71 if ["zip"].include?(File.extname(zipname).downcase[1..-1]) | |
72 dir = get_project_directory.gsub("/html", "") | |
73 if File.directory? dir | |
74 `rm -rf #{dir}/*` #clean up any exisiting docs | |
75 else | |
76 Dir.mkdir dir | |
77 end | |
78 filename = DataFile.save(dir, zipname, params[:upload]) | |
79 Dir.chdir(dir) | |
80 `unzip #{zipname}` | |
81 redirect_to show_embedded_url(@project), :notice => "Documentation uploaded" | |
82 else | |
83 render :index, :error => "File must be ZIP format" | |
84 end | |
85 else | |
86 render :index, :error => "No file uploaded" | |
87 end | |
88 end | |
89 | |
90 private | |
91 | |
92 def sanitize_filename(file_name) | |
93 # get only the filename, not the whole path (from IE) | |
94 just_filename = File.basename(file_name) | |
95 # replace all none alphanumeric, underscore or perioids | |
96 # with underscore | |
97 just_filename.sub(/[^\w\.\-]/,'_') | |
98 end | |
99 | |
100 def find_project | |
101 @project = Project.find(params[:id]) | |
102 rescue ActiveRecord::RecordNotFound | |
103 render_404 | |
104 end | |
105 | |
106 # Return the path to the html root directory for the current project | |
107 def get_project_directory | |
108 @project_directory ||= Setting.plugin_redmine_embedded['path'].to_s.gsub('{PROJECT}', @project.identifier) | |
109 end | |
110 | |
111 # Returns the absolute path of the requested file | |
112 # Parameter is an Array | |
113 def get_real_path(path) | |
114 real = get_project_directory | |
115 real = File.join(real, path) unless path.nil? || path.empty? | |
116 dir = File.expand_path(get_project_directory) | |
117 real = File.expand_path(real) | |
118 raise Errno::ENOENT unless real.starts_with?(dir) && File.exist?(real) | |
119 real | |
120 end | |
121 | |
122 # Returns the index file in the given directory | |
123 # and raises an exception if none is found | |
124 def get_index_file(dir) | |
125 indexes = Setting.plugin_redmine_embedded['index'].to_s.split | |
126 file = indexes.find {|f| File.exist?(File.join(dir, f))} | |
127 raise RedmineEmbeddedControllerError.new("No index file found in #{dir} (#{indexes.join(', ')}).") if file.nil? | |
128 file | |
129 end | |
130 | |
131 # Renders a given HTML file | |
132 def embed_file(path) | |
133 @content = File.read(path) | |
134 | |
135 # Extract html title from embedded page | |
136 if @content =~ %r{<title>([^<]*)</title>}mi | |
137 @title = $1.strip | |
138 end | |
139 | |
140 # Keep html body only | |
141 @content.gsub!(%r{^.*<body[^>]*>(.*)</body>.*$}mi, '\\1') | |
142 | |
143 # Re-encode content if needed | |
144 source_encoding = Setting.plugin_redmine_embedded['encoding'].to_s | |
145 unless source_encoding.blank? | |
146 begin; @content = Iconv.new('UTF-8', source_encoding).iconv(@content); rescue; end | |
147 end | |
148 | |
149 @doc_template = Redmine::Plugins::RedmineEmbedded.detect_template_from_path(path) | |
150 render :action => 'index' | |
151 end | |
152 end |