Skip to content

Commit da2151c

Browse files
authored
Clean up file attachments API (#63)
* Let the user specify their file attachments as simply a list of URLs
1 parent 55af18e commit da2151c

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

Examples/Example-iOS/AppDelegate.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
3636
BacktraceClient.shared?.attributes = ["foo": "bar", "testing": true]
3737

3838
let fileName = "sample.txt"
39-
let fileUrl = try? createAndWriteFile(fileName)
39+
guard let fileUrl = try? createAndWriteFile(fileName) else {
40+
print("Could not create the file attachment")
41+
return false
42+
}
4043
var crashAttachments = Attachments()
41-
crashAttachments[fileName] = fileUrl
44+
crashAttachments.append(fileUrl)
4245
BacktraceClient.shared?.attachments = crashAttachments
4346

4447
BacktraceClient.shared?.loggingDestinations = [BacktraceBaseDestination(level: .debug)]

Sources/Features/Attributes/AttributesProvider.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class AttributesProvider {
1414

1515
// attributes can be modified on runtime
1616
var attributes: Attributes = [:]
17-
var attachments: Attachments = [:]
17+
var attachments: Attachments = []
1818
private let attributesSources: [AttributesSource]
1919
private let faultInfo: FaultInfo
2020

@@ -45,7 +45,7 @@ extension AttributesProvider: SignalContext {
4545
}
4646

4747
var attachmentPaths: [String] {
48-
return attachments.map(\.value.path)
48+
return attachments.map(\.path)
4949
}
5050

5151
var allAttributes: Attributes {

Sources/Features/Client/Model/AttachmentBookmarkHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ enum AttachmentBookmarkHandlerImpl: AttachmentBookmarkHandler {
1010
var attachmentsBookmarksDict = Bookmarks()
1111
for attachment in attachments {
1212
do {
13-
let bookmark = try attachment.value.bookmarkData(options: URL.BookmarkCreationOptions.minimalBookmark)
14-
attachmentsBookmarksDict[attachment.key] = bookmark
13+
let bookmark = try attachment.bookmarkData(options: .minimalBookmark)
14+
attachmentsBookmarksDict[attachment.path] = bookmark
1515
} catch {
1616
BacktraceLogger.error("Could not bookmark attachment file URL. Error: \(error)")
1717
continue
@@ -35,7 +35,7 @@ enum AttachmentBookmarkHandlerImpl: AttachmentBookmarkHandler {
3535
BacktraceLogger.error("Bookmark data is stale. This should not happen")
3636
continue
3737
}
38-
attachments[bookmark.key] = fileUrl
38+
attachments.append(fileUrl)
3939
}
4040
return attachments
4141
}

Sources/Public/BacktraceClientCustomizing.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ public typealias BacktraceClientProtocol = BacktraceReporting & BacktraceClientC
77
public typealias Attributes = [String: Any]
88

99
/// Type-alias of passing file attachments to library.
10-
/// Expected format: Filename, File URL bookmark
11-
public typealias Attachments = [String: URL]
10+
public typealias Attachments = [URL]
1211

1312
/// Type-alias of storing file attachments on disk (as a bookmark)
1413
/// Expected format: Filename, File URL bookmark

Sources/Public/BacktraceCrashReporter.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,21 @@ extension BacktraceCrashReporter: CrashReporting {
7272
BacktraceLogger.error("Could not get cache directory URL")
7373
return [URL]()
7474
}
75-
let attachments = (try? AttachmentsStorage.retrieve(fileName: BacktraceCrashReporter.crashName)) ?? [:]
75+
let attachments = (try? AttachmentsStorage.retrieve(fileName: BacktraceCrashReporter.crashName)) ?? []
7676
var copiedFileAttachments = [URL]()
7777
for attachment in attachments {
7878
let fileManager = FileManager.default
79-
let copiedAttachmentPath = directoryUrl.appendingPathComponent(attachment.key)
79+
let fileName = attachment.lastPathComponent
80+
let copiedAttachmentPath = directoryUrl.appendingPathComponent(fileName)
8081
do {
81-
if !fileManager.fileExists(atPath: attachment.value.path) {
82+
if !fileManager.fileExists(atPath: attachment.path) {
8283
BacktraceLogger.error("File attachment from previous session does not exist")
8384
continue
8485
}
8586
if fileManager.fileExists(atPath: copiedAttachmentPath.path) {
8687
try fileManager.removeItem(atPath: copiedAttachmentPath.path)
8788
}
88-
try fileManager.copyItem(at: attachment.value, to: copiedAttachmentPath)
89+
try fileManager.copyItem(at: attachment, to: copiedAttachmentPath)
8990
copiedFileAttachments.append(copiedAttachmentPath)
9091
} catch {
9192
BacktraceLogger.error("Could not copy bookmarked attachment file from previous session. Error: \(error)")

Tests/AttachmentStorageTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class AttachmentStorageTests: QuickSpec {
1616
guard let fileUrl = try? self.createAFile() else {
1717
throw FileError.fileNotWritten
1818
}
19-
crashAttachments["myFile"] = fileUrl
19+
crashAttachments.append(fileUrl)
2020

2121
let attachmentsFileName = "attachments"
2222
try? AttachmentsStorage.store(crashAttachments,
@@ -28,7 +28,7 @@ final class AttachmentStorageTests: QuickSpec {
2828
(try? AttachmentsStorage.retrieve(fileName: attachmentsFileName,
2929
storage: storage,
3030
bookmarkHandler: bookmarkHandler)) ?? Attachments()
31-
let attachmentPaths = attachments.map(\.value.path)
31+
let attachmentPaths = attachments.map(\.path)
3232

3333
expect(attachmentPaths).toNot(beNil())
3434
expect(attachmentPaths.count).to(be(1))
@@ -49,7 +49,7 @@ final class AttachmentStorageTests: QuickSpec {
4949
(try? AttachmentsStorage.retrieve(fileName: attachmentsFileName,
5050
storage: storage,
5151
bookmarkHandler: bookmarkHandler)) ?? Attachments()
52-
let attachmentPaths = attachments.map(\.value.path)
52+
let attachmentPaths = attachments.map(\.path)
5353

5454
expect(attachmentPaths).toNot(beNil())
5555
expect(attachmentPaths.count).to(be(0))

Tests/Mocks/AttachmentBookmarkHandlerMock.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ import Foundation
22
import XCTest
33
@testable import Backtrace
44

5+
enum AttachmentsBookmarkError: Error {
6+
case invalidUrl
7+
}
8+
59
enum AttachmentBookmarkHandlerMock: AttachmentBookmarkHandler {
610
static func convertAttachmentUrlsToBookmarks(_ attachments: Attachments) throws -> Bookmarks {
711
var attachmentsBookmarksDict = Bookmarks()
812
for attachment in attachments {
9-
attachmentsBookmarksDict[attachment.key] = attachment.value.path.data(using: .utf8)
13+
attachmentsBookmarksDict[attachment.path] = attachment.path.data(using: .utf8)
1014
}
1115
return attachmentsBookmarksDict
1216
}
1317

1418
static func extractAttachmentUrls(_ bookmarks: Bookmarks) throws -> Attachments {
1519
var attachments = Attachments()
1620
for bookmark in bookmarks {
17-
attachments[bookmark.key] = URL(string: String(data: bookmark.value, encoding: .utf8) ?? String())
21+
guard let fileUrl = URL(string: String(data: bookmark.value, encoding: .utf8) ?? String()) else {
22+
throw AttachmentsBookmarkError.invalidUrl
23+
}
24+
attachments.append(fileUrl)
1825
}
1926
return attachments
2027
}

0 commit comments

Comments
 (0)