mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Use attached file to import geojson and phone takeout
This commit is contained in:
parent
f3b98ac83d
commit
5758f9a923
5 changed files with 34 additions and 38 deletions
|
|
@ -2,34 +2,28 @@
|
|||
|
||||
class Geojson::ImportParser
|
||||
include Imports::Broadcaster
|
||||
include PointValidation
|
||||
|
||||
attr_reader :import, :json, :user_id
|
||||
attr_reader :import, :user_id
|
||||
|
||||
def initialize(import, user_id)
|
||||
@import = import
|
||||
@json = import.raw_data
|
||||
@user_id = user_id
|
||||
end
|
||||
|
||||
def call
|
||||
data = Geojson::Params.new(json).call
|
||||
import.file.download do |file|
|
||||
json = Oj.load(file)
|
||||
|
||||
data.each.with_index(1) do |point, index|
|
||||
next if point_exists?(point, user_id)
|
||||
data = Geojson::Params.new(json).call
|
||||
|
||||
Point.create!(point.merge(user_id:, import_id: import.id))
|
||||
data.each.with_index(1) do |point, index|
|
||||
next if point_exists?(point, user_id)
|
||||
|
||||
broadcast_import_progress(import, index)
|
||||
Point.create!(point.merge(user_id:, import_id: import.id))
|
||||
|
||||
broadcast_import_progress(import, index)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def point_exists?(params, user_id)
|
||||
Point.exists?(
|
||||
lonlat: params[:lonlat],
|
||||
timestamp: params[:timestamp],
|
||||
user_id:
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -48,13 +48,15 @@ class GoogleMaps::PhoneTakeoutParser
|
|||
raw_signals = []
|
||||
raw_array = []
|
||||
|
||||
if import.raw_data.is_a?(Array)
|
||||
raw_array = parse_raw_array(import.raw_data)
|
||||
else
|
||||
if import.raw_data['semanticSegments']
|
||||
semantic_segments = parse_semantic_segments(import.raw_data['semanticSegments'])
|
||||
import.file.download do |file|
|
||||
json = Oj.load(file)
|
||||
|
||||
if json.is_a?(Array)
|
||||
raw_array = parse_raw_array(json)
|
||||
else
|
||||
semantic_segments = parse_semantic_segments(json['semanticSegments']) if json['semanticSegments']
|
||||
raw_signals = parse_raw_signals(json['rawSignals']) if json['rawSignals']
|
||||
end
|
||||
raw_signals = parse_raw_signals(import.raw_data['rawSignals']) if import.raw_data['rawSignals']
|
||||
end
|
||||
|
||||
semantic_segments + raw_signals + raw_array
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
class Photos::ImportParser
|
||||
include Imports::Broadcaster
|
||||
|
||||
include PointValidation
|
||||
attr_reader :import, :json, :user_id
|
||||
|
||||
def initialize(import, user_id)
|
||||
|
|
@ -29,12 +29,4 @@ class Photos::ImportParser
|
|||
|
||||
broadcast_import_progress(import, index)
|
||||
end
|
||||
|
||||
def point_exists?(point, timestamp)
|
||||
Point.exists?(
|
||||
lonlat: "POINT(#{point['longitude']} #{point['latitude']})",
|
||||
timestamp:,
|
||||
user_id:
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -12,8 +12,12 @@ RSpec.describe Geojson::ImportParser do
|
|||
|
||||
context 'when file content is an object' do
|
||||
let(:file_path) { Rails.root.join('spec/fixtures/files/geojson/export.json') }
|
||||
let(:raw_data) { JSON.parse(File.read(file_path)) }
|
||||
let(:import) { create(:import, user:, name: 'geojson.json', raw_data:) }
|
||||
let(:file) { Rack::Test::UploadedFile.new(file_path, 'application/json') }
|
||||
let(:import) { create(:import, user:, name: 'geojson.json', file:) }
|
||||
|
||||
before do
|
||||
import.file.attach(io: File.open(file_path), filename: 'geojson.json', content_type: 'application/json')
|
||||
end
|
||||
|
||||
it 'creates new points' do
|
||||
expect { service }.to change { Point.count }.by(10)
|
||||
|
|
|
|||
|
|
@ -8,11 +8,15 @@ RSpec.describe GoogleMaps::PhoneTakeoutParser do
|
|||
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before do
|
||||
import.file.attach(io: File.open(file_path), filename: 'phone_takeout.json', content_type: 'application/json')
|
||||
end
|
||||
|
||||
context 'when file content is an object' do
|
||||
# This file contains 3 duplicates
|
||||
let(:file_path) { Rails.root.join('spec/fixtures/files/google/phone-takeout.json') }
|
||||
let(:raw_data) { JSON.parse(File.read(file_path)) }
|
||||
let(:import) { create(:import, user:, name: 'phone_takeout.json', raw_data:) }
|
||||
let(:file) { Rack::Test::UploadedFile.new(file_path, 'application/json') }
|
||||
let(:import) { create(:import, user:, name: 'phone_takeout.json', file:) }
|
||||
|
||||
context 'when file exists' do
|
||||
it 'creates points' do
|
||||
|
|
@ -24,8 +28,8 @@ RSpec.describe GoogleMaps::PhoneTakeoutParser do
|
|||
context 'when file content is an array' do
|
||||
# This file contains 4 duplicates
|
||||
let(:file_path) { Rails.root.join('spec/fixtures/files/google/location-history.json') }
|
||||
let(:raw_data) { JSON.parse(File.read(file_path)) }
|
||||
let(:import) { create(:import, user:, name: 'phone_takeout.json', raw_data:) }
|
||||
let(:file) { Rack::Test::UploadedFile.new(file_path, 'application/json') }
|
||||
let(:import) { create(:import, user:, name: 'phone_takeout.json', file:) }
|
||||
|
||||
context 'when file exists' do
|
||||
it 'creates points' do
|
||||
|
|
|
|||
Loading…
Reference in a new issue