From ab5c6643b3be50dfacee8ee6ec6341c97194ffbf Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Tue, 4 Apr 2023 07:51:46 +0200 Subject: [PATCH] Fix a crash when deleting an uploading image --- .../Status/Editor/StatusEditorViewModel.swift | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift index 8c15fe72..9fa2a433 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift @@ -612,42 +612,46 @@ public class StatusEditorViewModel: NSObject, ObservableObject { error: nil) mediasImages[index] = newContainer do { - if let index = indexOf(container: newContainer) { - let compressor = StatusEditorCompressor() - if let image = originalContainer.image { - let imageData = try await compressor.compressImageForUpload(image) - let uploadedMedia = try await uploadMedia(data: imageData, mimeType: "image/jpeg") + let compressor = StatusEditorCompressor() + if let image = originalContainer.image { + let imageData = try await compressor.compressImageForUpload(image) + let uploadedMedia = try await uploadMedia(data: imageData, mimeType: "image/jpeg") + if let index = indexOf(container: newContainer) { mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil, movieTransferable: nil, gifTransferable: nil, mediaAttachment: uploadedMedia, error: nil) - if let uploadedMedia, uploadedMedia.url == nil { - scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) - } - } else if let videoURL = originalContainer.movieTransferable?.url, - let compressedVideoURL = await compressor.compressVideo(videoURL), - let data = try? Data(contentsOf: compressedVideoURL) - { - let uploadedMedia = try await uploadMedia(data: data, mimeType: compressedVideoURL.mimeType()) + } + if let uploadedMedia, uploadedMedia.url == nil { + scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) + } + } else if let videoURL = originalContainer.movieTransferable?.url, + let compressedVideoURL = await compressor.compressVideo(videoURL), + let data = try? Data(contentsOf: compressedVideoURL) + { + let uploadedMedia = try await uploadMedia(data: data, mimeType: compressedVideoURL.mimeType()) + if let index = indexOf(container: newContainer) { mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil, movieTransferable: originalContainer.movieTransferable, gifTransferable: nil, mediaAttachment: uploadedMedia, error: nil) - if let uploadedMedia, uploadedMedia.url == nil { - scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) - } - } else if let gifData = originalContainer.gifTransferable?.data { - let uploadedMedia = try await uploadMedia(data: gifData, mimeType: "image/gif") + } + if let uploadedMedia, uploadedMedia.url == nil { + scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) + } + } else if let gifData = originalContainer.gifTransferable?.data { + let uploadedMedia = try await uploadMedia(data: gifData, mimeType: "image/gif") + if let index = indexOf(container: newContainer) { mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil, movieTransferable: nil, gifTransferable: originalContainer.gifTransferable, mediaAttachment: uploadedMedia, error: nil) - if let uploadedMedia, uploadedMedia.url == nil { - scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) - } + } + if let uploadedMedia, uploadedMedia.url == nil { + scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) } } } catch {