mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 17:51:39 -05:00
71 lines
1.7 KiB
Ruby
71 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ImportsController < ApplicationController
|
|
before_action :authenticate_user!
|
|
before_action :authenticate_active_user!, only: %i[new create]
|
|
before_action :set_import, only: %i[show destroy]
|
|
|
|
def index
|
|
@imports =
|
|
current_user
|
|
.imports
|
|
.select(:id, :name, :source, :created_at, :points_count)
|
|
.order(created_at: :desc)
|
|
.page(params[:page])
|
|
end
|
|
|
|
def show; end
|
|
|
|
def new
|
|
@import = Import.new
|
|
end
|
|
|
|
def create
|
|
files = import_params[:files].reject(&:blank?)
|
|
|
|
import_ids = files.map do |file|
|
|
import = current_user.imports.create(
|
|
name: file.original_filename,
|
|
source: params[:import][:source]
|
|
)
|
|
|
|
file = File.read(file)
|
|
|
|
raw_data =
|
|
case params[:import][:source]
|
|
when 'gpx' then Hash.from_xml(file)
|
|
when 'owntracks' then OwnTracks::RecParser.new(file).call
|
|
else JSON.parse(file)
|
|
end
|
|
|
|
import.update(raw_data:)
|
|
import.id
|
|
end
|
|
|
|
import_ids.each { ImportJob.perform_later(current_user.id, _1) }
|
|
|
|
redirect_to imports_url, notice: "#{files.size} files are queued to be imported in background", status: :see_other
|
|
rescue StandardError => e
|
|
Import.where(user: current_user, name: files.map(&:original_filename)).destroy_all
|
|
|
|
flash.now[:error] = e.message
|
|
|
|
redirect_to new_import_path, notice: e.message, status: :unprocessable_entity
|
|
end
|
|
|
|
def destroy
|
|
Imports::Destroy.new(current_user, @import).call
|
|
|
|
redirect_to imports_url, notice: 'Import was successfully destroyed.', status: :see_other
|
|
end
|
|
|
|
private
|
|
|
|
def set_import
|
|
@import = Import.find(params[:id])
|
|
end
|
|
|
|
def import_params
|
|
params.require(:import).permit(:source, files: [])
|
|
end
|
|
end
|