Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Piperly/Models/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,38 @@ import Foundation

struct Book: Identifiable, Codable {
let id: UUID
let contentIdentity: String
let title: String
let author: String
let fileName: String
var coverImageName: String?

init(
id: UUID = UUID(),
contentIdentity: String,
title: String,
author: String,
fileName: String,
coverImageName: String? = nil
) {
self.id = id
self.contentIdentity = contentIdentity
self.title = title
self.author = author
self.fileName = fileName
self.coverImageName = coverImageName
}

/// Legacy blobs predate `contentIdentity`; default it to "" so the book
/// survives decode. BookStore backfills the real hash (and migrates the
/// filename) on load. See BookStore.backfillContentIdentities().
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
id = try c.decode(UUID.self, forKey: .id)
contentIdentity = try c.decodeIfPresent(String.self, forKey: .contentIdentity) ?? ""
title = try c.decode(String.self, forKey: .title)
author = try c.decode(String.self, forKey: .author)
fileName = try c.decode(String.self, forKey: .fileName)
coverImageName = try c.decodeIfPresent(String.self, forKey: .coverImageName)
}
}
231 changes: 197 additions & 34 deletions Piperly/Services/BookStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import CryptoKit
import Foundation
import SwiftUI
@preconcurrency import ReadiumShared
Expand All @@ -30,6 +31,7 @@ class BookStore: ObservableObject {

private let documentsURL: URL
private let coversURL: URL
private let importSnapshotsURL: URL
private let userDefaults: UserDefaults
private let booksKey = "piperly_books"
private let profilesKey = "piperly_reader_profiles"
Expand All @@ -40,6 +42,8 @@ class BookStore: ObservableObject {
private var debouncedBooksSave: Task<Void, Never>?
private var debouncedReadingStatesSave: Task<Void, Never>?
private var debouncedWordsSave: Task<Void, Never>?
private var importsByContentIdentity: [String: Task<Book, Error>] = [:]
nonisolated private static let fileReadChunkSize = 1_048_576

/// Keys whose persisted bytes were non-empty but decoded to nothing (total
/// corruption). We refuse to overwrite them with an empty array so a bad
Expand Down Expand Up @@ -84,16 +88,29 @@ class BookStore: ObservableObject {
}
}

init(userDefaults: UserDefaults = .standard) {
convenience init(userDefaults: UserDefaults = .standard) {
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
self.documentsURL = docs.appendingPathComponent("Books", isDirectory: true)
self.coversURL = docs.appendingPathComponent("Covers", isDirectory: true)
self.init(
documentsURL: docs.appendingPathComponent("Books", isDirectory: true),
coversURL: docs.appendingPathComponent("Covers", isDirectory: true),
userDefaults: userDefaults
)
}

init(documentsURL: URL, coversURL: URL, userDefaults: UserDefaults) {
self.documentsURL = documentsURL
self.coversURL = coversURL
self.importSnapshotsURL = documentsURL.deletingLastPathComponent()
.appendingPathComponent(".PiperlyImportSnapshots", isDirectory: true)
self.userDefaults = userDefaults

try? FileManager.default.createDirectory(at: documentsURL, withIntermediateDirectories: true)
try? FileManager.default.createDirectory(at: coversURL, withIntermediateDirectories: true)
try? FileManager.default.createDirectory(at: importSnapshotsURL, withIntermediateDirectories: true)
Self.clearStaleSnapshots(in: importSnapshotsURL)
Self.excludeFromBackup(documentsURL)
Self.excludeFromBackup(coversURL)
Self.excludeFromBackup(importSnapshotsURL)
loadBooks()
loadProfiles()
loadSelectedProfileID()
Expand All @@ -105,7 +122,7 @@ class BookStore: ObservableObject {

/// Books are re-importable and covers are re-derivable via `backfillCovers()`,
/// so neither belongs in iCloud backups (Apple Data Storage Guidelines).
private nonisolated static func excludeFromBackup(_ url: URL) {
nonisolated private static func excludeFromBackup(_ url: URL) {
var url = url
var values = URLResourceValues()
values.isExcludedFromBackup = true
Expand Down Expand Up @@ -329,18 +346,79 @@ class BookStore: ObservableObject {
let accessing = sourceURL.startAccessingSecurityScopedResource()
defer { if accessing { sourceURL.stopAccessingSecurityScopedResource() } }

let originalName = sourceURL.lastPathComponent
let storedFileName = Self.uniqueFileName(for: originalName)
let snapshotsURL = importSnapshotsURL
let snapshotURL = try await Task.detached(priority: .userInitiated) {
try Self.snapshotSource(at: sourceURL, in: snapshotsURL)
}.value
defer { try? FileManager.default.removeItem(at: snapshotURL) }

return try await importSnapshot(snapshotURL, originalName: sourceURL.lastPathComponent)
}

private func importSnapshot(_ snapshotURL: URL, originalName: String) async throws -> Book {
let contentIdentity = try await Task.detached(priority: .userInitiated) {
try Self.contentIdentity(for: snapshotURL)
}.value

if let importTask = importsByContentIdentity[contentIdentity] {
return try await importTask.value
}

let importTask = Task {
try await finishImport(
snapshotURL,
originalName: originalName,
contentIdentity: contentIdentity
)
}
importsByContentIdentity[contentIdentity] = importTask

do {
let book = try await importTask.value
importsByContentIdentity[contentIdentity] = nil
return book
} catch {
importsByContentIdentity[contentIdentity] = nil
throw error
}
}

private func finishImport(
_ snapshotURL: URL,
originalName: String,
contentIdentity: String
) async throws -> Book {
let storedFileName = Self.storageFileName(for: contentIdentity)
let destURL = documentsURL.appendingPathComponent(storedFileName)

try FileManager.default.copyItem(at: sourceURL, to: destURL)
if let existingBook = books.first(where: { $0.contentIdentity == contentIdentity }) {
if FileManager.default.fileExists(atPath: destURL.path) {
let filesMatch = try await Task.detached(priority: .userInitiated) {
try Self.filesHaveEqualBytes(snapshotURL, destURL)
}.value
guard filesMatch else { throw BookStoreError.contentIdentityCollision }
} else {
_ = try await openPublication(at: snapshotURL)
try await publishSnapshot(snapshotURL, to: destURL)
}
return existingBook
}

guard let (title, author, coverName) = try? await parseMetadataAndCover(from: destURL) else {
try? FileManager.default.removeItem(at: destURL)
guard let (title, author, coverName) = try? await parseMetadataAndCover(from: snapshotURL) else {
throw BookStoreError.unreadableBook
}

do {
try await publishSnapshot(snapshotURL, to: destURL)
} catch {
if let coverName {
try? FileManager.default.removeItem(at: coversURL.appendingPathComponent(coverName))
}
throw error
}

let book = Book(
contentIdentity: contentIdentity,
title: title ?? URL(fileURLWithPath: originalName).deletingPathExtension().lastPathComponent,
author: author ?? "Unknown Author",
fileName: storedFileName,
Expand All @@ -352,10 +430,76 @@ class BookStore: ObservableObject {
return book
}

/// Builds a collision-free destination filename by prefixing a UUID, while
/// preserving the original name (and extension) for readability.
nonisolated static func uniqueFileName(for originalName: String) -> String {
"\(UUID().uuidString)-\(originalName)"
private func publishSnapshot(_ snapshotURL: URL, to destURL: URL) async throws {
if FileManager.default.fileExists(atPath: destURL.path) {
let filesMatch = try await Task.detached(priority: .userInitiated) {
try Self.filesHaveEqualBytes(snapshotURL, destURL)
}.value
guard filesMatch else { throw BookStoreError.contentIdentityCollision }
return
}

do {
try FileManager.default.moveItem(at: snapshotURL, to: destURL)
} catch {
guard FileManager.default.fileExists(atPath: destURL.path) else { throw error }
let filesMatch = try await Task.detached(priority: .userInitiated) {
try Self.filesHaveEqualBytes(snapshotURL, destURL)
}.value
guard filesMatch else { throw BookStoreError.contentIdentityCollision }
}
}

nonisolated private static func snapshotSource(at sourceURL: URL, in directoryURL: URL) throws -> URL {
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
let snapshotURL = directoryURL.appendingPathComponent("\(UUID().uuidString).epub")
do {
try FileManager.default.copyItem(at: sourceURL, to: snapshotURL)
return snapshotURL
} catch {
try? FileManager.default.removeItem(at: snapshotURL)
throw error
}
}

nonisolated private static func clearStaleSnapshots(in directoryURL: URL) {
guard let staleURLs = try? FileManager.default.contentsOfDirectory(
at: directoryURL,
includingPropertiesForKeys: nil
) else { return }

for staleURL in staleURLs {
try? FileManager.default.removeItem(at: staleURL)
}
}

nonisolated static func contentIdentity(for url: URL) throws -> String {
let handle = try FileHandle(forReadingFrom: url)
defer { try? handle.close() }

var hasher = SHA256()
while let bytes = try handle.read(upToCount: fileReadChunkSize), !bytes.isEmpty {
hasher.update(data: bytes)
}
return hasher.finalize().map { String(format: "%02x", $0) }.joined()
}

nonisolated static func storageFileName(for contentIdentity: String) -> String {
"\(contentIdentity).epub"
}

nonisolated private static func filesHaveEqualBytes(_ firstURL: URL, _ secondURL: URL) throws -> Bool {
let firstHandle = try FileHandle(forReadingFrom: firstURL)
defer { try? firstHandle.close() }
let secondHandle = try FileHandle(forReadingFrom: secondURL)
defer { try? secondHandle.close() }

while true {
let firstBytes = try firstHandle.read(upToCount: fileReadChunkSize)
let secondBytes = try secondHandle.read(upToCount: fileReadChunkSize)
guard firstBytes == secondBytes else { return false }
guard let firstBytes, !firstBytes.isEmpty else { return true }
}
}

func bookURL(for book: Book) -> URL {
Expand Down Expand Up @@ -517,27 +661,7 @@ class BookStore: ObservableObject {
}

private func importBundledBook(from bundleURL: URL) async throws -> Book {
let fileName = bundleURL.lastPathComponent
let destURL = documentsURL.appendingPathComponent(fileName)

if !FileManager.default.fileExists(atPath: destURL.path) {
try FileManager.default.copyItem(at: bundleURL, to: destURL)
}

let (title, author, coverName) = (try? await parseMetadataAndCover(from: destURL)) ?? (nil, nil, nil)

let book = Book(
title: title ?? URL(fileURLWithPath: fileName).deletingPathExtension().lastPathComponent,
author: author ?? "Unknown Author",
fileName: fileName,
coverImageName: coverName
)

if !books.contains(where: { $0.fileName == fileName }) {
books.append(book)
saveBooks()
}
return book
try await importBook(from: bundleURL)
}

func deleteBook(_ book: Book) {
Expand Down Expand Up @@ -594,6 +718,44 @@ class BookStore: ObservableObject {
return name
}

/// Legacy books decoded with an empty `contentIdentity` (persisted before
/// stable identity existed). Hash the on-disk file, adopt the real identity,
/// and migrate the file to the deterministic `<hash>.epub` name so dedupe
/// and asset sync work. Legacy files still resolve via their old `fileName`
/// until migrated, so nothing breaks before this runs.
func backfillContentIdentities() async {
var changed = false
for i in books.indices where books[i].contentIdentity.isEmpty {
let currentURL = bookURL(for: books[i])
guard FileManager.default.fileExists(atPath: currentURL.path),
let identity = try? await Task.detached(priority: .utility, operation: {
try Self.contentIdentity(for: currentURL)
}).value else { continue }

let storedName = Self.storageFileName(for: identity)
let destURL = documentsURL.appendingPathComponent(storedName)
if currentURL.path != destURL.path {
if FileManager.default.fileExists(atPath: destURL.path) {
// Deterministic name already present (same bytes): drop the
// duplicate legacy file rather than clobber the canonical one.
try? FileManager.default.removeItem(at: currentURL)
} else {
try? FileManager.default.moveItem(at: currentURL, to: destURL)
}
}
books[i] = Book(
id: books[i].id,
contentIdentity: identity,
title: books[i].title,
author: books[i].author,
fileName: storedName,
coverImageName: books[i].coverImageName
)
changed = true
}
if changed { saveBooks() }
}

func backfillCovers() async {
var changed = false
for i in books.indices where books[i].coverImageName == nil {
Expand All @@ -611,6 +773,7 @@ class BookStore: ObservableObject {
enum BookStoreError: Error {
case invalidURL
case unreadableBook
case contentIdentityCollision
}

/// Decodes to `nil` instead of throwing, so a bad element in an array doesn't
Expand Down
1 change: 1 addition & 0 deletions Piperly/Views/Library/LibraryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct LibraryView: View {
}
}
.task {
await bookStore.backfillContentIdentities()
await bookStore.backfillCovers()
}
.fileImporter(
Expand Down
Loading
Loading