Merge branch 'szzh' into develop

gitlab v20150605
sw 10 years ago
commit e4b4f640a3

@ -62,6 +62,16 @@ class AttachmentsController < ApplicationController
render :action => 'file' render :action => 'file'
end end
def pdf?(file)
file.downcase.end_with?(".pdf")
end
def direct_download
send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
:type => detect_content_type(@attachment),
:disposition => 'attachment' #inline can open in browser
end
def download def download
# modify by nwb # modify by nwb
# 下载添加权限设置 # 下载添加权限设置
@ -69,20 +79,30 @@ class AttachmentsController < ApplicationController
if candown || User.current.admin? || User.current.id == @attachment.author_id if candown || User.current.admin? || User.current.id == @attachment.author_id
@attachment.increment_download @attachment.increment_download
if stale?(:etag => @attachment.digest) if stale?(:etag => @attachment.digest)
convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".html") if params[:force] == 'true'
if File.exist?(convered_file) direct_download
send_file convered_file, :type => 'text/html; charset=utf-8', :disposition => 'inline'
else else
send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), convered_file = @attachment.diskfile
:type => detect_content_type(@attachment), #如果本身不是pdf文件则先寻找是不是已转换化如果没有则转化
:disposition => 'attachment' #inline can open in browser 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.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
end
end end
end end
else else
render_403 :message => :notice_not_authorized render_403 :message => :notice_not_authorized
end end
rescue => e rescue => e
redirect_to "http: //" + (Setting.host_name.to_s) +"/file_not_found.html" redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html"
end end
#更新资源文件类型 #更新资源文件类型

@ -72,8 +72,8 @@ class Attachment < ActiveRecord::Base
@@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails") @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails")
before_save :files_to_final_location before_save :files_to_final_location
after_create :office_conver, :be_user_score ,:act_as_forge_activity# user_score after_create :office_conver, :be_user_score,:act_as_forge_activity# user_score
after_update :be_user_score after_update :office_conver, :be_user_score
after_destroy :delete_from_disk,:down_user_score after_destroy :delete_from_disk,:down_user_score
# add by nwb # add by nwb
@ -259,12 +259,14 @@ class Attachment < ActiveRecord::Base
end end
def office_conver def office_conver
saved_path = File.join(Rails.root, "files", "convered_office") # 不在这里做后台转换,换为点击时做转换
unless Dir.exist?(saved_path) # return unless %w(Project Course).include? (self.container_type)
Dir.mkdir(saved_path) # saved_path = File.join(Rails.root, "files", "convered_office")
end # unless Dir.exist?(saved_path)
convered_file = File.join(saved_path, self.disk_filename + ".html") # Dir.mkdir(saved_path)
OfficeConverTask.new.conver(self.diskfile, convered_file) # end
# convered_file = File.join(saved_path, self.disk_filename + ".pdf")
# OfficeConverTask.new.conver(self.diskfile, convered_file)
end end
# Copies the temporary file to its final location # Copies the temporary file to its final location

@ -31,15 +31,16 @@
<%= link_to("选入我的其他课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select c_lorange",:remote => true) if has_course?(User.current,file) %> <%= link_to("选入我的其他课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select c_lorange",:remote => true) if has_course?(User.current,file) %>
<% if delete_allowed && file.container_id == @course.id && file.container_type == "Course" %> <% if delete_allowed && file.container_id == @course.id && file.container_type == "Course" %>
<span id="is_public_<%= file.id %>"> <span id="is_public_<%= file.id %>">
<%= link_to (file.is_public? ? "公开":"私有"), update_file_dense_attachments_path(:attachmentid=>file.id,:newtype=>(file.is_public? ? 0:1)),:remote=>true,:class=>"f_l re_open c_blue",:method => :post %> <%= link_to (file.is_public? ? "公开":"私有"), update_file_dense_attachments_path(:attachmentid=>file.id,:newtype=>(file.is_public? ? 0:1)),:remote=>true,:class=>"f_l re_open c_blue",:method => :post %>
</span> </span>
<% else %> <% else %>
<!-- <#%= link_to (file.is_public? ? "公开":"私有"),"javascript:void(0)",:class=>"f_l re_open" %> --> <!-- <#%= link_to (file.is_public? ? "公开":"私有"),"javascript:void(0)",:class=>"f_l re_open" %> -->
<% end %> <% end %>
<% else %> <% else %>
<%= link_to("选入我的课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select c_lorange",:remote => true) if has_course?(User.current,file) %> <%= link_to("选入我的课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select c_lorange",:remote => true) if has_course?(User.current,file) %>
<% end %> <% end %>
<%= link_to '下载',download_named_attachment_path(file.id, file.filename, force: true),class: 'f_l re_open' %>
<% else %> <% else %>
<% end %> <% end %>
</div> </div>

@ -30,6 +30,7 @@
<% else %> <% else %>
<%= link_to(l(:label_slected_to_project),quote_resource_show_project_project_file_path(project,file),:class => "f_l re_select",:remote => true) if has_project?(User.current,file) %> <%= link_to(l(:label_slected_to_project),quote_resource_show_project_project_file_path(project,file),:class => "f_l re_select",:remote => true) if has_project?(User.current,file) %>
<% end %> <% end %>
<%= link_to '下载',download_named_attachment_path(file.id, file.filename, force: true),class: 'f_l re_open' %>
<% end %> <% end %>
</div> </div>
<div class="cl"></div> <div class="cl"></div>
@ -37,7 +38,7 @@
<p class="f_l c_grey02 font">文件大小:<%= number_to_human_size(file.filesize) %></p> <p class="f_l c_grey02 font">文件大小:<%= number_to_human_size(file.filesize) %></p>
<%= link_to( l(:button_delete), attachment_path(file), <%= link_to( l(:button_delete), attachment_path(file),
:data => {:confirm => l(:text_are_you_sure)}, :method => :delete,:class => "f_r re_de") if manage_allowed && file.container_id == project.id && file.container_type == "Project"%> :data => {:confirm => l(:text_are_you_sure)}, :method => :delete,:class => "f_r re_de") if manage_allowed && file.container_id == project.id && file.container_type == "Project"%>
<p class="f_r c_grey02" ><%= time_tag(file.created_on).html_safe %><%= l(:label_bids_published_ago) %>&nbsp;&nbsp;|&nbsp;&nbsp;下载<%= file.downloads %>&nbsp;&nbsp;|&nbsp;&nbsp;引用<%= file.quotes.nil? ? 0:file.quotes %> </p> <p class="f_r c_grey02" ><%= time_tag(file.created_on).html_safe %><%= l(:label_bids_published_ago) %>&nbsp;&nbsp;|&nbsp;&nbsp;下载<%= file.downloads %>&nbsp;&nbsp;|&nbsp;&nbsp;引用<%= file.quotes.nil? ? 0:file.quotes %> </p>
</div> </div>
<div class="cl"></div> <div class="cl"></div>
<div class="tag_h"> <div class="tag_h">

@ -131,9 +131,9 @@
<%= link_to image_tag(url_to_avatar(reply.author), :class => "avatar"), user_path(reply.author) %> <%= link_to image_tag(url_to_avatar(reply.author), :class => "avatar"), user_path(reply.author) %>
</td> </td>
<td class="comments" style="word-wrap: break-word;word-break: break-all;"> <td class="comments" style="word-wrap: break-word;word-break: break-all;">
<div class="reply_content" > <div class="reply_content" >
<%=h sanitize(reply.content.html_safe) %> <%=h sanitize(reply.content.html_safe) %>
</div> </div>
<p> <p>
<% if reply.attachments.any?%> <% if reply.attachments.any?%>
<% options = {:author => true, :deletable => reply.deleted_attach_able_by?(User.current) } %> <% options = {:author => true, :deletable => reply.deleted_attach_able_by?(User.current) } %>

@ -1,3 +0,0 @@
if Rails.env.production?
config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expires_in: 90.minutes }
end

@ -67,6 +67,8 @@ module RedmineApp
# Do not include all helpers # Do not include all helpers
config.action_controller.include_all_helpers = false config.action_controller.include_all_helpers = false
config.action_view.sanitized_allowed_tags = 'div', 'p', 'span', 'img', 'embed'
if File.exists?(File.join(File.dirname(__FILE__), 'additional_environment.rb')) if File.exists?(File.join(File.dirname(__FILE__), 'additional_environment.rb'))
instance_eval File.read(File.join(File.dirname(__FILE__), 'additional_environment.rb')) instance_eval File.read(File.join(File.dirname(__FILE__), 'additional_environment.rb'))
end end

@ -1,16 +1,18 @@
#coding=utf-8
namespace :office do namespace :office do
desc "conver any files to html" desc "conver any files to html"
task :conver => :environment do task :conver => :environment do
all_count = Attachment.count all_count = Attachment.where(["container_type IN (?)", %w(Project Course)]).count
i = 0 i = 0
Attachment.find_each do |a| Attachment.where(["container_type IN (?)", %w(Project Course)]).find_each do |a|
i += 1 i += 1
puts "process [#{i}/#{all_count}] => id #{a.id}" puts "process [#{i}/#{all_count}] => id #{a.id}"
saved_path = File.join(Rails.root, "files", "convered_office") saved_path = File.join(Rails.root, "files", "convered_office")
unless Dir.exist?(saved_path) unless Dir.exist?(saved_path)
Dir.mkdir(saved_path) Dir.mkdir(saved_path)
end end
convered_file = File.join(saved_path, a.disk_filename + ".html") convered_file = File.join(saved_path, a.disk_filename + ".pdf")
office = Trustie::Utils::Office.new(a.diskfile) office = Trustie::Utils::Office.new(a.diskfile)
if office.conver(convered_file) if office.conver(convered_file)
puts "process ok: #{convered_file} " puts "process ok: #{convered_file} "

@ -1,3 +1,5 @@
#coding=utf-8
module Trustie module Trustie
module Utils module Utils
class Office class Office
@ -7,7 +9,7 @@ module Trustie
end end
def office? def office?
%w(doc docx ppt pptx xls xlsx pdf).any?{|word| @file.downcase.end_with?(word)} %w(doc docx ppt pptx xls xlsx).any?{|word| @file.downcase.end_with?(word)}
end end
def conver(saved_file, force=false) def conver(saved_file, force=false)
@ -15,14 +17,18 @@ module Trustie
if File.exist? @file if File.exist? @file
if office? if office?
begin begin
resource = RestClient::Resource.new( # resource = RestClient::Resource.new(
'http://192.168.80.107/Any2HtmlHandler.ashx', # 'http://192.168.80.107/Any2HtmlHandler.ashx',
:timeout => -1, # :timeout => -1,
:open_timeout => -1 # :open_timeout => -1
) # )
req = resource.post :txtDes => File.new(@file, 'rb') # req = resource.post :txtDes => File.new(@file, 'rb')
File.open(saved_file, "wb+") do |f| req = RestClient.post 'http://192.168.80.107/Any2HtmlHandler.ashx',:txtDes => File.new(@file, 'rb')
f.write(req.body) File.delete(saved_file) if File.exist?(saved_file)
if req.body.length > 10 && !req.body.eql?('转换出错')
File.open(saved_file, "wb+") do |f|
f.write(req.body)
end
end end
return true return true
rescue =>e rescue =>e

Loading…
Cancel
Save