diff --git a/app/services/imports/create.rb b/app/services/imports/create.rb index 4ce3e7c2..7c34cc1f 100644 --- a/app/services/imports/create.rb +++ b/app/services/imports/create.rb @@ -24,12 +24,12 @@ class Imports::Create def parser(source) # Bad classes naming by the way, they are not parsers, they are point creators case source - when 'google_semantic_history' then GoogleMaps::SemanticHistoryParser - when 'google_phone_takeout' then GoogleMaps::PhoneTakeoutParser - when 'owntracks' then OwnTracks::ExportParser - when 'gpx' then Gpx::TrackParser - when 'immich_api' then Immich::ImportParser - when 'geojson' then Geojson::ImportParser + when 'google_semantic_history' then GoogleMaps::SemanticHistoryParser + when 'google_phone_takeout' then GoogleMaps::PhoneTakeoutParser + when 'owntracks' then OwnTracks::ExportParser + when 'gpx' then Gpx::TrackParser + when 'geojson' then Geojson::ImportParser + when 'immich_api', 'photoprism_api' then Photos::ImportParser end end diff --git a/app/services/photoprism/import_geodata.rb b/app/services/photoprism/import_geodata.rb index a24f2542..182681e6 100644 --- a/app/services/photoprism/import_geodata.rb +++ b/app/services/photoprism/import_geodata.rb @@ -11,23 +11,29 @@ class Photoprism::ImportGeodata def call photoprism_data = retrieve_photoprism_data + return log_no_data if photoprism_data.empty? - log_no_data and return if photoprism_data.empty? - - photoprism_data_json = parse_photoprism_data(photoprism_data) - file_name = file_name(photoprism_data_json) - import = user.imports.find_or_initialize_by(name: file_name, source: :photoprism_api) - - create_import_failed_notification(import.name) and return unless import.new_record? - - import.raw_data = photoprism_data_json - import.save! - - ImportJob.perform_later(user.id, import.id) + json_data = parse_photoprism_data(photoprism_data) + create_and_process_import(json_data) end private + def create_and_process_import(json_data) + import = find_or_create_import(json_data) + return create_import_failed_notification(import.name) unless import.new_record? + + import.update!(raw_data: json_data) + ImportJob.perform_later(user.id, import.id) + end + + def find_or_create_import(json_data) + user.imports.find_or_initialize_by( + name: file_name(json_data), + source: :photoprism_api + ) + end + def retrieve_photoprism_data Photoprism::RequestPhotos.new(user, start_date:, end_date:).call end @@ -52,9 +58,9 @@ class Photoprism::ImportGeodata def extract_geodata(asset) { - latitude: asset.dig('exifInfo', 'latitude'), - longitude: asset.dig('exifInfo', 'longitude'), - timestamp: Time.zone.parse(asset.dig('exifInfo', 'dateTimeOriginal')).to_i + latitude: asset['Lat'], + longitude: asset['Lng'], + timestamp: Time.zone.parse(asset['TakenAt']).to_i } end @@ -72,8 +78,8 @@ class Photoprism::ImportGeodata end def file_name(photoprism_data_json) - from = Time.zone.at(photoprism_data_json.first[:timestamp]).to_date - to = Time.zone.at(photoprism_data_json.last[:timestamp]).to_date + from = Time.zone.at(photoprism_data_json.first[:timestamp]).to_date + to = Time.zone.at(photoprism_data_json.last[:timestamp]).to_date "photoprism-geodata-#{user.email}-from-#{from}-to-#{to}.json" end diff --git a/app/services/immich/import_parser.rb b/app/services/photos/import_parser.rb similarity index 97% rename from app/services/immich/import_parser.rb rename to app/services/photos/import_parser.rb index b0a2a38c..97b9c9d4 100644 --- a/app/services/immich/import_parser.rb +++ b/app/services/photos/import_parser.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Immich::ImportParser +class Photos::ImportParser include Imports::Broadcaster attr_reader :import, :json, :user_id diff --git a/spec/services/immich/import_parser_spec.rb b/spec/services/photos/import_parser_spec.rb similarity index 97% rename from spec/services/immich/import_parser_spec.rb rename to spec/services/photos/import_parser_spec.rb index cefa4dc6..33460398 100644 --- a/spec/services/immich/import_parser_spec.rb +++ b/spec/services/photos/import_parser_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe Immich::ImportParser do +RSpec.describe Photos::ImportParser do describe '#call' do subject(:service) { described_class.new(import, user.id).call }