#9 Active Storage - rubocop

This commit is contained in:
Karel Pičman 2025-12-17 12:02:36 +01:00
parent 1201d3ef00
commit 15384c61e4

View File

@ -134,28 +134,26 @@ class ActiveStorageMigration < ActiveRecord::Migration[7.0]
end end
end end
def storage_base_path(r) def storage_base_path(rev)
time = r.created_at || DateTime.current time = rev.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 new_storage_filename(r, name) def new_storage_filename(rev, name)
filename = DmsfHelper.sanitize_filename(name) filename = DmsfHelper.sanitize_filename(name)
timestamp = DateTime.current.strftime('%y%m%d%H%M%S') 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! while File.exist? storage_base_path(rev).join("#{timestamp}_#{rev.dmsf_file.id}_#{filename}")
timestamp.succ! "#{timestamp}_#{rev.dmsf_file.id}_#{filename}"
end
"#{timestamp}_#{r.dmsf_file.id}_#{filename}"
end end
def disk_file(r, attachment) def disk_file(rev, attachment)
path = storage_base_path(r) path = storage_base_path(rev)
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
filename = new_storage_filename(r, attachment.blob&.filename&.to_s) filename = new_storage_filename(rev, attachment.blob&.filename&.to_s)
path.join(filename).to_s path.join(filename).to_s
end end
end end