Files
danbooru/script/fixes/102_fix_upload_media_asset_urls.rb
evazion 04d242c60c uploads: save filename, image URL, page URL for uploads.
* Save the filename for files uploaded from disk. This could be used in
  the future to extract source data if the filename is from a known site.

* Save both the image URL and the page URL for files uploaded from
  source. This is needed for multi-file uploads. The image URL is the
  URL of the file actually downloaded from the source. This can be
  different from the URL given by the user, if the user tried to upload
  a sample URL and we automatically changed it to the original URL. The
  page URL is the URL of the page containing the image. We don't always
  know this, for example if someone uploads a Twitter image without the
  bookmarklet, then we can't find the page URL.

* Add a fix script to backfill URLs for existing uploads. For file
  uploads, the filename will be set to "unknown.jpg". For source
  uploads, we fetch the source data again to get the image and page
  URLs. This may fail for uploads that have been deleted from the
  source since uploading.
2022-02-12 15:22:41 -06:00

23 lines
770 B
Ruby
Executable File

#!/usr/bin/env ruby
require_relative "base"
UploadMediaAsset.includes(:upload, :media_asset).where(source_url: "").find_each do |uma|
upload = uma.upload
if upload.source_strategy.present?
source_url = uma.upload.source_strategy.image_url
page_url = uma.upload.source_strategy.page_url
else
file_ext = uma.media_asset.file_ext
source_url = "file://unknown.#{file_ext}"
end
raise "No source url for #{page_url}" if source_url.blank?
uma.update_columns(source_url: source_url, page_url: page_url)
puts ({ upload_id: uma.upload.id, upload_media_asset_id: uma.id, source_url: source_url, page_url: page_url }).to_json
rescue Exception => e
puts ({ upload_id: uma.upload.id, upload_media_asset_id: uma.id, error: e.message }).to_json
end