Skip to content

Commit 625bfd2

Browse files
committed
feat: offline hardening
1 parent 35aba22 commit 625bfd2

192 files changed

Lines changed: 30641 additions & 10257 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.pnpm-store/v11/.pnpm-needs-build-marker‎

Whitespace-only changes.

‎.pnpm-store/v11/index.db‎

8 KB
Binary file not shown.

‎.pnpm-store/v11/index.db-shm‎

32 KB
Binary file not shown.

‎.pnpm-store/v11/index.db-wal‎

Whitespace-only changes.

‎Compendus/CCReader/Sources/CCReader/Engine/Native/CCDPack.swift‎

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ import Foundation
1111
import ZIPFoundation
1212

1313
public enum CCDPack {
14+
public enum PackError: LocalizedError {
15+
case unsafeEntry(String)
16+
case archiveTooLarge
17+
case missingManifest
18+
case missingResource(String)
19+
20+
public var errorDescription: String? {
21+
switch self {
22+
case .unsafeEntry(let path): return "Unsafe path in offline pack: \(path)"
23+
case .archiveTooLarge: return "Offline pack expands beyond its safe size limit."
24+
case .missingManifest: return "Offline pack is missing its manifest."
25+
case .missingResource(let path): return "Offline pack is missing resource: \(path)"
26+
}
27+
}
28+
}
29+
1430
/// Unzip a downloaded CCD pack archive into `destination`, replacing any
1531
/// existing contents. The archive contains `manifest.ccd.json` plus a
1632
/// `resources/<handle>` tree for every referenced image.
@@ -19,20 +35,123 @@ public enum CCDPack {
1935
@discardableResult
2036
public static func unpack(zipData: Data, into destination: URL) throws -> URL {
2137
let fm = FileManager.default
22-
23-
// Fresh extraction: remove any stale contents so partial/old packs don't linger.
24-
if fm.fileExists(atPath: destination.path) {
25-
try fm.removeItem(at: destination)
26-
}
27-
try fm.createDirectory(at: destination, withIntermediateDirectories: true)
28-
29-
// Write the zip to a temp file, then extract via ZIPFoundation.
3038
let tmpZip = fm.temporaryDirectory.appendingPathComponent("ccd-pack-\(UUID().uuidString).zip")
3139
try zipData.write(to: tmpZip, options: .atomic)
3240
defer { try? fm.removeItem(at: tmpZip) }
41+
return try install(zipURL: tmpZip, into: destination)
42+
}
3343

34-
try fm.unzipItem(at: tmpZip, to: destination)
44+
/// Transactionally install a pack. The previous verified directory remains
45+
/// available until the staged replacement has been fully extracted and checked.
46+
@discardableResult
47+
public static func install(zipURL: URL, into destination: URL) throws -> URL {
48+
let fm = FileManager.default
49+
let parent = destination.deletingLastPathComponent()
50+
try fm.createDirectory(at: parent, withIntermediateDirectories: true)
51+
let staging = parent.appendingPathComponent(".\(destination.lastPathComponent)-staging-\(UUID().uuidString)")
52+
let backup = parent.appendingPathComponent(".\(destination.lastPathComponent)-backup-\(UUID().uuidString)")
53+
defer {
54+
try? fm.removeItem(at: staging)
55+
try? fm.removeItem(at: backup)
56+
}
57+
58+
guard let archive = Archive(url: zipURL, accessMode: .read) else {
59+
throw CocoaError(.fileReadCorruptFile)
60+
}
61+
let compressedSize = (try? zipURL.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
62+
let expandedLimit = max(Int64(compressedSize) * 6, 128 * 1024 * 1024)
63+
var expanded: Int64 = 0
64+
for entry in archive {
65+
let path = entry.path.replacingOccurrences(of: "\\", with: "/")
66+
if path.hasPrefix("/") || path.split(separator: "/").contains("..") {
67+
throw PackError.unsafeEntry(entry.path)
68+
}
69+
expanded += Int64(entry.uncompressedSize)
70+
if expanded > expandedLimit { throw PackError.archiveTooLarge }
71+
}
3572

73+
try fm.createDirectory(at: staging, withIntermediateDirectories: true)
74+
try fm.unzipItem(at: zipURL, to: staging)
75+
try validateInstalledPack(at: staging)
76+
77+
let hadExisting = fm.fileExists(atPath: destination.path)
78+
if hadExisting { try fm.moveItem(at: destination, to: backup) }
79+
do {
80+
try fm.moveItem(at: staging, to: destination)
81+
if hadExisting { try? fm.removeItem(at: backup) }
82+
} catch {
83+
if hadExisting, !fm.fileExists(atPath: destination.path) {
84+
try? fm.moveItem(at: backup, to: destination)
85+
}
86+
throw error
87+
}
3688
return destination.appendingPathComponent("manifest.ccd.json")
3789
}
90+
91+
public static func validateInstalledPack(at directory: URL) throws {
92+
let fm = FileManager.default
93+
let manifest = directory.appendingPathComponent("manifest.ccd.json")
94+
guard fm.fileExists(atPath: manifest.path) else { throw PackError.missingManifest }
95+
let bundle = try CCDBundle.decode(from: Data(contentsOf: manifest))
96+
let resources = directory.appendingPathComponent("resources", isDirectory: true)
97+
for handle in resourceHandles(in: bundle) {
98+
let normalized = handle.replacingOccurrences(of: "\\", with: "/")
99+
if normalized.hasPrefix("/") || normalized.split(separator: "/").contains("..") {
100+
throw PackError.unsafeEntry(handle)
101+
}
102+
guard fm.fileExists(atPath: resources.appendingPathComponent(normalized).path) else {
103+
throw PackError.missingResource(handle)
104+
}
105+
}
106+
}
107+
108+
private static func resourceHandles(in bundle: CCDBundle) -> Set<String> {
109+
var result = Set<String>()
110+
func visit(_ blocks: [CCDBlock]) {
111+
for block in blocks {
112+
if (block.t == "image" || block.t == "embed"), let resource = block.resource {
113+
result.insert(resource)
114+
}
115+
if let children = block.children { visit(children) }
116+
if let items = block.listItems { items.forEach(visit) }
117+
if let items = block.defItems { items.forEach { visit($0.definition) } }
118+
if let rows = block.rows { rows.forEach { $0.cells.forEach { visit($0.blocks) } } }
119+
if let caption = block.caption { visit(caption) }
120+
}
121+
}
122+
bundle.chapters.forEach {
123+
visit($0.blocks)
124+
$0.notes?.values.forEach(visit)
125+
}
126+
return result
127+
}
128+
129+
/// Structural validation for a locally-readable CBZ artifact.
130+
public static func validateCBZ(at url: URL) throws {
131+
guard let archive = Archive(url: url, accessMode: .read) else {
132+
throw CocoaError(.fileReadCorruptFile)
133+
}
134+
let imageExtensions = Set(["jpg", "jpeg", "png", "gif", "webp", "bmp"])
135+
var imageCount = 0
136+
for entry in archive where entry.type == .file {
137+
let path = entry.path.replacingOccurrences(of: "\\", with: "/")
138+
if path.hasPrefix("/") || path.split(separator: "/").contains("..") {
139+
throw PackError.unsafeEntry(entry.path)
140+
}
141+
if imageExtensions.contains(URL(fileURLWithPath: path).pathExtension.lowercased()) {
142+
imageCount += 1
143+
}
144+
}
145+
if imageCount == 0 { throw PackError.missingResource("comic pages") }
146+
}
147+
148+
public static func validateEPUB(at url: URL) throws {
149+
guard let archive = Archive(url: url, accessMode: .read) else {
150+
throw CocoaError(.fileReadCorruptFile)
151+
}
152+
let names = Set(archive.map(\.path))
153+
guard names.contains("mimetype"), names.contains("META-INF/container.xml") else {
154+
throw PackError.missingResource("EPUB container metadata")
155+
}
156+
}
38157
}

‎Compendus/CCReader/Sources/CCReader/Engine/Native/NativeReaderEngine.swift‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public class NativeReaderEngine: ReaderEngine {
212212

213213
/// Chapter title for a given spine index.
214214
public func chapterTitle(forSpineIndex spineIndex: Int) -> String? {
215-
spineTitles[spineIndex]
215+
chapterTitleOrTocTitle(forSpineIndex: spineIndex)
216216
}
217217

218218
/// The CCD table-of-contents entries (title + spineIndex + nested children).
@@ -401,11 +401,13 @@ public class NativeReaderEngine: ReaderEngine {
401401
let spineIdx = json["spineIndex"] as? Int,
402402
chaptersBySpine[spineIdx] != nil {
403403
initialSpineIndex = spineIdx
404-
// Prefer charOffset for precise cross-device restoration
404+
// Prefer charOffset for precise cross-device restoration.
405+
// `progress` is book-wide and must never be reused as the
406+
// within-chapter progression when restoring a location.
405407
if let charOff = json["charOffset"] as? Int {
406408
pendingCharOffset = charOff
407409
}
408-
initialProgression = json["progress"] as? Double
410+
initialProgression = json["chapterProgression"] as? Double
409411
}
410412
// Legacy format: { href, locations: { progression, totalProgression } }
411413
else {
@@ -449,6 +451,10 @@ public class NativeReaderEngine: ReaderEngine {
449451
self.navigateToOffsetInCurrentChapter(charOffset)
450452
}
451453
await self.paginateAllChapters()
454+
// Background pagination changes the book-wide page
455+
// denominator. Publish one corrected location when it
456+
// completes so progress and TOC page numbers agree.
457+
self.updateLocation()
452458
}
453459
}
454460
}
@@ -1253,10 +1259,25 @@ public class NativeReaderEngine: ReaderEngine {
12531259

12541260
/// Title for a chapter: prefer the readingOrder title, fall back to the TOC.
12551261
private func chapterTitleOrTocTitle(forSpineIndex spineIndex: Int) -> String? {
1256-
if let t = spineTitles[spineIndex], !t.isEmpty { return t }
1262+
if let t = spineTitles[spineIndex], !t.isEmpty {
1263+
// Conversion pipelines sometimes use a filename-like placeholder
1264+
// even when the publication provides a human TOC heading.
1265+
if isGeneratedChapterTitle(t), let toc = tocTitle(forSpineIndex: spineIndex) {
1266+
return toc
1267+
}
1268+
return t
1269+
}
12571270
return tocTitle(forSpineIndex: spineIndex)
12581271
}
12591272

1273+
private func isGeneratedChapterTitle(_ title: String) -> Bool {
1274+
let stem = (title as NSString).deletingPathExtension.lowercased()
1275+
for prefix in ["chapter-", "chapter_"] where stem.hasPrefix(prefix) {
1276+
if Int(stem.dropFirst(prefix.count)) != nil { return true }
1277+
}
1278+
return false
1279+
}
1280+
12601281
/// Find a TOC entry title that targets the given spine index.
12611282
private func tocTitle(forSpineIndex spineIndex: Int) -> String? {
12621283
guard let bundle = ccdBundle else { return nil }
@@ -1644,6 +1665,7 @@ public class NativeReaderEngine: ReaderEngine {
16441665
"type": "epub",
16451666
"spineIndex": currentSpineIndex,
16461667
"progress": location.totalProgression,
1668+
"chapterProgression": location.progression,
16471669
// Keep href and title for display/fallback
16481670
"href": location.href ?? "",
16491671
"title": location.title ?? ""
@@ -1820,4 +1842,3 @@ public class NativeReaderEngine: ReaderEngine {
18201842
}
18211843
}
18221844
}
1823-

‎Compendus/CCReader/Tests/CCReaderTests/CCDPackTests.swift‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,30 @@ final class CCDPackTests: XCTestCase {
136136
let dropped = CCDContentMapper.nodes(for: bundle.chapters[0]) { _ in nil }
137137
XCTAssertTrue(imageURLs(in: dropped).isEmpty, "unresolved image handle must not produce an image node")
138138
}
139+
140+
func testFailedReplacementPreservesPreviouslyInstalledPack() throws {
141+
let root = FileManager.default.temporaryDirectory
142+
.appendingPathComponent("ccd-transaction-\(UUID().uuidString)", isDirectory: true)
143+
let destination = root.appendingPathComponent("book", isDirectory: true)
144+
defer { try? FileManager.default.removeItem(at: root) }
145+
_ = try CCDPack.unpack(zipData: try packData(), into: destination)
146+
let originalManifest = try Data(contentsOf: destination.appendingPathComponent("manifest.ccd.json"))
147+
148+
XCTAssertThrowsError(try CCDPack.unpack(zipData: Data("not a zip".utf8), into: destination))
149+
XCTAssertEqual(
150+
try Data(contentsOf: destination.appendingPathComponent("manifest.ccd.json")),
151+
originalManifest,
152+
"a failed replacement must not destroy the last verified offline copy"
153+
)
154+
}
155+
156+
func testInstalledPackValidatorDetectsMissingReferencedResource() throws {
157+
let (manifest, resources, dir) = try unpackFixture()
158+
defer { try? FileManager.default.removeItem(at: dir) }
159+
let bundle = try CCDBundle.decode(from: Data(contentsOf: manifest))
160+
let urls = bundle.chapters.flatMap { imageURLs(in: CCDContentMapper.nodes(for: $0, resolveResource: resolver(resources))) }
161+
let first = try XCTUnwrap(urls.first(where: \.isFileURL))
162+
try FileManager.default.removeItem(at: first)
163+
XCTAssertThrowsError(try CCDPack.validateInstalledPack(at: dir))
164+
}
139165
}

‎Compendus/Compendus.xcodeproj/project.pbxproj‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
36EA78AEF80572E668BF61EC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3198DB17E9FF90C3612D619A /* Foundation.framework */; };
11+
A1B2C3D4E5F60718293A4B5C /* OfflineHardeningTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D4E5F60718293A4B5D /* OfflineHardeningTests.swift */; };
1112
AA00000001000002PTTSMDL0 /* KokoroModel in Resources */ = {isa = PBXBuildFile; fileRef = AA00000001000001PTTSMDL0 /* KokoroModel */; };
1213
F91E18572F3679000029A1F0 /* ZIPFoundation in Frameworks */ = {isa = PBXBuildFile; productRef = F91E18582F3679000029A1F0 /* ZIPFoundation */; };
1314
F91E18752F37E6270029A1F0 /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F91E18742F37E6270029A1F0 /* WidgetKit.framework */; };
@@ -53,6 +54,7 @@
5354
/* Begin PBXFileReference section */
5455
3198DB17E9FF90C3612D619A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS18.0.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
5556
3D17E6D8AC7806D10F5B92E4 /* CompendusTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CompendusTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
57+
A1B2C3D4E5F60718293A4B5D /* OfflineHardeningTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfflineHardeningTests.swift; sourceTree = "<group>"; };
5658
AA00000001000001PTTSMDL0 /* KokoroModel */ = {isa = PBXFileReference; lastKnownFileType = folder; path = KokoroModel; sourceTree = "<group>"; };
5759
F91E180A2F366F9C0029A1F0 /* Compendus.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Compendus.app; sourceTree = BUILT_PRODUCTS_DIR; };
5860
F91E18732F37E6270029A1F0 /* CompendusWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = CompendusWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -133,6 +135,7 @@
133135
0A957213575147E9065818B7 /* CompendusTests */ = {
134136
isa = PBXGroup;
135137
children = (
138+
A1B2C3D4E5F60718293A4B5D /* OfflineHardeningTests.swift */,
136139
);
137140
path = CompendusTests;
138141
sourceTree = SOURCE_ROOT;
@@ -324,6 +327,7 @@
324327
isa = PBXSourcesBuildPhase;
325328
buildActionMask = 2147483647;
326329
files = (
330+
A1B2C3D4E5F60718293A4B5C /* OfflineHardeningTests.swift in Sources */,
327331
);
328332
runOnlyForDeploymentPostprocessing = 0;
329333
};
@@ -424,10 +428,12 @@
424428
ENABLE_USER_SELECTED_FILES = readonly;
425429
GENERATE_INFOPLIST_FILE = YES;
426430
INFOPLIST_FILE = Compendus/Info.plist;
431+
INFOPLIST_KEY_NSSpeechRecognitionUsageDescription = "Compendus uses speech recognition to generate transcripts for audiobook lyrics display.";
427432
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = NO;
428433
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = NO;
429434
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
430435
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphonesimulator*]" = YES;
436+
INFOPLIST_KEY_UILaunchStoryboardName = "Launch Screen";
431437
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphoneos*]" = UIStatusBarStyleDefault;
432438
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
433439
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
@@ -438,7 +444,7 @@
438444
MACOSX_DEPLOYMENT_TARGET = 26.2;
439445
MARKETING_VERSION = 1.0;
440446
OTHER_LDFLAGS = "-lc++";
441-
PRODUCT_BUNDLE_IDENTIFIER = com.gabrielcsapo.Compendus;
447+
PRODUCT_BUNDLE_IDENTIFIER = com.csapo.Compendus;
442448
PRODUCT_NAME = "$(TARGET_NAME)";
443449
PROVISIONING_PROFILE_SPECIFIER = "";
444450
REGISTER_APP_GROUPS = YES;
@@ -483,10 +489,12 @@
483489
ENABLE_USER_SELECTED_FILES = readonly;
484490
GENERATE_INFOPLIST_FILE = YES;
485491
INFOPLIST_FILE = Compendus/Info.plist;
492+
INFOPLIST_KEY_NSSpeechRecognitionUsageDescription = "Compendus uses speech recognition to generate transcripts for audiobook lyrics display.";
486493
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = NO;
487494
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = NO;
488495
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
489496
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphonesimulator*]" = YES;
497+
INFOPLIST_KEY_UILaunchStoryboardName = "Launch Screen";
490498
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphoneos*]" = UIStatusBarStyleDefault;
491499
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
492500
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
@@ -497,7 +505,7 @@
497505
MACOSX_DEPLOYMENT_TARGET = 26.2;
498506
MARKETING_VERSION = 1.0;
499507
OTHER_LDFLAGS = "-lc++";
500-
PRODUCT_BUNDLE_IDENTIFIER = com.gabrielcsapo.Compendus;
508+
PRODUCT_BUNDLE_IDENTIFIER = com.csapo.Compendus;
501509
PRODUCT_NAME = "$(TARGET_NAME)";
502510
PROVISIONING_PROFILE_SPECIFIER = "";
503511
REGISTER_APP_GROUPS = YES;
@@ -550,7 +558,7 @@
550558
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
551559
COPY_PHASE_STRIP = NO;
552560
DEBUG_INFORMATION_FORMAT = dwarf;
553-
DEVELOPMENT_TEAM = LXUHPDSG76;
561+
DEVELOPMENT_TEAM = PYQ4EQ49YL;
554562
ENABLE_STRICT_OBJC_MSGSEND = YES;
555563
ENABLE_TESTABILITY = YES;
556564
ENABLE_USER_SCRIPT_SANDBOXING = YES;
@@ -612,7 +620,7 @@
612620
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
613621
COPY_PHASE_STRIP = NO;
614622
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
615-
DEVELOPMENT_TEAM = LXUHPDSG76;
623+
DEVELOPMENT_TEAM = PYQ4EQ49YL;
616624
ENABLE_NS_ASSERTIONS = NO;
617625
ENABLE_STRICT_OBJC_MSGSEND = YES;
618626
ENABLE_USER_SCRIPT_SANDBOXING = YES;
@@ -652,7 +660,7 @@
652660
"@executable_path/../../Frameworks",
653661
);
654662
MARKETING_VERSION = 1.0;
655-
PRODUCT_BUNDLE_IDENTIFIER = com.gabrielcsapo.Compendus.CompendusWidget;
663+
PRODUCT_BUNDLE_IDENTIFIER = com.csapo.Compendus.CompendusWidget;
656664
PRODUCT_NAME = "$(TARGET_NAME)";
657665
PROVISIONING_PROFILE_SPECIFIER = "";
658666
SDKROOT = auto;
@@ -689,7 +697,7 @@
689697
"@executable_path/../../Frameworks",
690698
);
691699
MARKETING_VERSION = 1.0;
692-
PRODUCT_BUNDLE_IDENTIFIER = com.gabrielcsapo.Compendus.CompendusWidget;
700+
PRODUCT_BUNDLE_IDENTIFIER = com.csapo.Compendus.CompendusWidget;
693701
PRODUCT_NAME = "$(TARGET_NAME)";
694702
PROVISIONING_PROFILE_SPECIFIER = "";
695703
SDKROOT = auto;

0 commit comments

Comments
 (0)