diff --git a/app/controllers/aircraft_exports_controller.rb b/app/controllers/aircraft_exports_controller.rb index 88d66db..6c627db 100644 --- a/app/controllers/aircraft_exports_controller.rb +++ b/app/controllers/aircraft_exports_controller.rb @@ -14,6 +14,8 @@ class AircraftExportsController < ApplicationController path = EXPORT_DIR.join("#{SecureRandom.hex(12)}.zip").to_s exporter.write_to(path) log_event("aircraft", "Aircraft exported (.sam.zip)") - send_file path, filename: exporter.filename, type: "application/zip", disposition: "attachment" + # Generic binary, not application/zip: Safari unzips zip downloads into a + # folder ("Open safe files"), and the archive must stay one file to import. + send_file path, filename: exporter.filename, type: "application/octet-stream", disposition: "attachment" end end diff --git a/app/controllers/records_packages_controller.rb b/app/controllers/records_packages_controller.rb index ff5797d..57bb3d9 100644 --- a/app/controllers/records_packages_controller.rb +++ b/app/controllers/records_packages_controller.rb @@ -11,6 +11,8 @@ class RecordsPackagesController < ApplicationController path = dir.join("#{SecureRandom.hex(12)}.zip").to_s package.write_to(path) log_event("aircraft", "Records package downloaded") - send_file path, filename: package.filename, type: "application/zip", disposition: "attachment" + # Generic binary, not application/zip: Safari unzips zip downloads into a + # folder ("Open safe files"); the package should stay one file to hand over. + send_file path, filename: package.filename, type: "application/octet-stream", disposition: "attachment" end end diff --git a/app/services/aircraft_archive/importer.rb b/app/services/aircraft_archive/importer.rb index 5ab1ced..9a4ee83 100644 --- a/app/services/aircraft_archive/importer.rb +++ b/app/services/aircraft_archive/importer.rb @@ -35,6 +35,15 @@ module AircraftArchive raise Error, "Not a valid .sam.zip archive (#{e.message})" end + # "" normally; "folder/" when everything sits in one folder, as when a + # browser unzipped the download and the folder was zipped again (Finder's + # Compress adds that folder, and a __MACOSX folder that's ignored). + def self.root_prefix(zip) + return "" if zip.find_entry("manifest.json") + folders = zip.entries.map(&:name).reject { _1.start_with?("__MACOSX/") }.grep(%r{\A[^/]+/manifest\.json\z}) + folders.one? ? folders.first.delete_suffix("manifest.json") : "" + end + def self.validate_zip(zip) uncompressed = compressed = 0 zip.each do |entry| @@ -46,7 +55,7 @@ module AircraftArchive end raise Error, "The archive is compressed suspiciously well (over #{MAX_RATIO}:1)." if compressed.positive? && uncompressed / compressed > MAX_RATIO - entry = zip.find_entry("manifest.json") or raise Error, "The archive has no manifest.json. Is it a .sam.zip export?" + entry = zip.find_entry("#{root_prefix(zip)}manifest.json") or raise Error, "The archive has no manifest.json. Is it a .sam.zip export?" raise Error, "manifest.json is too large." if entry.size > MANIFEST_MAX_BYTES manifest = begin JSON.parse(entry.get_input_stream.read.force_encoding("UTF-8")) @@ -92,6 +101,7 @@ module AircraftArchive self.class.with_zip(@path) do |zip| @zip = zip @manifest = self.class.validate_zip(zip) + @root = self.class.root_prefix(zip) import_all end { aircraft: @aircraft, counts: @counts, warnings: @warnings } @@ -487,7 +497,7 @@ module AircraftArchive def upload(archive_path) raise Error, "unsafe file path #{archive_path}" unless self.class.safe_name?(archive_path) - entry = @zip.find_entry(archive_path) or raise Error, "file #{File.basename(archive_path)} isn't in the archive" + entry = @zip.find_entry("#{@root}#{archive_path}") or raise Error, "file #{File.basename(archive_path)} isn't in the archive" raise Error, "#{File.basename(archive_path)} is too large" if entry.size > FILE_MAX_BYTES ext = AircraftArchive.extension(archive_path) raise Error, "#{File.basename(archive_path)} isn't an allowed file type" unless EXTENSIONS.include?(ext) diff --git a/docs/guide/sharing.md b/docs/guide/sharing.md index d7184e2..c151aa1 100644 --- a/docs/guide/sharing.md +++ b/docs/guide/sharing.md @@ -21,7 +21,7 @@ Give it a label and optionally an expiry. Documents only appear if their visibil ## Backup and transfer -**Download .sam.zip** exports the whole aircraft with its files. Anyone can load it with **Fleet → Import** (in this app or the original Simple Aircraft Manager) to make a copy they own, for example when the aircraft is sold. Use a new tail number if the original is already in that app. +**Download .sam.zip** exports the whole aircraft with its files. Anyone can load it with **Fleet → Import** (in this app or the original Simple Aircraft Manager) to make a copy they own, for example when the aircraft is sold. Use a new tail number if the original is already in that app. If an older download arrived as a folder (Safari can unzip downloads by itself), compress the folder back into a zip (in Finder: right-click → Compress) and import that. ## Activity diff --git a/test/integration/aircraft_archive_flow_test.rb b/test/integration/aircraft_archive_flow_test.rb index 1cdf255..2287559 100644 --- a/test/integration/aircraft_archive_flow_test.rb +++ b/test/integration/aircraft_archive_flow_test.rb @@ -16,7 +16,8 @@ class AircraftArchiveFlowTest < ActionDispatch::IntegrationTest def download_export get aircraft_export_path(@a) assert_response :success - assert_equal "application/zip", response.media_type + assert_equal "application/octet-stream", response.media_type # so Safari doesn't unzip it into a folder + assert_match(/attachment; filename="n12345-[\d-]+\.sam\.zip"/, response.headers["Content-Disposition"]) path = File.join(Dir.mktmpdir, "export.sam.zip") File.binwrite(path, response.body) path @@ -40,6 +41,27 @@ class AircraftArchiveFlowTest < ActionDispatch::IntegrationTest assert_equal "Aircraft exported (.sam.zip)", @a.events.last.event_name end + test "an archive re-zipped from an unzipped folder still imports" do + sign_in @owner + path = download_export + rezipped = File.join(Dir.mktmpdir, "n12345.zip") + Zip::File.open(path) do |src| + Zip::OutputStream.open(rezipped) do |out| + out.put_next_entry("__MACOSX/n12345/._manifest.json") + out.write("resource fork") + src.each do |e| + next if e.directory? + out.put_next_entry("n12345-export/#{e.name}") + out.write(e.get_input_stream.read) + end + end + end + assert_equal "N12345", AircraftArchive::Importer.check(rezipped).tail_number + copy = AircraftArchive::Importer.new(rezipped, owner: @owner, tail_number: "N54321").run[:aircraft] + assert_equal @a.logbook_entries.count, copy.logbook_entries.count + assert_equal @a.documents.count, copy.documents.count + end + test "importing an archive: tail clash asks for a new tail, then the job builds the aircraft" do sign_in @owner path = download_export