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,47 +491,72 @@ 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 {
let asset = AVAsset(url: url) do {
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)))."))
return url.stopAccessingSecurityScopedResource()
}
asset.loadValuesAsynchronously(forKeys: ["tracks", "duration"], completionHandler: {
if asset.statusOfValue(forKey: "duration", error: nil) != .loaded {
return return
} }
guard let track = asset.tracks(withMediaType: .audio).first else {
return
}
let duration = track.timeRange.duration.seconds
Queue.mainQueue().async { asset.loadValuesAsynchronously(forKeys: ["tracks", "duration"], completionHandler: {
if duration > Double(settings.maxDuration) { if asset.statusOfValue(forKey: "duration", error: nil) != .loaded {
//TODO:localize url.stopAccessingSecurityScopedResource()
presentUndo(.info(title: "\(url.lastPathComponent) is too long", text: "The duration is longer than \(stringForDuration(Int32(settings.maxDuration)))."))
} else { return
disposable.set((context.engine.peers.uploadNotificationSound(title: url.lastPathComponent, data: data)
|> deliverOnMainQueue).start(next: { _ in
//TODO:localize
presentUndo(.notificationSoundAdded(title: "Sound Added", text: "The sound **\(url.deletingPathExtension().lastPathComponent)** was added to your Telegram tones.**", action: nil))
}, error: { _ in
}))
} }
} guard let track = asset.tracks(withMediaType: .audio).first else {
}) url.stopAccessingSecurityScopedResource()
return
}
let duration = track.timeRange.duration.seconds
Queue.mainQueue().async {
if duration > Double(settings.maxDuration) {
url.stopAccessingSecurityScopedResource()
//TODO:localize
presentUndo(.info(title: "\(url.lastPathComponent) is too long", text: "The duration is longer than \(stringForDuration(Int32(settings.maxDuration)))."))
} else {
disposable.set((context.engine.peers.uploadNotificationSound(title: url.lastPathComponent, data: data)
|> deliverOnMainQueue).start(next: { _ in
//TODO:localize
presentUndo(.notificationSoundAdded(title: "Sound Added", text: "The sound **\(url.deletingPathExtension().lastPathComponent)** was added to your Telegram tones.**", action: nil))
}, 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))
} }