Fix iCloud downloads

This commit is contained in:
Ali 2022-04-01 23:40:42 +04:00
parent b3678917c8
commit 9da972bea3

View File

@ -479,7 +479,7 @@ public func presentCustomNotificationSoundFilePicker(context: AccountContext, co
return return
} }
do { /*do {
let resources = try url.resourceValues(forKeys:[.fileSizeKey]) let resources = try url.resourceValues(forKeys:[.fileSizeKey])
if let size = resources.fileSize { if let size = resources.fileSize {
if Int32(size) > settings.maxSize { if Int32(size) > settings.maxSize {
@ -491,34 +491,48 @@ public func presentCustomNotificationSoundFilePicker(context: AccountContext, co
} catch { } catch {
print("Error: \(error)") print("Error: \(error)")
return return
}*/
if !url.startAccessingSecurityScopedResource() {
Logger.shared.log("NotificationSoundSelection", "startAccessingSecurityScopedResource failed")
return
} }
let coordinator = NSFileCoordinator(filePresenter: nil) let coordinator = NSFileCoordinator(filePresenter: nil)
var error: NSError? var error: NSError?
coordinator.coordinate(readingItemAt: url, options: .forUploading, error: &error, byAccessor: { url in coordinator.coordinate(readingItemAt: url, options: .forUploading, error: &error, byAccessor: { url in
Queue.mainQueue().async { Queue.mainQueue().async {
do {
let asset = AVAsset(url: url) let asset = AVAsset(url: url)
guard let data = try? Data(contentsOf: url) else { let data = try Data(contentsOf: url)
return
}
if data.count > settings.maxSize { if data.count > settings.maxSize {
//TODO:localize //TODO:localize
presentUndo(.info(title: "Audio is too large", text: "The file is over \(dataSizeString(Int64(settings.maxSize), formatting: DataSizeStringFormatting(presentationData: presentationData))).")) presentUndo(.info(title: "Audio is too large", text: "The file is over \(dataSizeString(Int64(settings.maxSize), formatting: DataSizeStringFormatting(presentationData: presentationData)))."))
url.stopAccessingSecurityScopedResource()
return return
} }
asset.loadValuesAsynchronously(forKeys: ["tracks", "duration"], completionHandler: { asset.loadValuesAsynchronously(forKeys: ["tracks", "duration"], completionHandler: {
if asset.statusOfValue(forKey: "duration", error: nil) != .loaded { if asset.statusOfValue(forKey: "duration", error: nil) != .loaded {
url.stopAccessingSecurityScopedResource()
return return
} }
guard let track = asset.tracks(withMediaType: .audio).first else { guard let track = asset.tracks(withMediaType: .audio).first else {
url.stopAccessingSecurityScopedResource()
return return
} }
let duration = track.timeRange.duration.seconds let duration = track.timeRange.duration.seconds
Queue.mainQueue().async { Queue.mainQueue().async {
if duration > Double(settings.maxDuration) { if duration > Double(settings.maxDuration) {
url.stopAccessingSecurityScopedResource()
//TODO:localize //TODO:localize
presentUndo(.info(title: "\(url.lastPathComponent) is too long", text: "The duration is longer than \(stringForDuration(Int32(settings.maxDuration))).")) presentUndo(.info(title: "\(url.lastPathComponent) is too long", text: "The duration is longer than \(stringForDuration(Int32(settings.maxDuration)))."))
} else { } else {
@ -527,11 +541,22 @@ public func presentCustomNotificationSoundFilePicker(context: AccountContext, co
//TODO:localize //TODO:localize
presentUndo(.notificationSoundAdded(title: "Sound Added", text: "The sound **\(url.deletingPathExtension().lastPathComponent)** was added to your Telegram tones.**", action: nil)) presentUndo(.notificationSoundAdded(title: "Sound Added", text: "The sound **\(url.deletingPathExtension().lastPathComponent)** was added to your Telegram tones.**", action: nil))
}, error: { _ in }, error: { _ in
url.stopAccessingSecurityScopedResource()
}, completed: {
url.stopAccessingSecurityScopedResource()
})) }))
} }
} }
}) })
} catch let e {
Logger.shared.log("NotificationSoundSelection", "Error: \(e)")
}
} }
}) })
if let error = error {
url.stopAccessingSecurityScopedResource()
Logger.shared.log("NotificationSoundSelection", "Error: \(error)")
}
}), in: .window(.root)) }), in: .window(.root))
} }