Skip to content

Commit 2089388

Browse files
authored
Add extension to the uploaded attachment (#126)
1 parent 34a4b3c commit 2089388

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

Sources/Features/Network/MultipartRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ extension MultipartRequest {
6565
body.appendString("\r\n")
6666

6767
// attachments
68-
for attachment in report.attachmentPaths.compactMap(Attachment.init(filePath:)) {
68+
for attachment in Set(report.attachmentPaths).compactMap(Attachment.init(filePath:)) {
6969
body.appendString(boundaryPrefix)
70-
body.appendString("Content-Disposition: form-data; name=\"\(attachment.name)\"; filename=\"\(attachment.name)\"\r\n")
70+
body.appendString("Content-Disposition: form-data; name=\"\(attachment.filename)\"; filename=\"\(attachment.filename)\"\r\n")
7171
body.appendString("Content-Type: \(attachment.mimeType)\r\n\r\n")
7272
body.append(attachment.data)
7373
body.appendString("\r\n")

Sources/Features/Repository/Model/Attachment.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import CoreServices
77

88
struct Attachment {
99
let data: Data
10-
let name: String
10+
// file name with extension that represents real file name
11+
let filename: String
12+
1113
let mimeType: String
1214

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

3436
mimeType = Attachment.mimeTypeForPath(fileUrl: fileURL)
35-
name = "attachment_" + (fileURL.lastPathComponent as NSString).deletingPathExtension + "_\(arc4random())"
37+
filename = "attachment_" + fileURL.lastPathComponent
3638
}
3739

3840
static private func mimeTypeForPath(fileUrl: URL) -> String {

Tests/AttachmentTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class AttachmentTests: QuickSpec {
3333
it("has mime type: text/plain") {
3434
expect(attachment.mimeType).to(equal("text/plain"))
3535
expect(attachment.data).toNot(beNil())
36-
expect(attachment.name).to(contain(["attachment_test_"]))
36+
expect(attachment.filename).to(contain(["attachment_test"]))
3737
}
3838
} else {
3939
fail()

0 commit comments

Comments
 (0)