From c331e5a9c8933064f8460de75e6cbec8a5805b42 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 17:42:41 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E8=BD=AChtml=E6=94=B9=E4=B8=BA=E8=BD=ACp?= =?UTF-8?q?df?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 13 ++++++++++--- lib/trustie/utils/office.rb | 9 ++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index c8b6a6fda..9c3b624af 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -62,6 +62,10 @@ class AttachmentsController < ApplicationController render :action => 'file' end + def pdf?(file) + file.downcase.end_with?(".pdf") + end + def download # modify by nwb # 下载添加权限设置 @@ -69,9 +73,12 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".html") - if File.exist?(convered_file) - send_file convered_file, :type => 'text/html; charset=utf-8', :disposition => 'inline' + convered_file = @attachment.disk_filename + unless pdf?(convered_file) + convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf") + end + if File.exist?(convered_file) && pdf?(convered_file) + send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline' else send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), :type => detect_content_type(@attachment), diff --git a/lib/trustie/utils/office.rb b/lib/trustie/utils/office.rb index 1bc18cb84..adfa3fb5c 100644 --- a/lib/trustie/utils/office.rb +++ b/lib/trustie/utils/office.rb @@ -7,7 +7,7 @@ module Trustie end 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 def conver(saved_file, force=false) @@ -21,8 +21,11 @@ module Trustie :open_timeout => -1 ) req = resource.post :txtDes => File.new(@file, 'rb') - File.open(saved_file, "wb+") do |f| - 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 return true rescue =>e From 9ecb119aa46354f9e1de47166d6c79857c83499a Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 17:43:42 +0800 Subject: [PATCH 02/11] =?UTF-8?q?task=E4=B8=AD=E4=B9=9F=E6=94=B9=E4=B8=BAp?= =?UTF-8?q?df?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/office.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/office.rake b/lib/tasks/office.rake index 3e4c0a286..ce2d3a221 100644 --- a/lib/tasks/office.rake +++ b/lib/tasks/office.rake @@ -10,7 +10,7 @@ namespace :office do unless Dir.exist?(saved_path) Dir.mkdir(saved_path) 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) if office.conver(convered_file) puts "process ok: #{convered_file} " From e4d68e988ea2b95b06d5d842f9369b8aaa5200ce Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 17:52:59 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=8A=A0=E4=B8=8Autf8=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 2 +- lib/tasks/office.rake | 2 ++ lib/trustie/utils/office.rb | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 9c3b624af..d3781d747 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -73,7 +73,7 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - convered_file = @attachment.disk_filename + convered_file = @attachment.diskfile unless pdf?(convered_file) convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf") end diff --git a/lib/tasks/office.rake b/lib/tasks/office.rake index ce2d3a221..c6ee8b4dd 100644 --- a/lib/tasks/office.rake +++ b/lib/tasks/office.rake @@ -1,3 +1,5 @@ +#coding=utf-8 + namespace :office do desc "conver any files to html" task :conver => :environment do diff --git a/lib/trustie/utils/office.rb b/lib/trustie/utils/office.rb index adfa3fb5c..61d6b9d4c 100644 --- a/lib/trustie/utils/office.rb +++ b/lib/trustie/utils/office.rb @@ -1,3 +1,5 @@ +#coding=utf-8 + module Trustie module Utils class Office From 3cc6fc1a64663df304b07a8059361dc994162069 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 18:15:08 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=88=A0=E6=8E=89additional=5Fenviroment?= =?UTF-8?q?.rb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/additional_environment.rb | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 config/additional_environment.rb diff --git a/config/additional_environment.rb b/config/additional_environment.rb deleted file mode 100644 index 5c73f6aab..000000000 --- a/config/additional_environment.rb +++ /dev/null @@ -1,3 +0,0 @@ -if Rails.env.production? - config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expires_in: 90.minutes } -end \ No newline at end of file From f86acbb10f08b99175f722536476a54209dcde05 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 18:30:49 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E9=9C=80=E8=A6=81=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E7=9A=84=E9=A2=84=E8=A7=88=E6=96=87=E4=BB=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/office.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/office.rake b/lib/tasks/office.rake index c6ee8b4dd..8bfc0c0ea 100644 --- a/lib/tasks/office.rake +++ b/lib/tasks/office.rake @@ -3,9 +3,9 @@ namespace :office do desc "conver any files to html" task :conver => :environment do - all_count = Attachment.count + all_count = Attachment.where(["container_type IN (?)", %w(Project Course)]).count i = 0 - Attachment.find_each do |a| + Attachment.where(["container_type IN (?)", %w(Project Course)]).find_each do |a| i += 1 puts "process [#{i}/#{all_count}] => id #{a.id}" saved_path = File.join(Rails.root, "files", "convered_office") From d2eb51bbb6f47644d64e156b54c3e8e26535326b Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 18:47:23 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=8F=AA=E5=A4=84=E7=90=86=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=BA=93=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/attachment.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 1e477ed04..4a6580a7a 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -259,11 +259,12 @@ class Attachment < ActiveRecord::Base end def office_conver + return unless %w(Project Course).included? (self.container_type) saved_path = File.join(Rails.root, "files", "convered_office") unless Dir.exist?(saved_path) Dir.mkdir(saved_path) end - convered_file = File.join(saved_path, self.disk_filename + ".html") + convered_file = File.join(saved_path, self.disk_filename + ".pdf") OfficeConverTask.new.conver(self.diskfile, convered_file) end From 3f9c5764e96f82e57c4438296c4e166df9959bfa Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 18:50:26 +0800 Subject: [PATCH 07/11] . --- app/models/attachment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 4a6580a7a..380e98e02 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -259,7 +259,7 @@ class Attachment < ActiveRecord::Base end def office_conver - return unless %w(Project Course).included? (self.container_type) + return unless %w(Project Course).include? (self.container_type) saved_path = File.join(Rails.root, "files", "convered_office") unless Dir.exist?(saved_path) Dir.mkdir(saved_path) From d3baada893c2ceba398dfc399518d2cb6ee05d31 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 18:57:05 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=9C=A8=E6=9B=B4=E6=96=B0Attachments?= =?UTF-8?q?=E6=97=B6=E4=B9=9F=E8=A6=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/attachment.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 380e98e02..1f1d61581 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -72,8 +72,8 @@ class Attachment < ActiveRecord::Base @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails") before_save :files_to_final_location - after_create :office_conver, :be_user_score ,:act_as_forge_activity# user_score - after_update :be_user_score + after_create :office_conver, :be_user_score,:act_as_forge_activity# user_score + after_update :office_conver, :be_user_score after_destroy :delete_from_disk,:down_user_score # add by nwb From 444658d0b3a8ef46431396bd9ec7ffb3f6df8364 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 4 Jun 2015 10:24:05 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=8F=98=E4=B8=BA?= =?UTF-8?q?=E6=97=A2=E6=97=B6=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 33 ++++++++++++++------- app/models/attachment.rb | 15 +++++----- app/views/files/_project_file_list.html.erb | 5 ++-- lib/trustie/utils/office.rb | 13 ++++---- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index d3781d747..8ad48c9b2 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -66,6 +66,12 @@ class AttachmentsController < ApplicationController 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 # modify by nwb # 下载添加权限设置 @@ -73,23 +79,30 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - convered_file = @attachment.diskfile - unless pdf?(convered_file) - convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf") - end - if File.exist?(convered_file) && pdf?(convered_file) - send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline' + if params[:force] == 'true' + direct_download else - send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), - :type => detect_content_type(@attachment), - :disposition => 'attachment' #inline can open in browser + convered_file = @attachment.diskfile + #如果本身不是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.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 else render_403 :message => :notice_not_authorized end 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 #更新资源文件类型 diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 1f1d61581..f999e27d6 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -259,13 +259,14 @@ class Attachment < ActiveRecord::Base end def office_conver - return unless %w(Project Course).include? (self.container_type) - saved_path = File.join(Rails.root, "files", "convered_office") - unless Dir.exist?(saved_path) - Dir.mkdir(saved_path) - end - convered_file = File.join(saved_path, self.disk_filename + ".pdf") - OfficeConverTask.new.conver(self.diskfile, convered_file) + # 不在这里做后台转换,换为点击时做转换 + # return unless %w(Project Course).include? (self.container_type) + # saved_path = File.join(Rails.root, "files", "convered_office") + # unless Dir.exist?(saved_path) + # Dir.mkdir(saved_path) + # end + # convered_file = File.join(saved_path, self.disk_filename + ".pdf") + # OfficeConverTask.new.conver(self.diskfile, convered_file) end # Copies the temporary file to its final location diff --git a/app/views/files/_project_file_list.html.erb b/app/views/files/_project_file_list.html.erb index 35aa36195..a5152ecdc 100644 --- a/app/views/files/_project_file_list.html.erb +++ b/app/views/files/_project_file_list.html.erb @@ -30,6 +30,7 @@ <% 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) %> <% end %> + <%= link_to '下载',download_named_attachment_path(file.id, file.filename, force: true),class: 'f_l re_open' %> <% end %>
@@ -37,7 +38,7 @@文件大小:<%= number_to_human_size(file.filesize) %>
<%= 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"%> -<%= time_tag(file.created_on).html_safe %><%= l(:label_bids_published_ago) %> | 下载<%= file.downloads %> | 引用<%= file.quotes.nil? ? 0:file.quotes %>
+<%= time_tag(file.created_on).html_safe %><%= l(:label_bids_published_ago) %> | 下载<%= file.downloads %> | 引用<%= file.quotes.nil? ? 0:file.quotes %>
<% if reply.attachments.any?%> <% options = {:author => true, :deletable => reply.deleted_attach_able_by?(User.current) } %> @@ -183,4 +183,4 @@ }); }; $("img").removeAttr("align"); - \ No newline at end of file + diff --git a/config/application.rb b/config/application.rb index 9e7a2ba81..83ba21b05 100644 --- a/config/application.rb +++ b/config/application.rb @@ -67,6 +67,8 @@ module RedmineApp # Do not include all helpers 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')) instance_eval File.read(File.join(File.dirname(__FILE__), 'additional_environment.rb')) end diff --git a/db/schema.rb b/db/schema.rb index 42c743588..1a59ccc86 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -606,6 +606,13 @@ ActiveRecord::Schema.define(:version => 20150602055730) do t.datetime "updated_at", :null => false end + create_table "invite_lists", :force => true do |t| + t.integer "project_id" + t.integer "user_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "issue_categories", :force => true do |t| t.integer "project_id", :default => 0, :null => false t.string "name", :limit => 30, :default => "", :null => false