diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 9d76023..accc7a7 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -1,13 +1,13 @@ class ReportsController < ApplicationController def index + @reports = Report.order(created_at: :desc).limit(10) end def create inn = params[:inn] - output_path = Rails.root.join('tmp', 'reports', inn) - FileUtils.mkdir_p(output_path) + report = Report.create!(inn: inn, status: 'processing') - FileProcessorJob.perform_async(inn, output_path.to_s) + FileProcessorJob.perform_async(inn, report.id) redirect_to reports_path, notice: 'Отчёт загружается в фоне' end diff --git a/app/models/report.rb b/app/models/report.rb new file mode 100644 index 0000000..b9ab0a8 --- /dev/null +++ b/app/models/report.rb @@ -0,0 +1,5 @@ +class Report < ApplicationRecord + has_many_attached :files + + validates :inn, presence: true +end diff --git a/app/sidekiq/file_processor_job.rb b/app/sidekiq/file_processor_job.rb index 8f36cec..a331ac5 100644 --- a/app/sidekiq/file_processor_job.rb +++ b/app/sidekiq/file_processor_job.rb @@ -5,16 +5,35 @@ require 'stringio' class FileProcessorJob include Sidekiq::Job - def perform(inn, output_path) + def perform(inn, report_id) + report = Report.find(report_id) Rails.logger.info "=== Начало загрузки для ИНН: #{inn} ===" - download_inn(inn, output_path) + + # Временная папка + temp_dir = Rails.root.join('tmp', 'reports', inn) + FileUtils.mkdir_p(temp_dir) + + download_inn(inn, temp_dir.to_s) + + Dir.glob("#{temp_dir}/**/*").select { |f| File.file?(f) }.each do |file_path| + report.files.attach( + io: File.open(file_path), + filename: File.basename(file_path) + ) + end + + # Удали временные файлы + FileUtils.rm_rf(temp_dir) + + report.update!(status: 'completed') Rails.logger.info "=== Завершено для ИНН: #{inn} ===" rescue => e + report.update!(status: 'failed') if report Rails.logger.error "=== ОШИБКА: #{e.message} ===" - Rails.logger.error e.backtrace.first(5).join("\n") raise end + private def get_content(url, json_need = false) diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erb index ea83847..577c6e5 100644 --- a/app/views/reports/index.html.erb +++ b/app/views/reports/index.html.erb @@ -1,17 +1,25 @@ -
<%= notice %>
- <% end %> +<% if notice %> +<%= notice %>
+<% end %> - <%= form_with url: reports_path, method: :post, local: true do |f| %> -ИНН: <%= report.inn %> | Статус: <%= report.status %>
+ <% if report.files.attached? %> +Файлы:
+ <% report.files.each do |file| %> + <%= link_to file.filename, rails_blob_path(file, disposition: "attachment") %> + <% end %> + <% end %> +