diff --git a/.app_version b/.app_version index e815b861..1985d914 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.15.1 +0.15.3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3512a708..131fe454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.15.3 - 2024-10-05 + +### Changed + +- Watcher now looks into `/tmp/imports/watched/USER@EMAIL.TLD` directory instead of `/tmp/imports/watched/` to allow using arbitrary file names for imports + # 0.15.1 - 2024-10-04 ### Added diff --git a/app/services/imports/watcher.rb b/app/services/imports/watcher.rb index 8c153bb1..43f3653b 100644 --- a/app/services/imports/watcher.rb +++ b/app/services/imports/watcher.rb @@ -6,33 +6,54 @@ class Imports::Watcher WATCHED_DIR_PATH = Rails.root.join('tmp/imports/watched') def call - %w[*.gpx *.json].each do |pattern| - Dir[WATCHED_DIR_PATH.join(pattern)].each do |file_path| - # valid file_name example: "email@dawarich.app_2024-01-01-2024-01-31.json" - file_name = File.basename(file_path) + user_directories.each do |user_email| + user = User.find_by(email: user_email) + next unless user - user = find_user(file_name) - next unless user + user_directory_path = File.join(WATCHED_DIR_PATH, user_email) + file_names = file_names(user_directory_path) - import = find_or_initialize_import(user, file_name) - - next if import.persisted? - - import_id = set_import_attributes(import, file_path, file_name) - - ImportJob.perform_later(user.id, import_id) + file_names.each do |file_name| + process_file(user, user_directory_path, file_name) end end end private + def user_directories + Dir.entries(WATCHED_DIR_PATH).select do |entry| + path = File.join(WATCHED_DIR_PATH, entry) + File.directory?(path) && !['.', '..'].include?(entry) + end + end + def find_user(file_name) email = file_name.split('_').first User.find_by(email:) end + def file_names(directory_path) + Dir.entries(directory_path).select do |file| + ['.gpx', '.json'].include?(File.extname(file)) + end + end + + def process_file(user, directory_path, file_name) + file_path = File.join(directory_path, file_name) + import = Import.find_or_initialize_by(user:, name: file_name) + + return if import.persisted? + + import.source = source(file_name) + import.raw_data = raw_data(file_path, import.source) + + import.save! + + ImportJob.perform_later(user.id, import.id) + end + def find_or_initialize_import(user, file_name) import_name = file_name.split('_')[1..].join('_') @@ -61,6 +82,6 @@ class Imports::Watcher def raw_data(file_path, source) file = File.read(file_path) - source == :gpx ? Hash.from_xml(file) : JSON.parse(file) + source.to_sym == :gpx ? Hash.from_xml(file) : JSON.parse(file) end end diff --git a/spec/fixtures/files/watched/user@domain.com_export_same_points.json b/spec/fixtures/files/watched/user@domain.com/export_same_points.json similarity index 100% rename from spec/fixtures/files/watched/user@domain.com_export_same_points.json rename to spec/fixtures/files/watched/user@domain.com/export_same_points.json diff --git a/spec/fixtures/files/watched/user@domain.com_gpx_track_single_segment.gpx b/spec/fixtures/files/watched/user@domain.com/gpx_track_single_segment.gpx similarity index 99% rename from spec/fixtures/files/watched/user@domain.com_gpx_track_single_segment.gpx rename to spec/fixtures/files/watched/user@domain.com/gpx_track_single_segment.gpx index c7447af0..3176fb4f 100644 --- a/spec/fixtures/files/watched/user@domain.com_gpx_track_single_segment.gpx +++ b/spec/fixtures/files/watched/user@domain.com/gpx_track_single_segment.gpx @@ -1236,4 +1236,4 @@ - + \ No newline at end of file diff --git a/tmp/imports/watched/put-your-files-here.txt b/tmp/imports/watched/put-your-files-here.txt deleted file mode 100644 index 4bd4ddd4..00000000 --- a/tmp/imports/watched/put-your-files-here.txt +++ /dev/null @@ -1,5 +0,0 @@ -The /public/imporst/watched/ directory is watched by Dawarich. Any files you put in this directory will be imported into the database. The name of the file must start with an email of the user you want to import the file for. The email must be followed by an underscore symbol (_) and the name of the file. - -For example, if you want to import a file for the user with the email address "email@dawarich.app", you would name the file "email@dawarich.app_2024-05-01_2024-05-31.gpx". The file will be imported into the database and the user will receive a notification in the app. - -Both GeoJSON and GPX files are supported.