#9 Active Storage - new revisions

This commit is contained in:
Karel Pičman 2025-12-12 14:03:31 +01:00
parent 64de6a1e9d
commit 911457de79
7 changed files with 15 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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