diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 0f3ea3754..cf4e6a8e9 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -74,6 +74,45 @@ class AttachmentsController < ApplicationController :disposition => 'attachment' #inline can open in browser end + def direct_download_history + @attachment_history = AttachmentHistory.find(params[:id]) + @attachment_history.increment_download + send_file @attachment_history.diskfile_history, :filename => filename_for_content_disposition(@attachment_history.filename), + :type => detect_content_type(@attachment_history), + :disposition => 'attachment' #inline can open in browser + end + + def download_history + @attachment_history = AttachmentHistory.find(params[:id]) + candown = attachment_history_candown @attachment_history + if candown || User.current.admin? || User.current.id == @attachment_history.author_id + if stale?(:etag => @attachment_history.digest) + if params[:preview] == 'true' + convered_file = @attachment_history.diskfile_history + #如果本身不是pdf文件,则先寻找是不是已转换化,如果没有则转化 + unless pdf?(convered_file) + convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf") + unless File.exist?(convered_file) + office = Trustie::Utils::Office.new(@attachment_history.diskfile) + office.conver(convered_file) + end + end + if File.exist?(convered_file) && pdf?(convered_file) + send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline' + else + direct_download_history + end + else + direct_download_history + end + end + else + render_403 :message => :notice_not_authorized + end + rescue => e + redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html" + end + def download # modify by nwb # 下载添加权限设置 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 08d7bc2c1..9d08be378 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1997,6 +1997,18 @@ module ApplicationHelper courses_doing end + def attachment_history_candown attachment_history + if attachment_history.container_type == "Course" + course = Course.find(attachment_history.container_id) + candown = User.current.member_of?(course) || (course.is_public && attachment_history.is_public == 1) + elsif attachment_history.container_type == "Project" + project = Project.find(attachment_history.container_id) + candown = User.current.member_of?(project) || (project.is_public && attachment_history.is_public == 1) + elsif attachment_history.container_type == "OrgSubfield" + org = OrgSubfield.find(attachment_history.container_id) + candown = User.current.member_of_org?(org) || (org.organization.is_public && attachment_history.is_public == 1) + end + end def attachment_candown attachment candown = false diff --git a/app/models/attachment_history.rb b/app/models/attachment_history.rb index 2160d242d..fb4e762ba 100644 --- a/app/models/attachment_history.rb +++ b/app/models/attachment_history.rb @@ -1,3 +1,14 @@ class AttachmentHistory < ActiveRecord::Base belongs_to :attachment,foreign_key: 'attachment_id' + cattr_accessor :storage_history_path + @@storage_history_path = Redmine::Configuration['attachments_storage_path'] || File.join(Rails.root, "files") + + # Returns file's location on disk + def diskfile_history + File.join(self.class.storage_history_path, disk_directory.to_s, disk_filename.to_s) + end + + def increment_download + increment!(:downloads) + end end diff --git a/app/views/attachments/_show_attachment_history.html.erb b/app/views/attachments/_show_attachment_history.html.erb index a04b5a89d..9dc700123 100644 --- a/app/views/attachments/_show_attachment_history.html.erb +++ b/app/views/attachments/_show_attachment_history.html.erb @@ -15,7 +15,7 @@ <% @attachment_histories.each do |history| %> <%= link_to truncate(history.filename,length: 35, omission: '...'), - download_named_attachment_path(history.attachment.id, history.filename), + download_history_attachment_path(history.id, history.filename), :title => history.filename+"\n"+history.description.to_s, :style => "overflow: hidden; white-space: nowrap;text-overflow: ellipsis;",:class => "linkBlue f_14 f_b" %> 版本号:<%= history.version %> diff --git a/config/routes.rb b/config/routes.rb index 3157c50a1..7060e4eb6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -869,6 +869,7 @@ RedmineApp::Application.routes.draw do get 'attachments/attachment_versions/:id',:to=>'attachments#attachment_versions',:as=>'attachments_versions' post 'attachments/upload_attachment_version',:to=>'attachments#upload_attachment_version',:as=>'upload_attachment_version' get 'attachments/download/:id/:filename', :to => 'attachments#download', :id => /\d+/, :filename => /.*/, :as => 'download_named_attachment' + get 'attachments/download_history/:id/:filename', :to => 'attachments#download_history', :id => /\d+/, :filename => /.*/, :as => 'download_history_attachment' get 'attachments/download/:id', :to => 'attachments#download', :id => /\d+/ get 'attachments/thumbnail/:id(/:size)', :to => 'attachments#thumbnail', :id => /\d+/, :size => /\d+/, :as => 'thumbnail' get 'attachments/autocomplete'