You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using a script to "fix" the filename metadata for some images my client uploaded. Despite repeated warnings, they put punctuation and white space in the actual file names.
The name of the file is part of the URL, and important to me and various web crawlers for SEO reasons. I also needed to have predictable filenames in storage on S3 so I could align them with CloudFront and still serve my desired URLs.
While running this latest script, I noticed the JSON [attacher]_data column had this value:
Note the doubling of the filename key, once null, and a second time with a value. Now because the last value "wins", this doesn't break anything else in my app. But it is odd. And I'm not sure where to go looking for my error (which I assume is why this is happening at all, not Shrine itself).
# renaming script ...
.find_each do |s|
begin
tmp = StringIO.new(s.file.read, 'rb')
old_name = s.file.metadata['filename'].dup
s.file_uid ||= old_name
new_name = s.safe_name(old_name)
s.file_attacher.assign(tmp, metadata: { filename: new_name })
s.save
puts new_name
rescue Shrine::FileNotFound => e
puts "\n\n\n#{e.message}\n\n\n"
end
end
This is perhaps fighting with the metadata_attributes plugin invoked here:
class PhotoUploader < Shrine
plugin :remove_attachment
plugin :metadata_attributes, filename: :name
plugin :determine_mime_type
def generate_location(io, record: nil, derivative: nil, **)
return super unless record.persisted?
table = record.attachable&.class&.table_name || 'loose'
id = record.attachable_id || 'images'
file = record.file_name
"#{table}/#{id}/#{file}"
end
end
Interestingly, the file_name attribute in the database (being set by the metadata_attributes plugin configuration here) is also null after this runs. So that tells me that the first instance of the filename key is probably still nil when the plugin comes through to make its column assignment, and is only getting set afterwards, as the duplicate key. (This is what led me to stash the original filename in the now-unused file_uid column left over from when this app used Paperclip or CarrierWave or something.)
Questions:
Is the hash best interacted with using string keys rather than symbols? It all gets flattened out later during JSON conversion, I presume, perhaps leading to this apparent duplication.
Is there a better way to assign the desired filename in the metadata?
Am I messing something up with my combination of the generate_location, metadata_attributes, and active_record plugins?
Thanks in advance for any suggestions, follow-up questions, or general advice you can offer!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am using a script to "fix" the filename metadata for some images my client uploaded. Despite repeated warnings, they put punctuation and white space in the actual file names.
The name of the file is part of the URL, and important to me and various web crawlers for SEO reasons. I also needed to have predictable filenames in storage on S3 so I could align them with CloudFront and still serve my desired URLs.
While running this latest script, I noticed the JSON [attacher]_data column had this value:
Note the doubling of the filename key, once null, and a second time with a value. Now because the last value "wins", this doesn't break anything else in my app. But it is odd. And I'm not sure where to go looking for my error (which I assume is why this is happening at all, not Shrine itself).
Main Shrine config:
This is perhaps fighting with the metadata_attributes plugin invoked here:
Interestingly, the
file_name
attribute in the database (being set by themetadata_attributes
plugin configuration here) is also null after this runs. So that tells me that the first instance of thefilename
key is probably still nil when the plugin comes through to make its column assignment, and is only getting set afterwards, as the duplicate key. (This is what led me to stash the original filename in the now-unused file_uid column left over from when this app used Paperclip or CarrierWave or something.)Questions:
generate_location
,metadata_attributes
, andactive_record
plugins?Thanks in advance for any suggestions, follow-up questions, or general advice you can offer!
Walter
Beta Was this translation helpful? Give feedback.
All reactions