fix(iOS): read file data into memory BEFORE revoking security-scoped access, prevents exists=false

This commit is contained in:
yumoqing 2026-07-03 08:22:14 +08:00
parent 6503712be9
commit 3ed5696ae0

View File

@ -771,36 +771,43 @@ final class PlayerBridge: ObservableObject {
/// iOS /// iOS
func handleFileImport(_ urls: [URL]) { func handleFileImport(_ urls: [URL]) {
diagLog("handleFileImport: \(urls.count) URLs") diagLog("handleFileImport: \(urls.count) URLs")
let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
for url in urls { for url in urls {
diagLog("processing: \(url.lastPathComponent)") diagLog("processing: \(url.lastPathComponent)")
let didStartAccessing = url.startAccessingSecurityScopedResource() let didStartAccessing = url.startAccessingSecurityScopedResource()
diagLog("startAccessingSecurityScopedResource: \(didStartAccessing)") diagLog("startAccessingSecurityScopedResource: \(didStartAccessing)")
defer {
// 访
let fileData: Data
do {
fileData = try Data(contentsOf: url)
diagLog("read \(fileData.count) bytes from source")
} catch {
diagLog("read failed: \(error.localizedDescription)")
if didStartAccessing { url.stopAccessingSecurityScopedResource() } if didStartAccessing { url.stopAccessingSecurityScopedResource() }
continue
} }
// app Documents security-scoped URL defer // 访
let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] if didStartAccessing { url.stopAccessingSecurityScopedResource() }
let localURL = docsDir.appendingPathComponent(url.lastPathComponent)
// Documents
let baseName = url.deletingPathExtension().lastPathComponent
let ext = url.pathExtension
var localURL = docsDir.appendingPathComponent(url.lastPathComponent)
if FileManager.default.fileExists(atPath: localURL.path) {
let ts = Int(Date().timeIntervalSince1970)
localURL = docsDir.appendingPathComponent("\(baseName)_\(ts).\(ext)")
}
do { do {
// try fileData.write(to: localURL)
if FileManager.default.fileExists(atPath: localURL.path) { diagLog("written \(fileData.count) bytes to: \(localURL.lastPathComponent)")
let ts = Int(Date().timeIntervalSince1970) addItem(url: localURL, name: url.lastPathComponent, type: "file")
let nameWithoutExt = url.deletingPathExtension().lastPathComponent
let ext = url.pathExtension
let newURL = docsDir.appendingPathComponent("\(nameWithoutExt)_\(ts).\(ext)")
try FileManager.default.copyItem(at: url, to: newURL)
diagLog("copied to: \(newURL.lastPathComponent)")
addItem(url: newURL, name: url.lastPathComponent, type: "file")
} else {
try FileManager.default.copyItem(at: url, to: localURL)
diagLog("copied to: \(localURL.lastPathComponent)")
addItem(url: localURL, name: url.lastPathComponent, type: "file")
}
} catch { } catch {
diagLog("copy failed: \(error.localizedDescription)") diagLog("write failed: \(error.localizedDescription)")
addItem(url: url, name: url.lastPathComponent, type: "file") showToast("Failed to import: \(url.lastPathComponent)")
} }
} }
} }