From e25fc851cf4a895994c7cbfad9128219f95d2e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Thu, 9 May 2024 14:22:37 +0200 Subject: [PATCH] The file is not uploaded to the custom file field #1517 --- .rubocop.yml | 4 ++ app/controllers/dmsf_files_controller.rb | 10 +---- app/helpers/dmsf_files_helper.rb | 8 ++-- app/helpers/dmsf_upload_helper.rb | 13 +----- app/models/dmsf_file_revision.rb | 50 +++++++++++++++++++++++- test/helpers/dmsf_files_helper_test.rb | 31 +++++++++++++++ test/unit/dmsf_file_revision_test.rb | 25 ++++++++++++ 7 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 test/helpers/dmsf_files_helper_test.rb diff --git a/.rubocop.yml b/.rubocop.yml index 5bdfa537..88f09e33 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -37,6 +37,10 @@ Layout/LineLength: Exclude: - app/models/dmsf_query.rb +Lint/SymbolConversion: + Exclude: + - test/unit/dmsf_file_revision_test.rb + Lint/ScriptPermission: Exclude: - extra/xapian_indexer.rb diff --git a/app/controllers/dmsf_files_controller.rb b/app/controllers/dmsf_files_controller.rb index 9c5540c8..9889670a 100644 --- a/app/controllers/dmsf_files_controller.rb +++ b/app/controllers/dmsf_files_controller.rb @@ -140,16 +140,8 @@ class DmsfFilesController < ApplicationController revision.mime_type = last_revision.mime_type revision.digest = last_revision.digest end - # Custom fields - if params[:dmsf_file_revision][:custom_field_values].present? - i = 0 - params[:dmsf_file_revision][:custom_field_values].each do |_, v| - revision.custom_field_values[i].value = v - i += 1 - end - end - + revision.copy_custom_field_values(params[:dmsf_file_revision][:custom_field_values], last_revision) @file.name = revision.name ok = true if revision.save diff --git a/app/helpers/dmsf_files_helper.rb b/app/helpers/dmsf_files_helper.rb index ea93a124..a1539436 100644 --- a/app/helpers/dmsf_files_helper.rb +++ b/app/helpers/dmsf_files_helper.rb @@ -22,9 +22,9 @@ module DmsfFilesHelper def clean_wiki_text(text) # If there is

tag, the text is moved one column to the right by Redmin's CSS. A new line causes double new line. - text.gsub! '

', '' - text.gsub! '

', '' - text.gsub! "\n\n", '
' - text.gsub "\n\t", '
' + text.gsub('

', '') + .gsub('

', '') + .gsub("\n\n", '
') + .gsub("\n\t", '
') end end diff --git a/app/helpers/dmsf_upload_helper.rb b/app/helpers/dmsf_upload_helper.rb index 5c9ea96e..9e2516a1 100644 --- a/app/helpers/dmsf_upload_helper.rb +++ b/app/helpers/dmsf_upload_helper.rb @@ -20,8 +20,6 @@ # Upload helper module DmsfUploadHelper - include Redmine::I18n - def self.commit_files_internal(committed_files, project, folder, controller = nil, container = nil, new_object: false) failed_uploads = [] files = [] @@ -72,15 +70,8 @@ module DmsfUploadHelper new_revision.mime_type = committed_file[:mime_type] new_revision.size = committed_file[:size] new_revision.digest = committed_file[:digest] - - if committed_file[:custom_field_values].present? - i = 0 - committed_file[:custom_field_values].each do |_, v| - new_revision.custom_field_values[i].value = v - i += 1 - end - end - + # Custom fields + new_revision.copy_custom_field_values(committed_file[:custom_field_values]) # Need to save file first to generate id for it in case of creation. # File id is needed to properly generate revision disk filename unless new_revision.valid? diff --git a/app/models/dmsf_file_revision.rb b/app/models/dmsf_file_revision.rb index ee2d6b57..c854bafc 100644 --- a/app/models/dmsf_file_revision.rb +++ b/app/models/dmsf_file_revision.rb @@ -99,7 +99,7 @@ class DmsfFileRevision < ApplicationRecord validates :description, length: { maximum: 1.kilobyte } validates :size, dmsf_max_file_size: true - def visible? + def visible?(_user = nil) deleted == STATUS_ACTIVE end @@ -405,4 +405,52 @@ class DmsfFileRevision < ApplicationRecord dependencies = DmsfFileRevision.where(disk_filename: disk_filename).all.size FileUtils.rm_f(disk_file) if dependencies <= 1 end + + def copy_custom_field_values(values, source_revision = nil) + # For a new revision we need to remember attachments' ids + if source_revision + ids = [] + source_revision.custom_field_values.each do |cv| + ids << cv.value if cv.custom_field.format.is_a?(Redmine::FieldFormat::AttachmentFormat) + end + end + # ActionParameters => Hash + h = DmsfFileRevision.params_to_hash(values) + # Super + self.custom_field_values = h + # For a new revision we need to duplicate attachments + return unless source_revision + + i = 0 + custom_field_values.each do |cv| + next unless cv.custom_field.format.is_a?(Redmine::FieldFormat::AttachmentFormat) + + if cv.value.blank? && h[cv.custom_field.id.to_s].present? + a = Attachment.find_by(id: ids[i]) + copy = a.copy + copy.save + cv.value = copy.id + end + i += 1 + end + end + + # Converts ActionParameters to an ordinary Hash + def self.params_to_hash(params) + result = {} + return result if params.blank? + + h = params.permit!.to_hash + h.each do |key, value| + if value.is_a?(Hash) + value = value.except('blank') + _, v = value.first + # We need a symbols here + result[key] = v&.symbolize_keys + else + result[key] = value + end + end + result + end end diff --git a/test/helpers/dmsf_files_helper_test.rb b/test/helpers/dmsf_files_helper_test.rb new file mode 100644 index 00000000..518d9538 --- /dev/null +++ b/test/helpers/dmsf_files_helper_test.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# Redmine plugin for Document Management System "Features" +# +# 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 +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.expand_path('../../test_helper', __FILE__) + +# DMSF helper +class DmsfFilesHelperTest < RedmineDmsf::Test::HelperTest + include DmsfFilesHelper + + def test_clean_wiki_test + text = "

xxx

\n\n\n\t" + assert_equal 'xxx

', clean_wiki_text(text) + end +end diff --git a/test/unit/dmsf_file_revision_test.rb b/test/unit/dmsf_file_revision_test.rb index 436d1ba3..c8e84ee4 100644 --- a/test/unit/dmsf_file_revision_test.rb +++ b/test/unit/dmsf_file_revision_test.rb @@ -277,4 +277,29 @@ class DmsfFileRevisionTest < RedmineDmsf::Test::UnitTest @revision1.size = 2.kilobytes assert_not @revision1.valid? end + + def test_visible + @revision1.deleted = DmsfFileRevision::STATUS_ACTIVE + assert @revision1.visible? + assert @revision1.visible?(@jsmith) + @revision1.deleted = DmsfFileRevision::STATUS_DELETED + assert_not @revision1.visible? + assert_not @revision1.visible?(@jsmith) + end + + def test_params_to_hash + parameters = ActionController::Parameters.new({ + '78': 'A', + '90': { + 'blank': '', + '1': { + 'filename': 'file.txt', + 'token': 'atoken' + } + } + }) + h = DmsfFileRevision.params_to_hash(parameters) + assert h.is_a?(Hash) + assert_equal 'atoken', h['90'][:token] + end end