Skip to content

Open source CapCut alternative.

TypeScript

fix(ios): read files by URL so recordings upload and thumbnails paint

David Le26d391111618
diff --git a/apps/ios/Donkey/Controllers/CameraController.swift b/apps/ios/Donkey/Controllers/CameraController.swift
index f8050c4d..11f80c7a 100644
--- a/apps/ios/Donkey/Controllers/CameraController.swift
+++ b/apps/ios/Donkey/Controllers/CameraController.swift
@@ -305,7 +305,7 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco
error: (any Error)?
) {
// A stop can report a benign error while still writing a usable file.
- let wroteFile = FileManager.default.fileExists(atPath: outputFileURL.path())
+ let wroteFile = FileManager.default.fileExists(atPath: outputFileURL.localPath)
guard wroteFile else {
events?(.recordingFailed)
return
diff --git a/apps/ios/Donkey/Controllers/CutCloudClient.swift b/apps/ios/Donkey/Controllers/CutCloudClient.swift
index 631555f7..5c39d7fc 100644
--- a/apps/ios/Donkey/Controllers/CutCloudClient.swift
+++ b/apps/ios/Donkey/Controllers/CutCloudClient.swift
@@ -347,11 +347,11 @@ extension CutCloudClient: CloudProjectsServicing {
try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
let stamp = Int(project.updatedAt.timeIntervalSince1970 * 1000)
let file = directory.appending(path: "\(project.id)-\(stamp).jpg")
- if FileManager.default.fileExists(atPath: file.path()) { return file }
+ if FileManager.default.fileExists(atPath: file.localPath) { return file }
guard let image = await thumbnailImage(for: project),
let data = image.jpegData(compressionQuality: 0.8) else { return nil }
// Versions of this project from before the edit leave with the refresh.
- let stale = (try? FileManager.default.contentsOfDirectory(atPath: directory.path()))?
+ let stale = (try? FileManager.default.contentsOfDirectory(atPath: directory.localPath))?
.filter { $0.hasPrefix("\(project.id)-") } ?? []
for name in stale {
try? FileManager.default.removeItem(at: directory.appending(path: name))
@@ -418,9 +418,7 @@ extension CutCloudClient {
/// disk, probed dimensions, and the stored thumbnail as the poster.
static func uploadPayload(for recording: Recording, media: MediaModel) async -> LibraryUpload? {
let fileURL = media.movieURL(for: recording)
- let size = ((try? FileManager.default.attributesOfItem(atPath: fileURL.path()))?[.size] as? NSNumber)?
- .int64Value ?? 0
- guard size > 0 else { return nil }
+ guard let size = fileURL.fileByteCount else { return nil }
var width: Int?
var height: Int?
let asset = AVURLAsset(url: fileURL)
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/FileURL.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/FileURL.swift
new file mode 100644
index 00000000..984a7574
--- /dev/null
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/FileURL.swift
@@ -0,0 +1,19 @@
+import Foundation
+
+// Every path-taking API in the app reads through these two properties.
+// `URL.path()` percent-encodes by default, and the app's own container is
+// rooted at "Application Support" — a name with a space in it — so the encoded
+// form names nothing on disk: image loads come back empty and file sizes read
+// as zero, which is silent in both cases.
+
+extension URL {
+ /// The filesystem path, decoded, for APIs that take a path string.
+ nonisolated public var localPath: String { path(percentEncoded: false) }
+
+ /// Bytes on disk, or nil when the file is missing or empty.
+ nonisolated public var fileByteCount: Int64? {
+ guard let size = try? resourceValues(forKeys: [.fileSizeKey]).fileSize,
+ size > 0 else { return nil }
+ return Int64(size)
+ }
+}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift
index 7598b2b6..2e494059 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift
@@ -338,7 +338,7 @@ public final class SyncEngine {
guard online, !storageFull else { continue }
guard ((try? journal.inspirationRemoteAssetId(item.id)) ?? nil) == nil else { continue }
guard let fileURL = ideas?.mediaURL(fileName: fileName),
- let bytes = fileSize(fileURL) else { continue }
+ let bytes = fileURL.fileByteCount else { continue }
let upload = LibraryUpload(
fileURL: fileURL,
fileName: fileName,
@@ -365,12 +365,6 @@ public final class SyncEngine {
}
}
- private nonisolated func fileSize(_ url: URL) -> Int64? {
- let size = (try? FileManager.default.attributesOfItem(atPath: url.path()))?[.size] as? NSNumber
- guard let size, size.int64Value > 0 else { return nil }
- return size.int64Value
- }
-
// MARK: Storage
private func refreshStorage() async {
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
index 4083e86e..c0dec77c 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
@@ -98,7 +98,7 @@ struct CameraScreen<CameraPreview: View>: View {
} label: {
ZStack {
if let url = media.thumbnailURL(for: recording),
- let image = UIImage(contentsOfFile: url.path()) {
+ let image = UIImage(contentsOfFile: url.localPath) {
Image(uiImage: image)
.resizable()
.scaledToFill()
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
index 0d848774..40100719 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
@@ -225,7 +225,7 @@ struct InspirationCard: View {
Group {
if isVideo {
VideoPlayer(player: AVPlayer(url: url))
- } else if let image = UIImage(contentsOfFile: url.path()) {
+ } else if let image = UIImage(contentsOfFile: url.localPath) {
Image(uiImage: image)
.resizable()
.scaledToFill()
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift
index 100db948..a6696bd7 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift
@@ -77,7 +77,7 @@ struct RecordingCard: View {
var body: some View {
ZStack {
if let url = media.thumbnailURL(for: recording),
- let image = UIImage(contentsOfFile: url.path()) {
+ let image = UIImage(contentsOfFile: url.localPath) {
Image(uiImage: image)
.resizable()
.scaledToFill()
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift
index 5353bdc0..0a8a2e5f 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift
@@ -58,7 +58,7 @@ struct ProjectCard: View {
Button(action: onPlay) {
ZStack {
if let thumbnail = project.thumbnail,
- let image = UIImage(contentsOfFile: thumbnail.path()) {
+ let image = UIImage(contentsOfFile: thumbnail.localPath) {
Image(uiImage: image)
.resizable()
.scaledToFill()
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index 9a3dc3a1..e9a0e29f 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -331,6 +331,17 @@ import Testing
#expect(rig.cloud.notes[note.id] != nil)
}
+ @Test func inspirationMediaUploadsOnce() async throws {
+ // The bytes sit under Application Support, whose name carries a
+ // space: the engine reads their size off the URL, not a path string.
+ let rig = try makeRig()
+ rig.ideas.addInspiration(mediaData: Data(repeating: 3, count: 64), isVideo: false)
+ await rig.engine.run()
+ #expect(rig.cloud.uploads.count == 1)
+ await rig.engine.run()
+ #expect(rig.cloud.uploads.count == 1)
+ }
+
@Test func offlineMovesNothing() async throws {
let rig = try makeRig()
rig.engine.network = .offline
@@ -344,6 +355,32 @@ import Testing
}
}
+@Suite struct FileURLTests {
+ /// The app's own container sits under "Application Support", so a path
+ /// with a space in it is the normal case, not the edge one.
+ func spacedDirectory() throws -> URL {
+ let directory = FileManager.default.temporaryDirectory
+ .appending(path: "Application Support \(UUID().uuidString)", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
+ return directory
+ }
+
+ @Test func bytesReadThroughASpacedPath() throws {
+ let file = try spacedDirectory().appending(path: "clip.mov")
+ try Data(repeating: 7, count: 512).write(to: file)
+ #expect(file.fileByteCount == 512)
+ #expect(FileManager.default.fileExists(atPath: file.localPath))
+ }
+
+ @Test func missingAndEmptyFilesReadAsNoBytes() throws {
+ let directory = try spacedDirectory()
+ let empty = directory.appending(path: "empty.mov")
+ try Data().write(to: empty)
+ #expect(empty.fileByteCount == nil)
+ #expect(directory.appending(path: "gone.mov").fileByteCount == nil)
+ }
+}
+
@Suite struct DurationTests {
@Test func formatting() {
#expect(formattedDuration(0) == "0:00")
diff --git a/docs/guides/swift.md b/docs/guides/swift.md
index 4d555ffa..11367a78 100644
--- a/docs/guides/swift.md
+++ b/docs/guides/swift.md
@@ -103,6 +103,10 @@ Mac users expect:
Feature-layer models stay plain structs; persistence types stay at the edge.
- Network with URLSession, async/await, and Codable. Decode at the boundary
into domain types; wire formats never leak past the client layer.
+- Keep files as `URL` end to end. `URL.path()` percent-encodes, and app
+ storage is rooted at "Application Support" — a name with a space — so an
+ encoded path names nothing on disk and every read fails silently. Where an
+ API insists on a string, hand it `localPath`.
## Tests