`normalize_for_source` was used to convert image URLs to page URLs when displaying sources on the post show page. Move all the code for converting image URLs to page URLs from `Sources::Strategies#normalize_for_source` to `Source::URL#page_url`. Before we had to be very careful in source strategies not to make any network calls in `normalize_for_source`, since it was used in the view for the post show page. Now all the code for generating page URLs is isolated in Source::URL, which makes source strategies simpler. It also makes it easier to check if a source is an image URL or page URL, and if the image URL is convertible to a page URL, which will make autotagging bad_link or bad_source feasible. Finally, this fixes it to generate better page URLs in a handful of cases: * https://www.artstation.com/artwork/qPVGP instead of https://anubis1982918.artstation.com/projects/qPVGP * https://yande.re/post/show?md5=b4b1d11facd1700544554e4805d47bb6s instead of https://yande.re/post?tags=md5:b4b1d11facd1700544554e4805d47bb6 * http://gallery.minitokyo.net/view/365677 instead of http://gallery.minitokyo.net/download/365677 * https://valkyriecrusade.fandom.com/wiki/File:Crimson_Hatsune_H.png instead of https://valkyriecrusade.wikia.com/wiki/File:Crimson_Hatsune_H.png * https://rule34.paheal.net/post/view/852405 instead of https://rule34.paheal.net/post/list/md5:854806addcd3b1246424e7cea49afe31/1
98 lines
3.6 KiB
Ruby
98 lines
3.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Note: even if the username is wrong, the url is still resolved correctly. Example:
|
|
# * https://foundation.app/@foundation/~/97376
|
|
#
|
|
# Unsupported patterns:
|
|
# * https://foundation.app/@ <- This seems to be a novelty account.
|
|
# * https://foundation.app/mochiiimo <- no @
|
|
# * https://foundation.app/collection/kgfgen
|
|
|
|
class Source::URL::Foundation < Source::URL
|
|
attr_reader :username, :token_id, :work_id, :hash
|
|
|
|
def self.match?(url)
|
|
url.host.in?(%w[foundation.app assets.foundation.app f8n-ipfs-production.imgix.net f8n-production-collection-assets.imgix.net])
|
|
end
|
|
|
|
def parse
|
|
case [host, *path_segments]
|
|
|
|
# https://foundation.app/@mochiiimo
|
|
# https://foundation.app/@KILLERGF
|
|
in "foundation.app", /^@/ => username
|
|
@username = username.delete_prefix("@")
|
|
|
|
# https://foundation.app/0x7E2ef75C0C09b2fc6BCd1C68B6D409720CcD58d2
|
|
in "foundation.app", /^0x\h{39}/ => user_id
|
|
@user_id = user_id
|
|
|
|
# https://foundation.app/@mochiiimo/~/97376
|
|
# https://foundation.app/@mochiiimo/foundation/97376
|
|
# https://foundation.app/@KILLERGF/kgfgen/4
|
|
in "foundation.app", /^@/ => username, collection, /^\d+/ => work_id
|
|
@username = username.delete_prefix("@")
|
|
@collection = collection
|
|
@work_id = work_id
|
|
|
|
# https://foundation.app/@asuka111art/dinner-with-cats-82426
|
|
in "foundation.app", /^@/ => username, /^.+-\d+$/ => slug
|
|
@username = username.delete_prefix("@")
|
|
@work_id = slug.split("-").last
|
|
|
|
# https://f8n-ipfs-production.imgix.net/QmX4MotNAAj9Rcyew43KdgGDxU1QtXemMHoUTNacMLLSjQ/nft.png
|
|
# https://f8n-ipfs-production.imgix.net/QmX4MotNAAj9Rcyew43KdgGDxU1QtXemMHoUTNacMLLSjQ/nft.png?q=80&auto=format%2Ccompress&cs=srgb&max-w=1680&max-h=1680
|
|
in "f8n-ipfs-production.imgix.net", hash, file
|
|
@hash = hash
|
|
|
|
# https://f8n-production-collection-assets.imgix.net/0x3B3ee1931Dc30C1957379FAc9aba94D1C48a5405/128711/QmcBfbeCMSxqYB3L1owPAxFencFx3jLzCPFx6xUBxgSCkH/nft.png
|
|
in "f8n-production-collection-assets.imgix.net", token_id, work_id, hash, file
|
|
@token_id = token_id
|
|
@work_id = work_id
|
|
@hash = hash
|
|
|
|
# https://f8n-production-collection-assets.imgix.net/0xFb0a8e1bB97fD7231Cd73c489dA4732Ae87995F0/4/nft.png
|
|
in "f8n-production-collection-assets.imgix.net", token_id, work_id, file
|
|
@token_id = token_id
|
|
@work_id = work_id
|
|
|
|
# https://assets.foundation.app/7i/gs/QmU8bbsjaVQpEKMDWbSZdDD6GsPmRYBhQtYRn8bEGv7igs/nft_q4.mp4
|
|
in "assets.foundation.app", *subdirs, hash, file
|
|
@hash = hash
|
|
|
|
else
|
|
end
|
|
end
|
|
|
|
def profile_url
|
|
if username.present?
|
|
"https://foundation.app/@#{username}"
|
|
elsif user_id.present?
|
|
"https://foundation.app/#{user_id}"
|
|
end
|
|
end
|
|
|
|
def page_url
|
|
return nil unless work_id.present?
|
|
return nil if host == "f8n-production-collection-assets.imgix.net" && @hash.blank?
|
|
# https://f8n-production-collection-assets.imgix.net/0xAcf67a11D93D22bbB51fddD9B039d43d5Db484Bc/3/nft.png cannot be normalized to a correct page url
|
|
|
|
username = @username || "foundation"
|
|
collection = @collection || "foundation"
|
|
"https://foundation.app/@#{username}/#{collection}/#{work_id}"
|
|
end
|
|
|
|
def full_image_url
|
|
if hash.present? && file_ext.present?
|
|
"https://f8n-ipfs-production.imgix.net/#{hash}/nft.#{file_ext}"
|
|
elsif host == "f8n-production-collection-assets.imgix.net" && token_id.present? && work_id.present? && file_ext.present?
|
|
"https://f8n-production-collection-assets.imgix.net/#{token_id}/#{work_id}/nft.#{file_ext}"
|
|
end
|
|
end
|
|
|
|
def ipfs_url
|
|
return nil unless hash.present? && file_ext.present?
|
|
"ipfs://#{hash}/nft.#{file_ext}"
|
|
end
|
|
end
|