Files
simple-aircraft-manager-rails/app/controllers/oil_analyses_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

118 lines
5.3 KiB
Ruby

class OilAnalysesController < ApplicationController
include AircraftScoped
before_action :require_owner!, only: %i[edit update destroy]
before_action :set_analysis, only: %i[show edit update destroy]
COMPARE_SAMPLES = 6 # columns in the trend table, like the lab report's history
def index
@analyses = @aircraft.oil_analyses.recent_first.includes(:component).with_attached_report
end
def show
@previous = @aircraft.oil_analyses.where(component_id: @analysis.component_id)
.where("sample_date < ?", @analysis.sample_date).recent_first.first
end
def new
engine = default_engine
@analysis = @aircraft.oil_analyses.new(sample_date: Date.current, component: engine,
engine_hours: engine&.hours_since_overhaul,
lab: @aircraft.oil_analyses.recent_first.pick(:lab))
end
def create
@analysis = @aircraft.oil_analyses.new(analysis_params)
if @analysis.save
log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_added_sampled", sample_date: l(@analysis.sample_date, format: :app)))
redirect_to aircraft_oil_analysis_path(@aircraft, @analysis), notice: t("flash.oil_analyses.oil_analysis_saved")
else
render :new, status: :unprocessable_entity
end
end
# Create records from a lab report PDF (AvLab or Blackstone). Reports repeat
# earlier samples, so samples already on file are skipped rather than duplicated.
def import
file = params[:report]
return redirect_to(new_aircraft_oil_analysis_path(@aircraft), alert: t("flash.oil_analyses.choose_a_lab_report")) unless file.respond_to?(:tempfile)
report = OilReportParser.from_pdf(file.tempfile)
file.rewind
if report.tail_number.present? && normalize_tail(report.tail_number) != normalize_tail(@aircraft.tail_number)
return redirect_to(new_aircraft_oil_analysis_path(@aircraft),
alert: t("flash.oil_analyses.that_report_is_for", report_tail: report.tail_number, tail_number: @aircraft.tail_number))
end
engine = engine_for(report)
created = []
current_record = nil
OilAnalysis.transaction do
report.samples.each do |sample|
record = existing_sample(sample) ||
@aircraft.oil_analyses.create!(sample.except(:current).merge(lab: report.lab, oil_type: report.oil_type, component: engine)).tap { created << _1 }
current_record = record if sample[:current]
end
end
current_record.report.attach(file) if current_record && !current_record.report.attached?
skipped = report.samples.size - created.size
created.each { log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_imported_sampled", sample_date: _1.sample_date)) }
notice = t("flash.oil_analyses.imported", count: created.size, lab: report.lab)
notice += " #{t('flash.oil_analyses.already_recorded', count: skipped)}" if skipped.positive?
redirect_to aircraft_oil_analyses_path(@aircraft), notice: notice
rescue OilReportParser::Error => e
redirect_to new_aircraft_oil_analysis_path(@aircraft), alert: t("flash.oil_analyses.couldn_t_import_that", message: e.message)
end
def edit; end
def update
if @analysis.update(analysis_params)
log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_updated_sampled", sample_date: l(@analysis.sample_date, format: :app)))
redirect_to aircraft_oil_analysis_path(@aircraft, @analysis), notice: t("flash.oil_analyses.oil_analysis_updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@analysis.destroy!
log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_deleted_sampled", sample_date: l(@analysis.sample_date, format: :app)))
redirect_to aircraft_oil_analyses_path(@aircraft), notice: t("flash.oil_analyses.oil_analysis_deleted")
end
private
def set_analysis = @analysis = @aircraft.oil_analyses.find(params[:id])
def normalize_tail(tail) = tail.to_s.upcase.delete("^A-Z0-9")
# Same sample date, and the same lab sample number unless one side has none.
def existing_sample(sample)
scope = @aircraft.oil_analyses.where(sample_date: sample[:sample_date])
scope = scope.where(sample_number: [ sample[:sample_number], nil ]) if sample[:sample_number]
scope.first
end
# The engine whose serial number is on the report, else the one in service.
def engine_for(report)
serial = report.engine_serial.to_s.strip.upcase
match = @aircraft.components.where.not(serial_number: [ nil, "" ]).find { _1.serial_number.strip.upcase == serial } if serial.present?
match || default_engine
end
def default_engine
@aircraft.components.joins(:component_type).where(status: "IN-USE", component_types: { name: "Engine" }).first
end
def analysis_params
params.require(:oil_analysis).permit(
:component_id, :sample_date, :analysis_date, :lab, :sample_number, :oil_type, :engine_hours, :oil_hours,
:filter_hours, :oil_added, :status, :lab_comments, :notes, :report, :excluded_from_averages,
elements: OilAnalysis::ELEMENTS.keys, element_averages: OilAnalysis::ELEMENTS.keys,
oil_properties: OilAnalysis::PROPERTIES.keys
).tap { |p| p[:component_id] = scoped_id(@aircraft.components, p[:component_id]) if p.key?(:component_id) }
end
end