2024-08-21 12:40:54 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Immich::ImportGeodata
|
2024-11-26 08:46:26 -05:00
|
|
|
attr_reader :user, :start_date, :end_date
|
2024-08-21 12:40:54 -04:00
|
|
|
|
2024-11-26 14:18:08 -05:00
|
|
|
def initialize(user, start_date: '1970-01-01', end_date: nil)
|
2024-08-21 12:40:54 -04:00
|
|
|
@user = user
|
2024-11-26 08:46:26 -05:00
|
|
|
@start_date = start_date
|
|
|
|
|
@end_date = end_date
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
2024-10-29 06:09:58 -04:00
|
|
|
immich_data = retrieve_immich_data
|
|
|
|
|
|
|
|
|
|
log_no_data and return if immich_data.empty?
|
|
|
|
|
|
2024-08-21 12:40:54 -04:00
|
|
|
immich_data_json = parse_immich_data(immich_data)
|
|
|
|
|
file_name = file_name(immich_data_json)
|
|
|
|
|
import = user.imports.find_or_initialize_by(name: file_name, source: :immich_api)
|
|
|
|
|
|
2024-09-02 12:28:17 -04:00
|
|
|
create_import_failed_notification(import.name) and return unless import.new_record?
|
2024-08-21 12:40:54 -04:00
|
|
|
|
|
|
|
|
import.raw_data = immich_data_json
|
|
|
|
|
import.save!
|
2024-11-26 08:46:26 -05:00
|
|
|
|
2025-03-23 13:37:10 -04:00
|
|
|
Import::ProcessJob.perform_later(import.id)
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def retrieve_immich_data
|
2024-11-26 08:46:26 -05:00
|
|
|
Immich::RequestPhotos.new(user, start_date:, end_date:).call
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse_immich_data(immich_data)
|
2024-11-18 09:41:43 -05:00
|
|
|
geodata = immich_data.map do |asset|
|
2024-09-08 10:52:35 -04:00
|
|
|
next unless valid?(asset)
|
2024-08-21 12:40:54 -04:00
|
|
|
|
2024-09-08 10:52:35 -04:00
|
|
|
extract_geodata(asset)
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
2024-09-08 10:52:35 -04:00
|
|
|
geodata.compact.sort_by { |data| data[:timestamp] }
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
2024-09-12 16:41:26 -04:00
|
|
|
def valid?(asset)
|
|
|
|
|
asset.dig('exifInfo', 'latitude') &&
|
|
|
|
|
asset.dig('exifInfo', 'longitude') &&
|
|
|
|
|
asset.dig('exifInfo', 'dateTimeOriginal')
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def extract_geodata(asset)
|
|
|
|
|
{
|
|
|
|
|
latitude: asset.dig('exifInfo', 'latitude'),
|
|
|
|
|
longitude: asset.dig('exifInfo', 'longitude'),
|
|
|
|
|
timestamp: Time.zone.parse(asset.dig('exifInfo', 'dateTimeOriginal')).to_i
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def log_no_data
|
2024-12-03 09:40:21 -05:00
|
|
|
Rails.logger.info 'No geodata found for Immich'
|
2024-10-29 06:09:58 -04:00
|
|
|
end
|
|
|
|
|
|
2024-09-02 12:28:17 -04:00
|
|
|
def create_import_failed_notification(import_name)
|
2024-08-21 12:40:54 -04:00
|
|
|
Notifications::Create.new(
|
|
|
|
|
user:,
|
|
|
|
|
kind: :info,
|
|
|
|
|
title: 'Import was not created',
|
2024-09-02 12:28:17 -04:00
|
|
|
content: "Import with the same name (#{import_name}) already exists. If you want to proceed, delete the existing import and try again."
|
2024-08-21 12:40:54 -04:00
|
|
|
).call
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def file_name(immich_data_json)
|
|
|
|
|
from = Time.zone.at(immich_data_json.first[:timestamp]).to_date
|
|
|
|
|
to = Time.zone.at(immich_data_json.last[:timestamp]).to_date
|
|
|
|
|
|
|
|
|
|
"immich-geodata-#{user.email}-from-#{from}-to-#{to}.json"
|
|
|
|
|
end
|
|
|
|
|
end
|