From bc7500bde97cdbbe7e6df98700622e1e47468528 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 10 Nov 2023 14:08:11 -0500 Subject: [PATCH] Fix crash when uploading attachment without known MIME type or extension --- .../Sources/ComposeUI/API/PostService.swift | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift b/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift index 6733c17d..c7de164d 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift @@ -114,13 +114,9 @@ class PostService: ObservableObject { } catch let error as DraftAttachment.ExportError { throw Error.attachmentData(index: index, cause: error) } - do { - let uploaded = try await uploadAttachment(data: data, utType: utType, description: attachment.attachmentDescription) - attachments.append(uploaded.id) - currentStep += 1 - } catch let error as Client.Error { - throw Error.attachmentUpload(index: index, cause: error) - } + let uploaded = try await uploadAttachment(index: index, data: data, utType: utType, description: attachment.attachmentDescription) + attachments.append(uploaded.id) + currentStep += 1 } return attachments } @@ -138,10 +134,21 @@ class PostService: ObservableObject { } } - private func uploadAttachment(data: Data, utType: UTType, description: String?) async throws -> Attachment { - let formAttachment = FormAttachment(mimeType: utType.preferredMIMEType!, data: data, fileName: "file.\(utType.preferredFilenameExtension!)") + private func uploadAttachment(index: Int, data: Data, utType: UTType, description: String?) async throws -> Attachment { + guard let mimeType = utType.preferredMIMEType else { + throw Error.attachmentMissingMimeType(index: index, type: utType) + } + var filename = "file" + if let ext = utType.preferredFilenameExtension { + filename.append(".\(ext)") + } + let formAttachment = FormAttachment(mimeType: mimeType, data: data, fileName: filename) let req = Client.upload(attachment: formAttachment, description: description) - return try await mastodonController.run(req).0 + do { + return try await mastodonController.run(req).0 + } catch let error as Client.Error { + throw Error.attachmentUpload(index: index, cause: error) + } } private func textForPosting() -> String { @@ -170,6 +177,7 @@ class PostService: ObservableObject { enum Error: Swift.Error, LocalizedError { case attachmentData(index: Int, cause: DraftAttachment.ExportError) + case attachmentMissingMimeType(index: Int, type: UTType) case attachmentUpload(index: Int, cause: Client.Error) case posting(Client.Error) @@ -177,6 +185,8 @@ class PostService: ObservableObject { switch self { case let .attachmentData(index: index, cause: cause): return "Attachment \(index + 1): \(cause.localizedDescription)" + case let .attachmentMissingMimeType(index: index, type: type): + return "Attachment \(index + 1): unknown MIME type for \(type.identifier)" case let .attachmentUpload(index: index, cause: cause): return "Attachment \(index + 1): \(cause.localizedDescription)" case let .posting(error):