From 911457de79f860c5939fb2f75de839a54698e0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Fri, 12 Dec 2025 14:03:31 +0100 Subject: [PATCH] #9 Active Storage - new revisions --- app/controllers/dmsf_files_controller.rb | 18 +++++++++--------- app/controllers/dmsf_upload_controller.rb | 5 ----- app/helpers/dmsf_upload_helper.rb | 2 +- app/models/dmsf_file.rb | 2 +- app/models/dmsf_file_revision.rb | 2 +- .../20251015130601_active_storage_migration.rb | 4 ++-- lib/redmine_dmsf/webdav/dmsf_resource.rb | 2 +- 7 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/controllers/dmsf_files_controller.rb b/app/controllers/dmsf_files_controller.rb index b498fdfa..d75689ac 100644 --- a/app/controllers/dmsf_files_controller.rb +++ b/app/controllers/dmsf_files_controller.rb @@ -131,24 +131,24 @@ class DmsfFilesController < ApplicationController revision.minor_version = DmsfUploadHelper.db_version(params[:version_minor]) revision.patch_version = DmsfUploadHelper.db_version(params[:version_patch]) + # New content if params[:dmsf_attachments].present? keys = params[:dmsf_attachments].keys file_upload = params[:dmsf_attachments][keys.first] if keys&.first - end - if file_upload - upload = DmsfUpload.create_from_uploaded_attachment(@project, @folder, file_upload) - if upload - revision.size = upload.size - revision.file.attach( - io: File.open(upload.tempfile_path), - filename: file_upload.filename, - content_type: Redmine::MimeType.of(file_upload.filename), + a = Attachment.find_by_token(file_upload[:token]) if file_upload + if a + revision.size = a.filesize + revision.shared_file.attach( + io: File.open(a.diskfile), + filename: a.filename, + content_type: a.content_type.presence || 'application/octet-stream', identify: false ) end else revision.size = last_revision.size end + # Custom fields revision.copy_custom_field_values(params[:dmsf_file_revision][:custom_field_values], last_revision) ok = true diff --git a/app/controllers/dmsf_upload_controller.rb b/app/controllers/dmsf_upload_controller.rb index 0b0677cc..0aaae79d 100644 --- a/app/controllers/dmsf_upload_controller.rb +++ b/app/controllers/dmsf_upload_controller.rb @@ -65,11 +65,6 @@ class DmsfUploadController < ApplicationController # REST API and Redmine attachment form def upload - unless request.media_type == 'application/octet-stream' - head :not_acceptable - return - end - @attachment = Attachment.new(file: request.body) @attachment.author = User.current @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) diff --git a/app/helpers/dmsf_upload_helper.rb b/app/helpers/dmsf_upload_helper.rb index 94f359ce..a3a4da08 100644 --- a/app/helpers/dmsf_upload_helper.rb +++ b/app/helpers/dmsf_upload_helper.rb @@ -91,7 +91,7 @@ module DmsfUploadHelper a = Attachment.find_by_token(committed_file[:token]) committed_file[:tempfile_path] = a.diskfile if a end - new_revision.file.attach( + new_revision.shared_file.attach( io: File.open(committed_file[:tempfile_path]), filename: new_revision.name, content_type: committed_file[:mime_type], diff --git a/app/models/dmsf_file.rb b/app/models/dmsf_file.rb index 78ef9396..28fedc85 100644 --- a/app/models/dmsf_file.rb +++ b/app/models/dmsf_file.rb @@ -333,7 +333,7 @@ class DmsfFile < ApplicationRecord end if last_revision.file.attached? begin - new_revision.file.attach( + new_revision.shared_file.attach( io: StringIO.new(last_revision.file.download), filename: filename, content_type: new_revision.file.content_type, diff --git a/app/models/dmsf_file_revision.rb b/app/models/dmsf_file_revision.rb index 369ec994..5c67f87d 100644 --- a/app/models/dmsf_file_revision.rb +++ b/app/models/dmsf_file_revision.rb @@ -278,7 +278,7 @@ class DmsfFileRevision < ApplicationRecord end def copy_file_content(open_file) - file.attach io: open_file, filename: dmsf_file.name + shared_file.attach io: open_file, filename: dmsf_file.name end # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields diff --git a/db/migrate/20251015130601_active_storage_migration.rb b/db/migrate/20251015130601_active_storage_migration.rb index e844b37e..c9ca3d5e 100644 --- a/db/migrate/20251015130601_active_storage_migration.rb +++ b/db/migrate/20251015130601_active_storage_migration.rb @@ -44,10 +44,10 @@ class ActiveStorageMigration < ActiveRecord::Migration[7.0] .find_each .with_index do |r, i| if i.zero? - r.file.attach( + r.shared_file.attach( io: File.open(path), filename: r.name, - content_type: r.content_type, + content_type: r.content_type || 'application/octet-stream', identify: false ) # Remove the original file diff --git a/lib/redmine_dmsf/webdav/dmsf_resource.rb b/lib/redmine_dmsf/webdav/dmsf_resource.rb index 6c228b33..8ce33526 100644 --- a/lib/redmine_dmsf/webdav/dmsf_resource.rb +++ b/lib/redmine_dmsf/webdav/dmsf_resource.rb @@ -779,7 +779,7 @@ module RedmineDmsf r.custom_field_values << CustomValue.new({ custom_field: cf, value: cf.default_value }) end if r.save(validate: false) # Skip validation due to invalid characters in the filename - r.file.attach( + r.shared_file.attach( io: File.new(DmsfHelper.temp_filename(basename), File::CREAT), filename: basename, content_type: 'application/octet-stream',