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) @objc(AbsDownloader)
public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate { 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 typealias DownloadProgressHandler = (_ downloadItem: DownloadItem, _ downloadItemPart: inout DownloadItemPart) throws -> Void
@ -307,16 +307,34 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
let itemDirectory = item.id let itemDirectory = item.id
NSLog("ITEM DIR \(itemDirectory)") NSLog("ITEM DIR \(itemDirectory)")
do { guard AbsDownloader.itemDownloadFolder(path: itemDirectory) != nil else {
try FileManager.default.createDirectory(at: AbsDownloader.downloadsDirectory.appendingPathComponent(itemDirectory), withIntermediateDirectories: true) NSLog("Failed to CREATE LI DIRECTORY \(itemDirectory)")
} catch {
NSLog("Failed to CREATE LI DIRECTORY \(error)")
throw LibraryItemDownloadError.failedDirectory throw LibraryItemDownloadError.failedDirectory
} }
return itemDirectory 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 { enum LibraryItemDownloadError: String, Error {

View file

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

View file

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