Prevent iCloud backups of downloads

This commit is contained in:
ronaldheft 2022-08-11 13:46:30 -04:00
parent 02eabb82c1
commit 8edeefc1cd
3 changed files with 27 additions and 9 deletions

View file

@ -11,7 +11,7 @@ import Capacitor
@objc(AbsDownloader)
public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
static let downloadsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
static private let downloadsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
typealias DownloadProgressHandler = (_ downloadItem: DownloadItem, _ downloadItemPart: inout DownloadItemPart) throws -> Void
@ -307,16 +307,34 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
let itemDirectory = item.id
NSLog("ITEM DIR \(itemDirectory)")
do {
try FileManager.default.createDirectory(at: AbsDownloader.downloadsDirectory.appendingPathComponent(itemDirectory), withIntermediateDirectories: true)
} catch {
NSLog("Failed to CREATE LI DIRECTORY \(error)")
guard AbsDownloader.itemDownloadFolder(path: itemDirectory) != nil else {
NSLog("Failed to CREATE LI DIRECTORY \(itemDirectory)")
throw LibraryItemDownloadError.failedDirectory
}
return itemDirectory
}
static func itemDownloadFolder(path: String) -> URL? {
do {
var itemFolder = AbsDownloader.downloadsDirectory.appendingPathComponent(path)
if !FileManager.default.fileExists(atPath: itemFolder.path) {
try FileManager.default.createDirectory(at: itemFolder, withIntermediateDirectories: true)
}
// Make sure we don't backup download files to iCloud
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try itemFolder.setResourceValues(resourceValues)
return itemFolder
} catch {
NSLog("Failed to CREATE LI DIRECTORY \(error)")
return nil
}
}
}
enum LibraryItemDownloadError: String, Error {

View file

@ -82,7 +82,7 @@ struct DownloadItemPart: Realmable, Codable {
var destinationUri: String?
var destinationURL: URL? {
if let destinationUri = self.destinationUri {
return AbsDownloader.downloadsDirectory.appendingPathComponent(destinationUri)
return AbsDownloader.itemDownloadFolder(path: destinationUri)!
} else {
return nil
}

View file

@ -25,7 +25,7 @@ struct LocalLibraryItem: Realmable, Codable {
var contentUrl: String? {
if let path = _contentUrl {
return AbsDownloader.downloadsDirectory.appendingPathComponent(path).absoluteString
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString
} else {
return nil
}
@ -33,7 +33,7 @@ struct LocalLibraryItem: Realmable, Codable {
var coverContentUrl: String? {
if let path = self._coverContentUrl {
return AbsDownloader.downloadsDirectory.appendingPathComponent(path).absoluteString
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString
} else {
return nil
}
@ -93,7 +93,7 @@ struct LocalFile: Realmable, Codable {
var size: Int = 0
var contentUrl: String {
return AbsDownloader.downloadsDirectory.appendingPathComponent(_contentUrl).absoluteString
return AbsDownloader.itemDownloadFolder(path: _contentUrl)!.absoluteString
}
static func primaryKey() -> String? {