Merge pull request #304 from Freika/fix/import-watcher-user-directory

Fix/import watcher user directory
This commit is contained in:
Evgenii Burmakin 2024-10-05 14:00:27 +03:00 committed by GitHub
commit a47a149993
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 71 additions and 21 deletions

View file

@ -1 +1 @@
0.15.1
0.15.3

View file

@ -5,6 +5,38 @@ 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
To expose the watcher functionality to the user, a new directory `/tmp/imports/watched/` was created. Add new volume to the `docker-compose.yml` file to expose this directory to the host machine.
```diff
...
dawarich_app:
image: freikin/dawarich:latest
container_name: dawarich_app
volumes:
- gem_cache:/usr/local/bundle/gems
- public:/var/app/public
+ - watched:/var/app/tmp/watched
...
dawarich_sidekiq:
image: freikin/dawarich:latest
container_name: dawarich_sidekiq
volumes:
- gem_cache:/usr/local/bundle/gems
- public:/var/app/public
+ - watched:/var/app/tmp/watched
...
```
### 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

View file

@ -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

View file

@ -27,6 +27,7 @@ services:
volumes:
- gem_cache:/usr/local/bundle/gems
- public:/var/app/public
- watched:/var/app/tmp/watched
networks:
- dawarich
ports:
@ -68,6 +69,7 @@ services:
volumes:
- gem_cache:/usr/local/bundle/gems
- public:/var/app/public
- watched:/var/app/tmp/watched
networks:
- dawarich
stdin_open: true

View file

@ -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.