2017-05-18 22:43:10 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Remotable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-03-26 21:02:10 +09:00
|
|
|
class_methods do
|
2020-06-29 20:56:55 +09:00
|
|
|
def remotable_attachment(attachment_name, limit, suppress_errors: true, download_on_assign: true, attribute_name: nil)
|
2023-12-02 00:53:35 +09:00
|
|
|
attribute_name ||= :"#{attachment_name}_remote_url"
|
2020-06-29 20:56:55 +09:00
|
|
|
|
2023-12-18 19:26:09 +09:00
|
|
|
define_method(:"download_#{attachment_name}!") do |url = nil|
|
2020-06-30 00:59:04 +09:00
|
|
|
url ||= self[attribute_name]
|
2017-05-18 22:43:10 +09:00
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
return if url.blank?
|
|
|
|
|
2017-07-03 18:03:34 +09:00
|
|
|
begin
|
|
|
|
parsed_url = Addressable::URI.parse(url).normalize
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
return
|
|
|
|
end
|
2017-05-18 22:43:10 +09:00
|
|
|
|
2020-06-29 20:56:55 +09:00
|
|
|
return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.blank?
|
2017-05-18 22:43:10 +09:00
|
|
|
|
|
|
|
begin
|
2018-03-24 20:49:54 +09:00
|
|
|
Request.new(:get, url).perform do |response|
|
2019-09-10 22:29:12 +09:00
|
|
|
raise Mastodon::UnexpectedResponseError, response unless (200...300).cover?(response.code)
|
2018-03-24 20:49:54 +09:00
|
|
|
|
2023-12-18 19:26:09 +09:00
|
|
|
public_send(:"#{attachment_name}=", ResponseWithLimit.new(response, limit))
|
2018-03-24 20:49:54 +09:00
|
|
|
end
|
2024-10-08 23:59:51 +09:00
|
|
|
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS => e
|
2023-02-07 11:44:36 +09:00
|
|
|
Rails.logger.debug { "Error fetching remote #{attachment_name}: #{e}" }
|
2023-12-18 19:26:09 +09:00
|
|
|
public_send(:"#{attachment_name}=", nil) if public_send(:"#{attachment_name}_file_name").present?
|
2019-09-10 22:29:12 +09:00
|
|
|
raise e unless suppress_errors
|
2020-07-20 05:28:27 +09:00
|
|
|
rescue Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError, Paperclip::Error, Mastodon::DimensionsValidationError, Mastodon::StreamValidationError => e
|
2023-02-07 11:44:36 +09:00
|
|
|
Rails.logger.debug { "Error fetching remote #{attachment_name}: #{e}" }
|
2023-12-18 19:26:09 +09:00
|
|
|
public_send(:"#{attachment_name}=", nil) if public_send(:"#{attachment_name}_file_name").present?
|
2017-05-18 22:43:10 +09:00
|
|
|
end
|
2020-07-01 06:58:02 +09:00
|
|
|
|
|
|
|
nil
|
2017-05-18 22:43:10 +09:00
|
|
|
end
|
2017-07-12 00:25:49 +09:00
|
|
|
|
2023-12-18 19:26:09 +09:00
|
|
|
define_method(:"#{attribute_name}=") do |url|
|
|
|
|
return if self[attribute_name] == url && public_send(:"#{attachment_name}_file_name").present?
|
2017-07-12 00:25:49 +09:00
|
|
|
|
2020-06-30 00:59:04 +09:00
|
|
|
self[attribute_name] = url if has_attribute?(attribute_name)
|
2017-07-12 00:25:49 +09:00
|
|
|
|
2023-12-18 19:26:09 +09:00
|
|
|
public_send(:"download_#{attachment_name}!", url) if download_on_assign
|
2017-07-12 00:25:49 +09:00
|
|
|
end
|
2020-06-29 20:56:55 +09:00
|
|
|
|
2023-12-29 19:23:30 +09:00
|
|
|
alias_method(:"reset_#{attachment_name}!", :"download_#{attachment_name}!")
|
2017-05-18 22:43:10 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|