Skip to content

Add extension to the uploaded attachment #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Features/Network/MultipartRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ extension MultipartRequest {
body.appendString("\r\n")

// attachments
for attachment in report.attachmentPaths.compactMap(Attachment.init(filePath:)) {
for attachment in Set(report.attachmentPaths).compactMap(Attachment.init(filePath:)) {
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"\(attachment.name)\"; filename=\"\(attachment.name)\"\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(attachment.filename)\"; filename=\"\(attachment.filename)\"\r\n")
body.appendString("Content-Type: \(attachment.mimeType)\r\n\r\n")
body.append(attachment.data)
body.appendString("\r\n")
Expand Down
6 changes: 4 additions & 2 deletions Sources/Features/Repository/Model/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import CoreServices

struct Attachment {
let data: Data
let name: String
// file name with extension that represents real file name
let filename: String

let mimeType: String

// Make sure attachments are not bigger than 10 MB.
Expand All @@ -32,7 +34,7 @@ struct Attachment {
}

mimeType = Attachment.mimeTypeForPath(fileUrl: fileURL)
name = "attachment_" + (fileURL.lastPathComponent as NSString).deletingPathExtension + "_\(arc4random())"
filename = "attachment_" + fileURL.lastPathComponent
}

static private func mimeTypeForPath(fileUrl: URL) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Tests/AttachmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class AttachmentTests: QuickSpec {
it("has mime type: text/plain") {
expect(attachment.mimeType).to(equal("text/plain"))
expect(attachment.data).toNot(beNil())
expect(attachment.name).to(contain(["attachment_test_"]))
expect(attachment.filename).to(contain(["attachment_test"]))
}
} else {
fail()
Expand Down
Loading