Files
dawarich/app/controllers/imports_controller.rb
Eugene Burmakin 7eb3103645 Rename a method
2025-08-22 20:40:06 +02:00

134 lines
3.6 KiB
Ruby

# frozen_string_literal: true
class ImportsController < ApplicationController
include ActiveStorage::SetCurrent
before_action :authenticate_user!
before_action :set_import, only: %i[show edit update destroy]
before_action :authorize_import, only: %i[show edit update destroy]
before_action :validate_points_limit, only: %i[new create]
after_action :verify_authorized, except: %i[index]
after_action :verify_policy_scoped, only: %i[index]
def index
@imports = policy_scope(Import)
.select(:id, :name, :source, :created_at, :processed, :status)
.order(created_at: :desc)
.page(params[:page])
end
def show; end
def edit; end
def new
@import = Import.new
authorize @import
end
def update
@import.update(import_params)
redirect_to imports_url, notice: 'Import was successfully updated.', status: :see_other
end
def create
@import = Import.new
authorize @import
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_entity and return
end
created_imports = []
raw_files.each do |item|
next if item.is_a?(ActionDispatch::Http::UploadedFile)
import = create_import_from_signed_id(item)
created_imports << import if import.present?
end
if created_imports.any?
redirect_to imports_url,
notice: "#{created_imports.size} files are queued to be imported in background",
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_entity and return
end
rescue StandardError => e
if created_imports.present?
import_ids = created_imports.map(&:id).compact
Import.where(id: import_ids).destroy_all if import_ids.any?
end
Rails.logger.error "Import error: #{e.message}"
Rails.logger.error e.backtrace.join("\n")
ExceptionReporter.call(e)
redirect_to new_import_path, alert: 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 authorize_import
authorize @import
end
def import_params
params.require(:import).permit(:name, files: [])
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)
import = current_user.imports.build(name: blob.filename.to_s)
import.file.attach(blob)
import.source = detect_import_source(import.file) if import.source.blank?
import.save!
import
end
def detect_import_source(file_attachment)
temp_file_path = Imports::SecureFileDownloader.new(file_attachment).download_to_temp_file
Imports::SourceDetector.new_from_file_header(temp_file_path).detect_source
rescue StandardError => e
Rails.logger.warn "Failed to auto-detect import source for #{file_attachment.filename}: #{e.message}"
nil
ensure
# Cleanup temp file
if temp_file_path && File.exist?(temp_file_path)
File.unlink(temp_file_path)
end
end
def validate_points_limit
limit_exceeded = PointsLimitExceeded.new(current_user).call
redirect_to imports_path, alert: 'Points limit exceeded', status: :unprocessable_entity if limit_exceeded
end
end