Skip to content

Commit

Permalink
Stop gt 255 filenames breaking export (#530)
Browse files Browse the repository at this point in the history
* truncate filenames that exporter uses that are greater than 255 chars

* remove unneccessary filename=

* rubocop: add space around operators
  • Loading branch information
cziaarm authored May 24, 2022
1 parent df7fcc5 commit 7b058cc
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions app/models/concerns/bulkrax/export_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ def write_files
end
end

# Prepend the file_set id to ensure a unique filename
# Prepend the file_set id to ensure a unique filename and also one that is not longer than 255 characters
def filename(file_set)
return if file_set.original_file.blank?
fn = file_set.original_file.file_name.first
mime = Mime::Type.lookup(file_set.original_file.mime_type)
ext_mime = MIME::Types.of(file_set.original_file.file_name).first
if fn.include?(file_set.id) || importerexporter.metadata_only?
return fn if mime.to_s == ext_mime.to_s
return "#{fn}.#{mime.to_sym}"
filename = "#{fn}.#{mime.to_sym}"
filename = fn if mime.to_s == ext_mime.to_s
else
return "#{file_set.id}_#{fn}" if mime.to_s == ext_mime.to_s
return "#{file_set.id}_#{fn}.#{mime.to_sym}"
filename = "#{file_set.id}_#{fn}.#{mime.to_sym}"
filename = "#{file_set.id}_#{fn}" if mime.to_s == ext_mime.to_s
end
# Remove extention truncate and reattach
ext = File.extname(filename)
"#{File.basename(filename, ext)[0...(255 - ext.length)]}#{ext}"
end
end
end

0 comments on commit 7b058cc

Please sign in to comment.