2024-12-03 08:44:24 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Photos::Thumbnail
|
2025-02-10 14:37:20 -05:00
|
|
|
SUPPORTED_SOURCES = %w[immich photoprism].freeze
|
|
|
|
|
|
2024-12-03 08:44:24 -05:00
|
|
|
def initialize(user, source, id)
|
|
|
|
|
@user = user
|
|
|
|
|
@source = source
|
|
|
|
|
@id = id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
2025-08-23 12:46:00 -04:00
|
|
|
raise ArgumentError, 'Photo source cannot be nil' if source.nil?
|
2025-08-23 13:09:18 -04:00
|
|
|
unsupported_source_error unless SUPPORTED_SOURCES.include?(source)
|
2025-02-10 14:37:20 -05:00
|
|
|
|
2024-12-04 07:50:41 -05:00
|
|
|
HTTParty.get(request_url, headers: headers)
|
2024-12-03 08:44:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
attr_reader :user, :source, :id
|
|
|
|
|
|
|
|
|
|
def source_url
|
2025-02-10 14:37:20 -05:00
|
|
|
user.safe_settings.public_send("#{source}_url")
|
2024-12-03 08:44:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def source_api_key
|
2025-02-10 14:37:20 -05:00
|
|
|
user.safe_settings.public_send("#{source}_api_key")
|
2024-12-03 08:44:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def source_path
|
|
|
|
|
case source
|
|
|
|
|
when 'immich'
|
|
|
|
|
"/api/assets/#{id}/thumbnail?size=preview"
|
|
|
|
|
when 'photoprism'
|
|
|
|
|
preview_token = Rails.cache.read("#{Photoprism::CachePreviewToken::TOKEN_CACHE_KEY}_#{user.id}")
|
|
|
|
|
"/api/v1/t/#{id}/#{preview_token}/tile_500"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-04 07:50:41 -05:00
|
|
|
def request_url
|
|
|
|
|
"#{source_url}#{source_path}"
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-03 08:44:24 -05:00
|
|
|
def headers
|
|
|
|
|
request_headers = {
|
|
|
|
|
'accept' => 'application/octet-stream'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request_headers['X-Api-Key'] = source_api_key if source == 'immich'
|
|
|
|
|
|
|
|
|
|
request_headers
|
|
|
|
|
end
|
2025-02-10 14:37:20 -05:00
|
|
|
|
2025-08-23 15:57:25 -04:00
|
|
|
def unsupported_source_error
|
2025-02-10 14:37:20 -05:00
|
|
|
raise ArgumentError, "Unsupported source: #{source}"
|
|
|
|
|
end
|
2024-12-03 08:44:24 -05:00
|
|
|
end
|