Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when importing files after some delay #1134

Merged
merged 2 commits into from
Apr 26, 2024
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
11 changes: 10 additions & 1 deletion BookPlayer/Coordinators/LibraryListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,17 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat
}

func processFiles(urls: [URL]) {
let temporaryDirectoryPath = FileManager.default.temporaryDirectory.absoluteString
let documentsFolder = DataManager.getDocumentsFolderURL()

for url in urls {
self.importManager.process(url)
/// At some point (iOS 17?), the OS stopped sending the picked files to the Documents/Inbox folder, instead
/// it's now sent to a temp folder that can't be relied on to keep the file existing until the import is finished
if url.absoluteString.contains(temporaryDirectoryPath) {
try! FileManager.default.moveItem(at: url, to: documentsFolder)
} else {
importManager.process(url)
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion BookPlayer/Library/ItemList Screen/ItemListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,20 @@ class ItemListViewModel: ViewModelProtocol {
// MARK: - Import related functions
extension ItemListViewModel {
func handleNewFiles(_ urls: [URL]) {
let temporaryDirectoryPath = FileManager.default.temporaryDirectory.absoluteString
let documentsFolder = DataManager.getDocumentsFolderURL()

for url in urls {
importManager.process(url)
/// At some point (iOS 17?), the OS stopped sending the picked files to the Documents/Inbox folder, instead
/// it's now sent to a temp folder that can't be relied on to keep the file existing until the import is finished
if url.absoluteString.contains(temporaryDirectoryPath) {
let destinationURL = documentsFolder.appendingPathComponent(url.lastPathComponent)
if !FileManager.default.fileExists(atPath: destinationURL.path) {
try! FileManager.default.copyItem(at: url, to: destinationURL)
}
} else {
importManager.process(url)
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions BookPlayer/PrivacyInfo.xcprivacy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<dict>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>3B52.1</string>
</array>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryFileTimestamp</string>
</dict>
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryDiskSpace</string>
Expand Down