dawarich/app/serializers/api/photo_serializer.rb

76 lines
1.4 KiB
Ruby
Raw Normal View History

2024-12-02 12:21:12 -05:00
# frozen_string_literal: true
class Api::PhotoSerializer
2024-12-03 07:50:05 -05:00
def initialize(photo, source)
2024-12-04 07:17:15 -05:00
@photo = photo.with_indifferent_access
2024-12-03 07:50:05 -05:00
@source = source
2024-12-02 12:21:12 -05:00
end
def call
{
id: id,
latitude: latitude,
longitude: longitude,
localDateTime: local_date_time,
originalFileName: original_file_name,
city: city,
state: state,
country: country,
2024-12-03 07:50:05 -05:00
type: type,
2024-12-10 12:49:37 -05:00
orientation: orientation,
2024-12-03 07:50:05 -05:00
source: source
2024-12-02 12:21:12 -05:00
}
end
private
2024-12-03 07:50:05 -05:00
attr_reader :photo, :source
2024-12-02 12:21:12 -05:00
def id
photo['id'] || photo['Hash']
2024-12-02 12:21:12 -05:00
end
def latitude
photo.dig('exifInfo', 'latitude') || photo['Lat']
end
def longitude
photo.dig('exifInfo', 'longitude') || photo['Lng']
end
def local_date_time
photo['localDateTime'] || photo['TakenAtLocal']
end
def original_file_name
photo['originalFileName'] || photo['OriginalName']
end
def city
photo.dig('exifInfo', 'city') || photo['PlaceCity']
end
def state
photo.dig('exifInfo', 'state') || photo['PlaceState']
end
def country
photo.dig('exifInfo', 'country') || photo['PlaceCountry']
end
def type
(photo['type'] || photo['Type']).downcase
end
2024-12-10 12:49:37 -05:00
def orientation
case source
when 'immich'
photo.dig('exifInfo', 'orientation') == '6' ? 'portrait' : 'landscape'
when 'photoprism'
photo['Portrait'] ? 'portrait' : 'landscape'
else
'landscape' # default orientation for nil or unknown source
2024-12-10 12:49:37 -05:00
end
end
2024-12-02 12:21:12 -05:00
end