dawarich/app/controllers/imports_controller.rb

132 lines
3.6 KiB
Ruby
Raw Normal View History

2024-05-18 09:00:44 -04:00
# frozen_string_literal: true
2024-03-15 18:27:31 -04:00
class ImportsController < ApplicationController
include ActiveStorage::SetCurrent
2024-03-15 18:27:31 -04:00
before_action :authenticate_user!
2025-04-18 13:48:02 -04:00
before_action :set_import, only: %i[show edit update destroy]
before_action :authorize_import, only: %i[show edit update destroy]
2025-05-16 12:51:48 -04:00
before_action :validate_points_limit, only: %i[new create]
after_action :verify_authorized, except: %i[index]
after_action :verify_policy_scoped, only: %i[index]
2024-03-15 18:27:31 -04:00
def index
@imports = policy_scope(Import)
.select(:id, :name, :source, :created_at, :processed, :status)
.order(created_at: :desc)
.page(params[:page])
2024-03-15 18:27:31 -04:00
end
2024-05-18 09:00:44 -04:00
def show; end
2024-03-15 18:27:31 -04:00
2025-04-18 13:48:02 -04:00
def edit; end
2024-03-15 18:27:31 -04:00
def new
@import = Import.new
authorize @import
2024-03-15 18:27:31 -04:00
end
2025-04-18 13:48:02 -04:00
def update
@import.update(import_params)
redirect_to imports_url, notice: 'Import was successfully updated.', status: :see_other
end
2024-03-15 18:27:31 -04:00
def create
2025-07-29 14:14:24 -04:00
@import = Import.new
authorize @import
2025-05-03 15:46:30 -04:00
files_params = params.dig(:import, :files)
raw_files = Array(files_params).reject(&:blank?)
if raw_files.empty?
redirect_to new_import_path, alert: 'No files were selected for upload', status: :unprocessable_content and return
end
2025-05-03 15:46:30 -04:00
created_imports = []
2024-03-15 18:27:31 -04:00
2025-05-03 15:46:30 -04:00
raw_files.each do |item|
next if item.is_a?(ActionDispatch::Http::UploadedFile)
2024-05-31 17:18:57 -04:00
2025-05-03 15:46:30 -04:00
import = create_import_from_signed_id(item)
created_imports << import if import.present?
end
2025-05-03 15:46:30 -04:00
if created_imports.any?
redirect_to imports_url,
2025-05-03 15:46:30 -04:00
notice: "#{created_imports.size} files are queued to be imported in background",
2025-08-22 13:10:40 -04:00
status: :see_other and return
else
redirect_to new_import_path,
alert: 'No valid file references were found. Please upload files using the file selector.',
status: :unprocessable_content and return
end
2024-03-15 18:27:31 -04:00
rescue StandardError => e
2025-05-03 15:46:30 -04:00
if created_imports.present?
import_ids = created_imports.map(&:id).compact
Import.where(id: import_ids).destroy_all if import_ids.any?
end
2024-03-24 13:55:35 -04:00
Rails.logger.error "Import error: #{e.message}"
Rails.logger.error e.backtrace.join("\n")
2025-05-03 14:36:09 -04:00
ExceptionReporter.call(e)
redirect_to new_import_path, alert: e.message, status: :unprocessable_content
2024-03-15 18:27:31 -04:00
end
def destroy
2025-02-15 12:49:30 -05:00
Imports::Destroy.new(current_user, @import).call
2024-05-18 09:00:44 -04:00
redirect_to imports_url, notice: 'Import was successfully destroyed.', status: :see_other
2024-03-15 18:27:31 -04:00
end
private
def set_import
@import = Import.find(params[:id])
end
def authorize_import
authorize @import
end
2024-03-15 18:27:31 -04:00
def import_params
2025-08-22 13:10:40 -04:00
params.require(:import).permit(:name, files: [])
2024-03-15 18:27:31 -04:00
end
def create_import_from_signed_id(signed_id)
Rails.logger.debug "Creating import from signed ID: #{signed_id[0..20]}..."
blob = ActiveStorage::Blob.find_signed(signed_id)
2025-08-23 10:07:15 -04:00
import_name = generate_unique_import_name(blob.filename.to_s)
import = current_user.imports.build(name: import_name)
import.file.attach(blob)
2025-08-22 13:10:40 -04:00
import.save!
import
end
2025-05-16 12:51:48 -04:00
2025-08-23 10:07:15 -04:00
def generate_unique_import_name(original_name)
return original_name unless current_user.imports.exists?(name: original_name)
2025-08-22 14:13:10 -04:00
2025-08-23 10:07:15 -04:00
# Extract filename and extension
basename = File.basename(original_name, File.extname(original_name))
extension = File.extname(original_name)
2025-08-23 10:07:15 -04:00
# Add current datetime
timestamp = Time.current.strftime('%Y%m%d_%H%M%S')
"#{basename}_#{timestamp}#{extension}"
2025-08-22 13:10:40 -04:00
end
2025-05-16 12:51:48 -04:00
def validate_points_limit
limit_exceeded = PointsLimitExceeded.new(current_user).call
redirect_to imports_path, alert: 'Points limit exceeded', status: :unprocessable_content if limit_exceeded
2025-05-16 12:51:48 -04:00
end
2024-03-15 18:27:31 -04:00
end