2024-08-21 12:40:54 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2025-04-23 17:36:16 -04:00
|
|
|
class Photos::Importer
|
2024-11-04 07:14:06 -05:00
|
|
|
include Imports::Broadcaster
|
2025-03-23 14:13:59 -04:00
|
|
|
include PointValidation
|
2025-08-22 13:10:40 -04:00
|
|
|
attr_reader :import, :user_id, :file_path
|
2024-08-21 12:40:54 -04:00
|
|
|
|
2025-08-22 13:10:40 -04:00
|
|
|
def initialize(import, user_id, file_path = nil)
|
2024-08-21 12:40:54 -04:00
|
|
|
@import = import
|
|
|
|
|
@user_id = user_id
|
2025-08-22 13:10:40 -04:00
|
|
|
@file_path = file_path
|
2024-08-21 12:40:54 -04:00
|
|
|
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-13 17:25:26 -04:00
|
|
|
|
2025-04-23 17:27:55 -04:00
|
|
|
json.each.with_index(1) { |point, index| create_point(point, index) }
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
|
|
|
|
|
2024-11-07 07:07:54 -05:00
|
|
|
def create_point(point, index)
|
2025-06-08 10:41:01 -04:00
|
|
|
return 0 unless valid?(point)
|
2024-08-21 12:40:54 -04:00
|
|
|
return 0 if point_exists?(point, point['timestamp'])
|
|
|
|
|
|
|
|
|
|
Point.create(
|
2025-06-08 10:41:01 -04:00
|
|
|
lonlat: point['lonlat'],
|
|
|
|
|
longitude: point['longitude'],
|
|
|
|
|
latitude: point['latitude'],
|
|
|
|
|
timestamp: point['timestamp'].to_i,
|
|
|
|
|
raw_data: point,
|
|
|
|
|
import_id: import.id,
|
2024-08-21 12:40:54 -04:00
|
|
|
user_id:
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-04 07:14:26 -05:00
|
|
|
broadcast_import_progress(import, index)
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|
2025-06-08 10:41:01 -04:00
|
|
|
|
|
|
|
|
def valid?(point)
|
|
|
|
|
point['latitude'].present? &&
|
|
|
|
|
point['longitude'].present? &&
|
|
|
|
|
point['timestamp'].present?
|
|
|
|
|
end
|
2024-08-21 12:40:54 -04:00
|
|
|
end
|