#9 Active Storage - migration
This commit is contained in:
parent
de4806498e
commit
1a429c4d47
@ -84,7 +84,7 @@ class ActiveStorageMigration < ActiveRecord::Migration[7.0]
|
|||||||
# Migrate attachments
|
# Migrate attachments
|
||||||
ActiveStorage::Attachment.find_each do |a|
|
ActiveStorage::Attachment.find_each do |a|
|
||||||
r = a.record
|
r = a.record
|
||||||
new_path = disk_file(r)
|
new_path = disk_file(r, a)
|
||||||
unless File.exist?(new_path)
|
unless File.exist?(new_path)
|
||||||
a.blob.open do |f|
|
a.blob.open do |f|
|
||||||
# Move the attachment
|
# Move the attachment
|
||||||
@ -92,7 +92,7 @@ class ActiveStorageMigration < ActiveRecord::Migration[7.0]
|
|||||||
r.record_timestamps = false # Do not modify updated_at column
|
r.record_timestamps = false # Do not modify updated_at column
|
||||||
DmsfFileRevision.no_touching do
|
DmsfFileRevision.no_touching do
|
||||||
# Mime type
|
# Mime type
|
||||||
r.mime_type = f.content_type
|
r.mime_type = a.blob.content_type
|
||||||
# Disk filename
|
# Disk filename
|
||||||
r.disk_filename = File.basename(new_path)
|
r.disk_filename = File.basename(new_path)
|
||||||
# Digest
|
# Digest
|
||||||
@ -134,18 +134,28 @@ class ActiveStorageMigration < ActiveRecord::Migration[7.0]
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def storage_base_path(dmsf_file_revision)
|
def storage_base_path(r)
|
||||||
time = dmsf_file_revision.created_at || DateTime.current
|
time = r.created_at || DateTime.current
|
||||||
DmsfFile.storage_path.join(time.strftime('%Y')).join time.strftime('%m')
|
DmsfFile.storage_path.join(time.strftime('%Y')).join time.strftime('%m')
|
||||||
end
|
end
|
||||||
|
|
||||||
def disk_file(dmsf_file_revision)
|
def new_storage_filename(r, name)
|
||||||
path = storage_base_path(dmsf_file_revision)
|
filename = DmsfHelper.sanitize_filename(name)
|
||||||
|
timestamp = DateTime.current.strftime('%y%m%d%H%M%S')
|
||||||
|
while File.exist? storage_base_path(r).join("#{timestamp}_#{r.dmsf_file.id}_#{filename}")
|
||||||
|
timestamp.succ!
|
||||||
|
end
|
||||||
|
"#{timestamp}_#{r.dmsf_file.id}_#{filename}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def disk_file(r, attachment)
|
||||||
|
path = storage_base_path(r)
|
||||||
begin
|
begin
|
||||||
FileUtils.mkdir_p path
|
FileUtils.mkdir_p path
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
Rails.logger.error e.message
|
Rails.logger.error e.message
|
||||||
end
|
end
|
||||||
path.join(dmsf_file_revision.disk_filename).to_s
|
filename = new_storage_filename(r, attachment.blob&.filename&.to_s)
|
||||||
|
path.join(filename).to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user