annotate vendor/plugins/embedded/app/controllers/embedded_controller.rb @ 1477:f2ad2199b49a bibplugin_integration

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