Fix notification service media download

This commit is contained in:
Ali
2023-01-24 15:45:28 +01:00
parent 1ce798c465
commit e943444b48
7 changed files with 173 additions and 4 deletions

View File

@@ -460,6 +460,7 @@ private struct NotificationContent: CustomStringConvertible {
string += " userInfo: \(String(describing: self.userInfo)),\n"
string += " senderImage: \(self.senderImage != nil ? "non-empty" : "empty"),\n"
string += " isLockedMessage: \(String(describing: self.isLockedMessage)),\n"
string += " attachments: \(self.attachments),\n"
string += "}"
return string
}
@@ -1164,7 +1165,13 @@ private final class NotificationServiceHandler {
} else {
let intervals: Signal<[(Range<Int64>, MediaBoxFetchPriority)], NoError> = .single([(0 ..< Int64.max, MediaBoxFetchPriority.maximum)])
fetchMediaSignal = Signal { subscriber in
let collectedData = Atomic<Data>(value: Data())
final class DataValue {
var data = Data()
var totalSize: Int64?
}
let collectedData = Atomic<DataValue>(value: DataValue())
return standaloneMultipartFetch(
postbox: stateManager.postbox,
network: stateManager.network,
@@ -1191,11 +1198,33 @@ private final class NotificationServiceHandler {
).start(next: { result in
switch result {
case let .dataPart(_, data, _, _):
var isCompleted = false
let _ = collectedData.modify { current in
var current = current
current.append(data)
let current = current
current.data.append(data)
if let totalSize = current.totalSize, Int64(current.data.count) >= totalSize {
isCompleted = true
}
return current
}
if isCompleted {
subscriber.putNext(collectedData.with({ $0.data }))
subscriber.putCompletion()
}
case let .resourceSizeUpdated(size):
var isCompleted = false
let _ = collectedData.modify { current in
let current = current
current.totalSize = size
if Int64(current.data.count) >= size {
isCompleted = true
}
return current
}
if isCompleted {
subscriber.putNext(collectedData.with({ $0.data }))
subscriber.putCompletion()
}
default:
break
}
@@ -1203,7 +1232,7 @@ private final class NotificationServiceHandler {
subscriber.putNext(nil)
subscriber.putCompletion()
}, completed: {
subscriber.putNext(collectedData.with({ $0 }))
subscriber.putNext(collectedData.with({ $0.data }))
subscriber.putCompletion()
})
}
@@ -1303,10 +1332,15 @@ private final class NotificationServiceHandler {
}
Logger.shared.log("NotificationService \(episode)", "Unread count: \(value.0), isCurrentAccount: \(isCurrentAccount)")
Logger.shared.log("NotificationService \(episode)", "mediaAttachment: \(String(describing: mediaAttachment)), mediaData: \(String(describing: mediaData?.count))")
if let image = mediaAttachment as? TelegramMediaImage, let resource = largestImageRepresentation(image.representations)?.resource {
if let mediaData = mediaData {
stateManager.postbox.mediaBox.storeResourceData(resource.id, data: mediaData, synchronous: true)
if let messageId {
let _ = addSynchronizeAutosaveItemOperation(postbox: stateManager.postbox, messageId: messageId, mediaId: image.imageId).start()
}
}
if let storedPath = stateManager.postbox.mediaBox.completedResourcePath(resource, pathExtension: "jpg") {
if let attachment = try? UNNotificationAttachment(identifier: "image", url: URL(fileURLWithPath: storedPath), options: nil) {