From 951ccadf26ebc139aec692e97e19101ba160f744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Fri, 22 Aug 2014 14:53:24 +0200 Subject: [PATCH] Standard attachments upload form used for large files --- app/controllers/dmsf_upload_controller.rb | 10 ++--- app/models/dmsf_upload.rb | 45 ++++++++++++------- app/views/dmsf/show.html.erb | 2 +- .../_multi_upload.html.erb | 36 ++++----------- 4 files changed, 43 insertions(+), 50 deletions(-) rename app/views/{dmsf => dmsf_upload}/_multi_upload.html.erb (86%) diff --git a/app/controllers/dmsf_upload_controller.rb b/app/controllers/dmsf_upload_controller.rb index fa1e0347..ab2394fe 100644 --- a/app/controllers/dmsf_upload_controller.rb +++ b/app/controllers/dmsf_upload_controller.rb @@ -1,7 +1,7 @@ # Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš -# Copyright (C) 2014 Karel Pičman +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011-14 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -30,12 +30,12 @@ class DmsfUploadController < ApplicationController helper :dmsf_workflows def upload_files - uploaded_files = params[:uploaded_files] + uploaded_files = params[:attachments] @uploads = [] if uploaded_files && uploaded_files.is_a?(Hash) # standard file input uploads - uploaded_files.each_value do |uploaded_file| - @uploads.push(DmsfUpload.create_from_uploaded_file(@project, @folder, uploaded_file)) + uploaded_files.each_value do |uploaded_file| + @uploads.push(DmsfUpload.create_from_uploaded_attachment(@project, @folder, uploaded_file)) end else # plupload multi upload completed diff --git a/app/models/dmsf_upload.rb b/app/models/dmsf_upload.rb index 5fb2161a..41fc5145 100644 --- a/app/models/dmsf_upload.rb +++ b/app/models/dmsf_upload.rb @@ -30,25 +30,31 @@ class DmsfUpload attr_accessor :minor_version attr_accessor :locked attr_accessor :workflow - attr_accessor :custom_values + attr_accessor :custom_values def disk_file "#{DmsfHelper.temp_dir}/#{self.disk_filename}" - end + end - def self.create_from_uploaded_file(project, folder, uploaded_file) - uploaded = { - :disk_filename => DmsfHelper.temp_filename(uploaded_file.original_filename), - :content_type => uploaded_file.content_type.to_s, - :original_filename => uploaded_file.original_filename, - } - - File.open("#{DmsfHelper.temp_dir}/#{uploaded[:disk_filename]}", 'wb') do |f| - while (buffer = uploaded_file.read(8192)) - f.write(buffer) + def self.create_from_uploaded_attachment(project, folder, uploaded_file) + a = Attachment.find_by_token(uploaded_file[:token]) + if a + uploaded = { + :disk_filename => DmsfHelper.temp_filename(a.filename), + :content_type => a.content_type, + :original_filename => a.filename, + :comment => uploaded_file[:description] + } + File.open(a.diskfile, 'rb') do |fr| + File.open("#{DmsfHelper.temp_dir}/#{uploaded[:disk_filename]}", 'wb') do |fw| + while (buffer = fr.read(8192)) + fw.write(buffer) + end + end end + a.destroy + DmsfUpload.new(project, folder, uploaded) end - DmsfUpload.new(project, folder, uploaded) end def initialize(project, folder, uploaded) @@ -62,19 +68,24 @@ class DmsfUpload @disk_filename = uploaded[:disk_filename] @mime_type = uploaded[:content_type] - @size = File.size(disk_file) + @size = File.size(disk_file) if file.nil? || file.last_revision.nil? @title = DmsfFileRevision.filename_to_title(@name) - @description = nil + @description = uploaded[:comment] @major_version = 0 @minor_version = 0 @workflow = nil - @custom_values = DmsfFileRevision.new(:file => DmsfFile.new(:project => @project)).custom_field_values + @custom_values = DmsfFileRevision.new(:file => DmsfFile.new(:project => @project)).custom_field_values else last_revision = file.last_revision @title = last_revision.title - @description = last_revision.description + if last_revision.description.present? + @description = last_revision.description + @comment = uploaded[:comment] if uploaded[:comment].present? + elsif uploaded[:comment].present? + @comment = uploaded[:comment] + end @major_version = last_revision.major_version @minor_version = last_revision.minor_version @workflow = last_revision.workflow diff --git a/app/views/dmsf/show.html.erb b/app/views/dmsf/show.html.erb index ae1e981e..cba074ea 100644 --- a/app/views/dmsf/show.html.erb +++ b/app/views/dmsf/show.html.erb @@ -274,4 +274,4 @@ <% end %> -<%= render(:partial => 'multi_upload') if (@file_manipulation_allowed && !@locked_for_user) %> \ No newline at end of file +<%= render(:partial => 'dmsf_upload/multi_upload') if (@file_manipulation_allowed && !@locked_for_user) %> \ No newline at end of file diff --git a/app/views/dmsf/_multi_upload.html.erb b/app/views/dmsf_upload/_multi_upload.html.erb similarity index 86% rename from app/views/dmsf/_multi_upload.html.erb rename to app/views/dmsf_upload/_multi_upload.html.erb index 72569cb2..9b8df737 100644 --- a/app/views/dmsf/_multi_upload.html.erb +++ b/app/views/dmsf_upload/_multi_upload.html.erb @@ -1,7 +1,8 @@ <%# Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš -# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011-14 Karel Picman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -41,18 +42,13 @@ <%= l(:note_upload_files_greater_than_two_gb) if Setting.attachment_max_size.to_i >= 2097151 %> -
+

- - <%= file_field_tag('uploaded_files[1]', :size => 30, :id => nil) %> - -
- <%= link_to(l(:label_add_another_file), '#', :onclick => 'dmsfAddFileField(); return false;' ) %> - (<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>) - -

+ + <%= render :partial => 'attachments/form' %> +

<%= submit_tag(l(:submit_upload_files)) %> -
+
<% end %> @@ -72,21 +68,7 @@ initPlUploader(uploader); - var dmsfFileFieldCount = 1; - - function dmsfAddFileField() { - if (dmsfFileFieldCount >= 10) return false; - dmsfFileFieldCount++; - - var f = document.createElement('input'); - f.type = 'file'; - f.name = 'uploaded_files[' + dmsfFileFieldCount + ']'; - f.size = 30; - - var p = document.getElementById('uploaded_files_fields'); - p.appendChild(document.createElement('br')); - p.appendChild(f); - } + var dmsfFileFieldCount = 1; <% content_for :header_tags do %>