历史版本提供附件下载功能

This commit is contained in:
huang 2016-02-20 16:19:35 +08:00
parent 534d93de77
commit a7470e059d
5 changed files with 64 additions and 1 deletions

View File

@ -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
# 下载添加权限设置

View File

@ -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

View File

@ -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

View File

@ -15,7 +15,7 @@
<% @attachment_histories.each do |history| %>
<span class="attachment">
<%= 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" %>
<span>版本号:<%= history.version %></span>
</span>

View File

@ -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'