mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
- Components can't be made into a parent loop, and the parts tree walks stop safely if one exists (logbook entry and squawk resolution pages crashed). - A rejected aircraft photo no longer breaks the edit page; empty photos are refused; images that can't be read aren't resized, and a failed resize sends the original instead of an error. - Unsupported formats (/report.pdf…) answer 406; tampered upload references, out-of-range numbers and constraint violations get a form message instead of a 500; requests with null bytes are refused (400). - Blank values in required columns with defaults take the default; optional links to records that don't exist are validation errors. - Imports (oil report, logbook scans, .sam.zip) accept only uploaded files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
2.4 KiB
Ruby
56 lines
2.4 KiB
Ruby
# Upload scanned logbook pages (images or PDFs) as a document and, when an
|
|
# Anthropic API key is configured, have Claude transcribe them into logbook
|
|
# entries in the background (LogbookTranscriptionJob).
|
|
class LogbookImportsController < ApplicationController
|
|
include AircraftScoped
|
|
before_action :require_owner!
|
|
|
|
def new
|
|
@collections = @aircraft.document_collections.ordered
|
|
end
|
|
|
|
def create
|
|
files = Array(params[:pages]).select { _1.respond_to?(:original_filename) } # uploaded files only
|
|
return redirect_to(new_aircraft_logbook_import_path(@aircraft), alert: t("flash.logbook_imports.choose_the_scanned_pages")) if files.empty?
|
|
|
|
# Page order follows the file names ("page-002" before "page-010").
|
|
files = files.sort_by { |f| f.original_filename.to_s.downcase.scan(/\d+|\D+/).map { _1.match?(/\d/) ? [ 0, _1.to_i ] : [ 1, _1 ] } }
|
|
collection = find_or_build_collection
|
|
document = @aircraft.documents.new(name: params[:document_name].presence || collection.name, doc_type: doc_type,
|
|
document_collection: collection)
|
|
document.pages.attach(files)
|
|
unless collection.valid? && document.valid?
|
|
@collections = @aircraft.document_collections.ordered
|
|
flash.now[:alert] = (collection.errors.full_messages + document.errors.full_messages).to_sentence
|
|
return render :new, status: :unprocessable_entity
|
|
end
|
|
|
|
import = nil
|
|
ActiveRecord::Base.transaction do
|
|
collection.save!
|
|
document.save!
|
|
import = @aircraft.logbook_imports.create!(user: current_user, document: document, log_type: params[:log_type],
|
|
transcribe: params[:transcribe] == "1" && LogbookImport.ai_available?)
|
|
end
|
|
log_event("document", t("flash.logbook_imports.pages_uploaded", name: document.name, count: files.size))
|
|
LogbookTranscriptionJob.perform_later(import.id)
|
|
redirect_to aircraft_logbook_import_path(@aircraft, import)
|
|
end
|
|
|
|
def show
|
|
@import = @aircraft.logbook_imports.find(params[:id])
|
|
end
|
|
|
|
private
|
|
|
|
def find_or_build_collection
|
|
if params[:collection_id].present? && params[:collection_id] != "new"
|
|
@aircraft.document_collections.find(params[:collection_id])
|
|
else
|
|
@aircraft.document_collections.new(name: params[:collection_name].presence || "Logbooks", visibility: "maintenance")
|
|
end
|
|
end
|
|
|
|
def doc_type = Document::TYPES.key?(params[:doc_type]) ? params[:doc_type] : "LOG"
|
|
end
|