The file is not uploaded to the custom file field #1517

This commit is contained in:
Karel Pičman 2024-05-09 14:22:37 +02:00
parent 944d73bb41
commit e25fc851cf
7 changed files with 116 additions and 25 deletions

View File

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

View File

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

View File

@ -22,9 +22,9 @@
module DmsfFilesHelper
def clean_wiki_text(text)
# If there is <p> tag, the text is moved one column to the right by Redmin's CSS. A new line causes double new line.
text.gsub! '<p>', ''
text.gsub! '</p>', ''
text.gsub! "\n\n", '<br>'
text.gsub "\n\t", '<br>'
text.gsub('<p>', '')
.gsub('</p>', '')
.gsub("\n\n", '<br>')
.gsub("\n\t", '<br>')
end
end

View File

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

View File

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

View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
# Redmine plugin for Document Management System "Features"
#
# Karel Pičman <karel.picman@kontron.com>
#
# 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 = "<p>xxx</p>\n\n\n\t"
assert_equal 'xxx<br><br>', clean_wiki_text(text)
end
end

View File

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