Files
simple-aircraft-manager-rails/app/services/aircraft_archive.rb
T
rickyandClaude Opus 5.5 ef7cd2efba Admin setting for the maximum upload size
- Admin -> Settings: maximum upload size per file (1-1024 MB, default 50),
  stored in a new settings table and read on each use, so a change applies
  to every process at once
- Enforced by attachment validation and the direct-upload endpoint; upload
  forms, bulk upload and Admin -> About show the current limit
- .sam.zip import accepts files up to 1 GB to match

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:45:12 -05:00

44 lines
2.1 KiB
Ruby

# Aircraft archives (.sam.zip): a whole aircraft's records and files in one ZIP,
# in the format of the original Simple Aircraft Manager (manifest schema v2), so
# archives move both ways between this app and the original.
#
# manifest.json all records, with archive-local IDs linking them
# attachments/… pictures, document pages, squawk files, track logs…
#
# Records carry a few extra keys for data only this app has (all squawk
# attachments, engine monitor files, oil report PDFs and lab averages, newer AD
# fields). The original ignores keys it doesn't know inside records, but rejects
# unknown top-level keys, so none are added there.
module AircraftArchive
class Error < StandardError; end
SCHEMA_VERSION = 2
TOP_LEVEL_KEYS = %w[
schema_version exported_at source_instance aircraft component_types components document_collections
documents document_images logbook_entries squawks inspection_types inspection_records ads ad_compliances
consumable_records major_records notes oil_analysis_reports flight_logs features
].freeze
OPTIONAL_KEYS = %w[features].freeze # and flight_logs before schema v2
MANIFEST_MAX_BYTES = 50.megabytes
FILE_MAX_BYTES = 1.gigabyte # the most Admin → Settings allows for uploads
ARCHIVE_MAX_BYTES = 10.gigabytes # uncompressed
MAX_RATIO = 100 # compression ratio above this looks like a zip bomb
# Leading bytes per extension; text formats have none to check.
MAGIC = {
"jpg" => [ "\xFF\xD8\xFF".b ], "jpeg" => [ "\xFF\xD8\xFF".b ], "png" => [ "\x89PNG\r\n\x1A\n".b ],
"gif" => [ "GIF8".b ], "webp" => [ "RIFF".b ], "bmp" => [ "BM".b ], "tif" => [ "II*\x00".b, "MM\x00*".b ],
"tiff" => [ "II*\x00".b, "MM\x00*".b ], "pdf" => [ "%PDF-".b ]
}.freeze
TEXT_EXTENSIONS = %w[txt csv gpx kml xml].freeze
EXTENSIONS = (MAGIC.keys + TEXT_EXTENSIONS).freeze
def self.extension(path) = File.extname(path.to_s).delete(".").downcase
def self.content_matches?(data, ext)
return EXTENSIONS.include?(ext) unless MAGIC.key?(ext)
MAGIC[ext].any? { data.byteslice(0, _1.bytesize) == _1 }
end
end