dawarich/app/services/photos/importer.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2025-04-23 17:36:16 -04:00
class Photos::Importer
include Imports::Broadcaster
include PointValidation
2025-08-22 13:10:40 -04:00
attr_reader :import, :user_id, :file_path
2025-08-22 13:10:40 -04:00
def initialize(import, user_id, file_path = nil)
@import = import
@user_id = user_id
2025-08-22 13:10:40 -04:00
@file_path = file_path
end
def call
2025-08-22 13:10:40 -04:00
json = if file_path && File.exist?(file_path)
Oj.load_file(file_path, mode: :compat)
else
file_content = Imports::SecureFileDownloader.new(import.file).download_with_verification
Oj.load(file_content, mode: :compat)
end
2025-04-23 17:27:55 -04:00
json.each.with_index(1) { |point, index| create_point(point, index) }
end
2024-11-07 07:07:54 -05:00
def create_point(point, index)
return 0 unless valid?(point)
return 0 if point_exists?(point, point['timestamp'])
Point.create(
lonlat: point['lonlat'],
longitude: point['longitude'],
latitude: point['latitude'],
timestamp: point['timestamp'].to_i,
raw_data: point,
import_id: import.id,
user_id:
)
broadcast_import_progress(import, index)
end
def valid?(point)
point['latitude'].present? &&
point['longitude'].present? &&
point['timestamp'].present?
end
end