Files
simple-aircraft-manager-rails/app/controllers/aircraft_imports_controller.rb
T
rickyandClaude Opus 5.5 908c943e21 Fix crashes found by fuzzing malformed input
- 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>
2026-11-14 13:17:43 -05:00

40 lines
1.7 KiB
Ruby

# Upload a .sam.zip (from this app or the original Simple Aircraft Manager) to
# create an aircraft with all its records. The archive is checked right away;
# the import itself runs in the background (AircraftImportJob).
class AircraftImportsController < ApplicationController
before_action :set_import, only: :show
def new; end
def create
file = params[:archive]
return redirect_to(new_aircraft_import_path, alert: t("flash.aircraft_imports.choose_a_sam_zip")) unless file.respond_to?(:tempfile)
preview = AircraftArchive::Importer.check(file.tempfile.path)
tail = params[:tail_number].to_s.strip.upcase.presence || preview.tail_number.to_s.upcase
if Aircraft.where("UPPER(tail_number) = ?", tail).exists?
@preview = preview
@conflict = tail
flash.now[:alert] = t("flash.aircraft_imports.an_aircraft_already_exists", tail: tail)
return render :new, status: :unprocessable_entity
end
import = AircraftImport.create!(user: current_user, filename: file.original_filename, tail_number: tail, source: preview.source)
import.archive.attach(io: file.tempfile.tap(&:rewind), filename: file.original_filename.presence || "archive.sam.zip", content_type: "application/zip")
AircraftImportJob.perform_later(import.id)
redirect_to aircraft_import_path(import)
rescue AircraftArchive::Error => e
redirect_to new_aircraft_import_path, alert: t("flash.aircraft_imports.that_archive_can_t", message: e.message)
end
def show; end
private
# Users see their own imports; admins see all.
def set_import
scope = current_user.admin? ? AircraftImport.all : AircraftImport.where(user: current_user)
@import = scope.find(params[:id])
end
end