luis@1119
|
1 # Redmine - project management software
|
luis@1119
|
2 # Copyright (C) 2008 Jean-Philippe Lang
|
luis@1119
|
3 #
|
luis@1119
|
4 # This program is free software; you can redistribute it and/or
|
luis@1119
|
5 # modify it under the terms of the GNU General Public License
|
luis@1119
|
6 # as published by the Free Software Foundation; either version 2
|
luis@1119
|
7 # of the License, or (at your option) any later version.
|
luis@1119
|
8 #
|
luis@1119
|
9 # This program is distributed in the hope that it will be useful,
|
luis@1119
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
luis@1119
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
luis@1119
|
12 # GNU General Public License for more details.
|
luis@1119
|
13 #
|
luis@1119
|
14 # You should have received a copy of the GNU General Public License
|
luis@1119
|
15 # along with this program; if not, write to the Free Software
|
luis@1119
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
luis@1119
|
17
|
luis@1119
|
18 require 'iconv'
|
luis@1119
|
19
|
luis@1119
|
20 class DataFile < ActiveRecord::Base
|
luis@1119
|
21 def self.save(directory, zipname, upload)
|
luis@1119
|
22 path = File.join(directory, zipname)
|
luis@1119
|
23 File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
|
luis@1119
|
24 end
|
luis@1119
|
25 end
|
luis@1119
|
26
|
luis@1119
|
27 class RedmineEmbeddedController < ApplicationController
|
luis@1119
|
28 class RedmineEmbeddedControllerError < StandardError; end
|
luis@1119
|
29
|
luis@1119
|
30 unloadable
|
luis@1119
|
31 layout 'base'
|
luis@1119
|
32 before_filter :find_project, :authorize
|
luis@1119
|
33
|
luis@1119
|
34 def index
|
luis@1119
|
35 file = params[:request_path]
|
luis@1119
|
36 path = get_real_path(file)
|
luis@1119
|
37 if File.directory?(path)
|
luis@1119
|
38 file = get_index_file(path)
|
luis@1119
|
39 target = file || []
|
luis@1119
|
40 #target << file
|
luis@1119
|
41 # Forces redirect to the index file when the requested path is a directory
|
luis@1119
|
42 # so that relative links in embedded html pages work
|
luis@1119
|
43 redirect_to :request_path => target
|
luis@1119
|
44 return
|
luis@1119
|
45 end
|
luis@1119
|
46
|
luis@1119
|
47 # Check file extension
|
luis@1119
|
48 raise RedmineEmbeddedControllerError.new('This file can not be viewed (invalid extension).') unless Redmine::Plugins::RedmineEmbedded.valid_extension?(path)
|
luis@1119
|
49
|
luis@1119
|
50 if Redmine::MimeType.is_type?('image', path)
|
luis@1119
|
51 send_file path, :disposition => 'inline', :type => Redmine::MimeType.of(path)
|
luis@1119
|
52 else
|
luis@1119
|
53 embed_file path
|
luis@1119
|
54 end
|
luis@1119
|
55
|
luis@1119
|
56 rescue Errno::ENOENT => e
|
luis@1119
|
57 @content = "No documentation found"
|
luis@1119
|
58 @title = ""
|
luis@1119
|
59 render :index
|
luis@1119
|
60 rescue Errno::EACCES => e
|
luis@1119
|
61 # Can not read the file
|
luis@1119
|
62 render_error "Unable to read the file: #{e.message}"
|
luis@1119
|
63 rescue RedmineEmbeddedControllerError => e
|
luis@1119
|
64 render_error e.message
|
luis@1119
|
65 end
|
luis@1119
|
66
|
luis@1119
|
67 def upload
|
luis@1119
|
68 if params[:upload]
|
luis@1119
|
69 file = params[:upload]
|
luis@1119
|
70 zipname = sanitize_filename(params[:upload]['datafile'].original_filename)
|
luis@1119
|
71 if ["zip"].include?(File.extname(zipname).downcase[1..-1])
|
luis@1119
|
72 dir = get_project_directory.gsub("/html", "")
|
luis@1119
|
73 if File.directory? dir
|
luis@1119
|
74 `rm -rf #{dir}/*` #clean up any exisiting docs
|
luis@1119
|
75 else
|
luis@1119
|
76 Dir.mkdir dir
|
luis@1119
|
77 end
|
luis@1119
|
78 filename = DataFile.save(dir, zipname, params[:upload])
|
luis@1119
|
79 Dir.chdir(dir)
|
luis@1119
|
80 `unzip #{zipname}`
|
luis@1119
|
81 redirect_to show_embedded_url(@project), :notice => "Documentation uploaded"
|
luis@1119
|
82 else
|
luis@1119
|
83 render :index, :error => "File must be ZIP format"
|
luis@1119
|
84 end
|
luis@1119
|
85 else
|
luis@1119
|
86 render :index, :error => "No file uploaded"
|
luis@1119
|
87 end
|
luis@1119
|
88 end
|
luis@1119
|
89
|
luis@1119
|
90 private
|
luis@1119
|
91
|
luis@1119
|
92 def sanitize_filename(file_name)
|
luis@1119
|
93 # get only the filename, not the whole path (from IE)
|
luis@1119
|
94 just_filename = File.basename(file_name)
|
luis@1119
|
95 # replace all none alphanumeric, underscore or perioids
|
luis@1119
|
96 # with underscore
|
luis@1119
|
97 just_filename.sub(/[^\w\.\-]/,'_')
|
luis@1119
|
98 end
|
luis@1119
|
99
|
luis@1119
|
100 def find_project
|
luis@1119
|
101 @project = Project.find(params[:id])
|
luis@1119
|
102 rescue ActiveRecord::RecordNotFound
|
luis@1119
|
103 render_404
|
luis@1119
|
104 end
|
luis@1119
|
105
|
luis@1119
|
106 # Return the path to the html root directory for the current project
|
luis@1119
|
107 def get_project_directory
|
luis@1119
|
108 @project_directory ||= Setting.plugin_redmine_embedded['path'].to_s.gsub('{PROJECT}', @project.identifier)
|
luis@1119
|
109 end
|
luis@1119
|
110
|
luis@1119
|
111 # Returns the absolute path of the requested file
|
luis@1119
|
112 # Parameter is an Array
|
luis@1119
|
113 def get_real_path(path)
|
luis@1119
|
114 real = get_project_directory
|
luis@1119
|
115 real = File.join(real, path) unless path.nil? || path.empty?
|
luis@1119
|
116 dir = File.expand_path(get_project_directory)
|
luis@1119
|
117 real = File.expand_path(real)
|
luis@1119
|
118 raise Errno::ENOENT unless real.starts_with?(dir) && File.exist?(real)
|
luis@1119
|
119 real
|
luis@1119
|
120 end
|
luis@1119
|
121
|
luis@1119
|
122 # Returns the index file in the given directory
|
luis@1119
|
123 # and raises an exception if none is found
|
luis@1119
|
124 def get_index_file(dir)
|
luis@1119
|
125 indexes = Setting.plugin_redmine_embedded['index'].to_s.split
|
luis@1119
|
126 file = indexes.find {|f| File.exist?(File.join(dir, f))}
|
luis@1119
|
127 raise RedmineEmbeddedControllerError.new("No index file found in #{dir} (#{indexes.join(', ')}).") if file.nil?
|
luis@1119
|
128 file
|
luis@1119
|
129 end
|
luis@1119
|
130
|
luis@1119
|
131 # Renders a given HTML file
|
luis@1119
|
132 def embed_file(path)
|
luis@1119
|
133 @content = File.read(path)
|
luis@1119
|
134
|
luis@1119
|
135 # Extract html title from embedded page
|
luis@1119
|
136 if @content =~ %r{<title>([^<]*)</title>}mi
|
luis@1119
|
137 @title = $1.strip
|
luis@1119
|
138 end
|
luis@1119
|
139
|
luis@1119
|
140 # Keep html body only
|
luis@1119
|
141 @content.gsub!(%r{^.*<body[^>]*>(.*)</body>.*$}mi, '\\1')
|
luis@1119
|
142
|
luis@1119
|
143 # Re-encode content if needed
|
luis@1119
|
144 source_encoding = Setting.plugin_redmine_embedded['encoding'].to_s
|
luis@1119
|
145 unless source_encoding.blank?
|
luis@1119
|
146 begin; @content = Iconv.new('UTF-8', source_encoding).iconv(@content); rescue; end
|
luis@1119
|
147 end
|
luis@1119
|
148
|
luis@1119
|
149 @doc_template = Redmine::Plugins::RedmineEmbedded.detect_template_from_path(path)
|
luis@1119
|
150 render :action => 'index'
|
luis@1119
|
151 end
|
luis@1119
|
152 end
|