Repository Public
1// SP Notes iOS2// File: Features/Export/ZipWriter.swift3// Description: Dependency-free ZIP archive writer (deflate or stored, CRC-32, UTF-8 names, streaming from files) and the export of4// notes as an archive: the note file, its attachments and a metadata.json.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import UIKit10import zlib11import SPNotesCore1213enum ZipError: LocalizedError {14 case cannotCreate(URL)15 case duplicateEntry(String)16 case invalidName(String)17 /// The classic ZIP format (no ZIP64) holds at most 65 535 entries and 4 GiB per entry and per archive.18 case tooLarge19 case compressionFailed20 case finished2122 var errorDescription: String? {23 switch self {24 case .tooLarge: String(localized: "L’archive dépasse la taille maximale d’un fichier ZIP (4 Go).")25 default: String(localized: "La création de l’archive a échoué.")26 }27 }28}2930/// Writes a ZIP archive sequentially. Files are streamed in chunks: memory use does not depend on their size.31/// Not thread-safe: use one writer from one task.32final class ZipWriter {33 enum Method: UInt16 { case stored = 0, deflated = 8 }3435 private struct Entry {36 var name: [UInt8]37 var method: Method38 var crc: UInt3239 var compressedSize: UInt3240 var size: UInt3241 var time: UInt1642 var date: UInt1643 var offset: UInt3244 }4546 let url: URL47 private let handle: FileHandle48 private var entries: [Entry] = []49 private var names = Set<String>()50 private var offset: UInt64 = 051 private var isFinished = false52 private static let chunkSize = 256 * 102453 /// Already compressed formats are stored as they are (deflate would only cost time).54 private static let storedExtensions: Set<String> = ["jpg", "jpeg", "png", "heic", "heif", "gif", "webp", "avif", "mp4", "mov", "m4v", "m4a", "mp3", "aac", "zip", "gz", "7z", "rar", "docx", "xlsx", "pptx", "pages", "numbers", "key", "epub"]5556 init(url: URL) throws {57 self.url = url58 try? FileManager.default.removeItem(at: url)59 guard FileManager.default.createFile(atPath: url.path, contents: nil) else { throw ZipError.cannotCreate(url) }60 handle = try FileHandle(forWritingTo: url)61 }6263 deinit { try? handle.close() }6465 /// Adds bytes held in memory under `path` (forward slashes, relative).66 func addData(_ data: Data, path: String, modified: Date = Date(), method: Method? = nil) throws {67 var consumed = false68 try add(path: path, modified: modified, method: method ?? Self.defaultMethod(for: path)) {69 defer { consumed = true }70 return consumed ? nil : data71 }72 }7374 /// Adds a file, streamed from disk, under `path`.75 func addFile(at source: URL, path: String, modified: Date? = nil, method: Method? = nil) throws {76 let reader = try FileHandle(forReadingFrom: source)77 defer { try? reader.close() }78 let date = modified ?? ((try? source.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate ?? Date())79 try add(path: path, modified: date, method: method ?? Self.defaultMethod(for: path)) {80 try reader.read(upToCount: Self.chunkSize)81 }82 }8384 /// Writes the central directory and closes the file. Nothing can be added afterwards.85 func finish() throws {86 guard !isFinished else { return }87 isFinished = true88 guard entries.count <= Int(UInt16.max), offset <= UInt64(UInt32.max) else { throw ZipError.tooLarge }89 let start = offset90 var directory = Data()91 for entry in entries {92 directory.appendLE(UInt32(0x0201_4b50))93 directory.appendLE(UInt16(0x031e)) // made by: Unix, spec 3.094 directory.appendLE(UInt16(20)) // needed: 2.0 (deflate)95 directory.appendLE(UInt16(0x0800)) // names are UTF-896 directory.appendLE(entry.method.rawValue)97 directory.appendLE(entry.time); directory.appendLE(entry.date)98 directory.appendLE(entry.crc); directory.appendLE(entry.compressedSize); directory.appendLE(entry.size)99 directory.appendLE(UInt16(entry.name.count))100 directory.appendLE(UInt16(0)); directory.appendLE(UInt16(0)) // extra, comment101 directory.appendLE(UInt16(0)); directory.appendLE(UInt16(0)) // disk, internal attributes102 directory.appendLE(UInt32(0o100644) << 16) // regular file rw-r--r--103 directory.appendLE(entry.offset)104 directory.append(contentsOf: entry.name)105 }106 guard start + UInt64(directory.count) <= UInt64(UInt32.max) else { throw ZipError.tooLarge }107 var end = Data()108 end.appendLE(UInt32(0x0605_4b50))109 end.appendLE(UInt16(0)); end.appendLE(UInt16(0))110 end.appendLE(UInt16(entries.count)); end.appendLE(UInt16(entries.count))111 end.appendLE(UInt32(directory.count)); end.appendLE(UInt32(start))112 end.appendLE(UInt16(0))113 try write(directory)114 try write(end)115 try handle.synchronize()116 try handle.close()117 }118119 // MARK: internals120121 private static func defaultMethod(for path: String) -> Method {122 storedExtensions.contains((path as NSString).pathExtension.lowercased()) ? .stored : .deflated123 }124125 private func write(_ data: Data) throws {126 try handle.write(contentsOf: data)127 offset += UInt64(data.count)128 }129130 /// Local header with empty sizes, then the (compressed) bytes pulled from `next`, then the header is patched in place.131 private func add(path: String, modified: Date, method: Method, next: () throws -> Data?) throws {132 guard !isFinished else { throw ZipError.finished }133 let name = Array(path.utf8)134 guard !name.isEmpty, name.count <= Int(UInt16.max), !path.hasPrefix("/"), !path.contains("\\"), !path.split(separator: "/").contains("..") else { throw ZipError.invalidName(path) }135 guard names.insert(path.lowercased()).inserted else { throw ZipError.duplicateEntry(path) }136 guard entries.count < Int(UInt16.max), offset <= UInt64(UInt32.max) else { throw ZipError.tooLarge }137 let (time, date) = Self.dosDateTime(modified)138 let headerOffset = offset139 var header = Data()140 header.appendLE(UInt32(0x0403_4b50))141 header.appendLE(UInt16(20))142 header.appendLE(UInt16(0x0800))143 header.appendLE(method.rawValue)144 header.appendLE(time); header.appendLE(date)145 header.appendLE(UInt32(0)); header.appendLE(UInt32(0)); header.appendLE(UInt32(0)) // crc, sizes: patched below146 header.appendLE(UInt16(name.count)); header.appendLE(UInt16(0))147 header.append(contentsOf: name)148 try write(header)149150 var crc: UInt = crc32(0, nil, 0)151 var size: UInt64 = 0152 var compressed: UInt64 = 0153 var deflater = method == .deflated ? try Deflater() : nil154 while let chunk = try next(), !chunk.isEmpty {155 size += UInt64(chunk.count)156 crc = chunk.withUnsafeBytes { crc32(crc, $0.bindMemory(to: Bytef.self).baseAddress, uInt($0.count)) }157 let out = try deflater?.compress(chunk, finish: false) ?? chunk158 compressed += UInt64(out.count)159 if !out.isEmpty { try write(out) }160 guard size <= UInt64(UInt32.max), compressed <= UInt64(UInt32.max) else { throw ZipError.tooLarge }161 }162 if let tail = try deflater?.compress(Data(), finish: true) {163 compressed += UInt64(tail.count)164 try write(tail)165 }166 deflater = nil167 guard compressed <= UInt64(UInt32.max), offset <= UInt64(UInt32.max) else { throw ZipError.tooLarge }168169 var patch = Data()170 patch.appendLE(UInt32(truncatingIfNeeded: crc)); patch.appendLE(UInt32(compressed)); patch.appendLE(UInt32(size))171 try handle.seek(toOffset: headerOffset + 14)172 try handle.write(contentsOf: patch)173 try handle.seek(toOffset: offset)174 entries.append(Entry(name: name, method: method, crc: UInt32(truncatingIfNeeded: crc), compressedSize: UInt32(compressed), size: UInt32(size), time: time, date: date, offset: UInt32(headerOffset)))175 }176177 static func dosDateTime(_ date: Date) -> (time: UInt16, date: UInt16) {178 let c = Calendar(identifier: .gregorian).dateComponents(in: .current, from: date)179 let year = min(max((c.year ?? 1980) - 1980, 0), 127)180 let time = UInt16((c.hour ?? 0) << 11 | (c.minute ?? 0) << 5 | (c.second ?? 0) / 2)181 let day = UInt16(year << 9 | (c.month ?? 1) << 5 | (c.day ?? 1))182 return (time, day)183 }184}185186/// Raw deflate stream (no zlib header), as ZIP stores it.187private final class Deflater {188 private var stream = z_stream()189 private var buffer = [UInt8](repeating: 0, count: 256 * 1024)190191 init() throws {192 guard deflateInit2_(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY, ZLIB_VERSION, Int32(MemoryLayout<z_stream>.size)) == Z_OK else { throw ZipError.compressionFailed }193 }194195 deinit { deflateEnd(&stream) }196197 func compress(_ input: Data, finish: Bool) throws -> Data {198 var out = Data()199 try input.withUnsafeBytes { (raw: UnsafeRawBufferPointer) in200 stream.next_in = UnsafeMutablePointer(mutating: raw.bindMemory(to: Bytef.self).baseAddress)201 stream.avail_in = uInt(raw.count)202 var status = Z_OK203 repeat {204 let produced: Int = try buffer.withUnsafeMutableBufferPointer { pointer in205 stream.next_out = pointer.baseAddress206 stream.avail_out = uInt(pointer.count)207 status = deflate(&stream, finish ? Z_FINISH : Z_NO_FLUSH)208 guard status == Z_OK || status == Z_STREAM_END || status == Z_BUF_ERROR else { throw ZipError.compressionFailed }209 return pointer.count - Int(stream.avail_out)210 }211 out.append(contentsOf: buffer[0..<produced])212 } while stream.avail_out == 0 || (finish && status != Z_STREAM_END)213 stream.next_in = nil214 }215 return out216 }217}218219private extension Data {220 mutating func appendLE<T: FixedWidthInteger>(_ value: T) {221 Swift.withUnsafeBytes(of: value.littleEndian) { append(contentsOf: $0) }222 }223}224225// MARK: - Notes archive226227/// Export of one or several notes as a ZIP archive.228///229/// One note: `<title>.<ext>`, `attachments/<file>…`, `metadata.json` at the root.230/// Several notes: the same three things inside one folder per note (`<title>/…`, made unique).231/// In Markdown and HTML the `/api/files/<id>` references of the attachments present in the archive are rewritten to232/// `attachments/<file>` so pictures and files open from the extracted folder.233@MainActor234enum NoteArchive {235 typealias Note = (title: String, document: NoteDocument, attachments: [URL], metadata: [String: String])236237 /// A note rendered and ready to be written (no main-actor state).238 struct Prepared: Sendable {239 var folder: String // "" or "<name>/"240 var fileName: String241 var content: Data242 var attachments: [(name: String, url: URL)]243 var metadata: Data244 }245246 /// Builds the archive in a temporary directory and returns its URL. Synchronous: for a few notes with small attachments.247 static func exportArchive(notes: [Note], format: ExportFormat) throws -> URL {248 let prepared = try prepare(notes: notes, format: format)249 return try write(prepared, to: try archiveURL(for: notes))250 }251252 /// Same result; the notes are rendered on the main actor, the attachments are compressed off it.253 static func exportArchiveInBackground(notes: [Note], format: ExportFormat) async throws -> URL {254 let prepared = try prepare(notes: notes, format: format)255 let url = try archiveURL(for: notes)256 return try await Task.detached(priority: .userInitiated) { try write(prepared, to: url) }.value257 }258259 private static func archiveURL(for notes: [Note]) throws -> URL {260 let name: String261 if notes.count == 1 { name = NoteExporter.sanitizedFileName(notes[0].title) } else {262 let formatter = DateFormatter()263 formatter.locale = Locale(identifier: "en_US_POSIX")264 formatter.dateFormat = "yyyy-MM-dd"265 name = "SP Notes \(formatter.string(from: Date()))"266 }267 return try NoteExporter.temporaryURL(fileName: "\(name).zip")268 }269270 static func prepare(notes: [Note], format: ExportFormat) throws -> [Prepared] {271 var folders = UniqueNames()272 let exportedAt = ISO8601DateFormatter().string(from: Date())273 return try notes.map { note in274 let base = NoteExporter.sanitizedFileName(note.title)275 let folder = notes.count == 1 ? "" : folders.claim(base) + "/"276 var names = UniqueNames()277 var attachments: [(name: String, url: URL)] = []278 var byID: [String: (name: String, url: URL)] = [:]279 for url in note.attachments {280 let name = names.claim(NoteExporter.sanitizedFileName((url.lastPathComponent as NSString).deletingPathExtension, fallback: "fichier"), extension: url.pathExtension.precomposedStringWithCanonicalMapping)281 attachments.append((name, url))282 // The attachment store keeps files as `<id>/<filename>`.283 let id = url.deletingLastPathComponent().lastPathComponent.lowercased()284 if UUID(uuidString: id) != nil { byID[id] = (name, url) }285 }286 let images = ImageLookup(byID: byID.mapValues(\.url))287 var content: Data288 switch format {289 case .markdown, .html:290 // Pictures are files of the archive: link them instead of embedding them.291 var text = format == .markdown ? note.document.markdown() : note.document.html(title: note.title)292 for (id, file) in byID {293 let path = "attachments/" + (file.name.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? file.name)294 text = text.replacingOccurrences(of: AttachmentFiles.source(forAttachmentID: id), with: path, options: .caseInsensitive)295 }296 content = Data(text.utf8)297 case .plainText, .rtf, .pdf:298 content = try NoteExporter.data(document: note.document, title: note.title, format: format, imageProvider: images.image(forSource:))299 }300 let fileName = "\(base).\(format.fileExtension)"301 let info: [String: Any] = [302 "title": note.title, "file": fileName, "format": format.rawValue, "exportedAt": exportedAt,303 "attachments": attachments.map { "attachments/\($0.name)" }, "metadata": note.metadata,304 ]305 let metadata = try JSONSerialization.data(withJSONObject: info, options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes])306 return Prepared(folder: folder, fileName: fileName, content: content, attachments: attachments, metadata: metadata)307 }308 }309310 nonisolated static func write(_ notes: [Prepared], to url: URL) throws -> URL {311 let zip = try ZipWriter(url: url)312 do {313 for note in notes {314 try zip.addData(note.content, path: note.folder + note.fileName)315 for attachment in note.attachments where FileManager.default.fileExists(atPath: attachment.url.path) {316 try zip.addFile(at: attachment.url, path: "\(note.folder)attachments/\(attachment.name)")317 }318 try zip.addData(note.metadata, path: note.folder + "metadata.json")319 }320 try zip.finish()321 } catch {322 try? FileManager.default.removeItem(at: url)323 throw error324 }325 return url326 }327328 /// Pictures of the exported attachments, by document `src`, decoded at a bounded size.329 private struct ImageLookup {330 var byID: [String: URL]331 func image(forSource src: String) -> UIImage? {332 guard let id = AttachmentFiles.attachmentID(fromSource: src), let url = byID[id],333 let source = CGImageSourceCreateWithURL(url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary) else { return nil }334 let options: [CFString: Any] = [kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceCreateThumbnailWithTransform: true, kCGImageSourceThumbnailMaxPixelSize: 1600]335 guard let image = CGImageSourceCreateThumbnailAtIndex(source, 0, options as CFDictionary) else { return nil }336 return UIImage(cgImage: image, scale: 2, orientation: .up)337 }338 }339}340341/// Hands out names that are unique without regard to case ("Note", "Note 2", "Note 3"…).342struct UniqueNames {343 private var used = Set<String>()344345 mutating func claim(_ base: String, extension ext: String = "") -> String {346 func full(_ n: Int) -> String {347 let stem = n == 1 ? base : "\(base) \(n)"348 return ext.isEmpty ? stem : "\(stem).\(ext)"349 }350 var n = 1351 while !used.insert(full(n).lowercased()).inserted { n += 1 }352 return full(n)353 }354}355356// MARK: - 2.0: the files a note references (pictures, files, and the attachments of the 3.0 blocks)357358extension NoteArchive {359 /// A note ready for the archive with every file its document references that is on the device: pictures and files as360 /// before, and — through the Core's `attachmentReferences()` — the renders of `mermaid` / `drawing`, the ink and PencilKit361 /// data of a drawing, the file of a `pdf` and of an `audio` block. Files that are not on the device are left out (never362 /// downloaded for an export); `missing` counts them.363 static func note(title: String, document: NoteDocument, metadata: [String: String] = [:], app: AppModel) async -> (note: Note, missing: Int) {364 var urls: [URL] = []365 var missing = 0366 for id in document.attachmentReferences() {367 if let url = await app.fileOnDevice(forAttachment: id) { urls.append(url) } else { missing += 1 }368 }369 return ((title: title, document: document, attachments: urls, metadata: metadata), missing)370 }371}372