-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attachment support for crash reports (#59)
Attachment support for crash reports
- Loading branch information
1 parent
6cb3b4b
commit f4dd272
Showing
22 changed files
with
512 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
Sources/Features/Client/Model/AttachmentBookmarkHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
|
||
protocol AttachmentBookmarkHandler { | ||
static func convertAttachmentUrlsToBookmarks(_ attachments: Attachments) throws -> Bookmarks | ||
static func extractAttachmentUrls(_ bookmarks: Bookmarks) throws -> Attachments | ||
} | ||
|
||
enum AttachmentBookmarkHandlerImpl: AttachmentBookmarkHandler { | ||
static func convertAttachmentUrlsToBookmarks(_ attachments: Attachments) throws -> Bookmarks { | ||
var attachmentsBookmarksDict = Bookmarks() | ||
for attachment in attachments { | ||
do { | ||
let bookmark = try attachment.value.bookmarkData(options: URL.BookmarkCreationOptions.minimalBookmark) | ||
attachmentsBookmarksDict[attachment.key] = bookmark | ||
} catch { | ||
BacktraceLogger.error("Could not bookmark attachment file URL. Error: \(error)") | ||
continue | ||
} | ||
} | ||
return attachmentsBookmarksDict | ||
} | ||
|
||
static func extractAttachmentUrls(_ bookmarks: Bookmarks) throws -> Attachments { | ||
var attachments = Attachments() | ||
for bookmark in bookmarks { | ||
var stale = Bool(false) | ||
guard let fileUrl = try? URL(resolvingBookmarkData: bookmark.value, | ||
options: URL.BookmarkResolutionOptions(), | ||
relativeTo: nil, | ||
bookmarkDataIsStale: &stale) else { | ||
BacktraceLogger.error("Could not resolve file URL from bookmark") | ||
continue | ||
} | ||
if stale { | ||
BacktraceLogger.error("Bookmark data is stale. This should not happen") | ||
continue | ||
} | ||
attachments[bookmark.key] = fileUrl | ||
} | ||
return attachments | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Foundation | ||
|
||
enum AttachmentsStorageError: Error { | ||
case invalidDictionary | ||
case invalidBookmark | ||
} | ||
|
||
enum AttachmentsStorage { | ||
struct AttachmentsConfig: Config { | ||
let cacheUrl: URL | ||
let directoryUrl: URL | ||
let fileUrl: URL | ||
|
||
init(fileName: String) throws { | ||
guard let cacheDirectoryURL = | ||
FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { | ||
throw FileError.noCacheDirectory | ||
} | ||
self.cacheUrl = cacheDirectoryURL | ||
self.directoryUrl = cacheDirectoryURL.appendingPathComponent(directoryName) | ||
self.fileUrl = directoryUrl.appendingPathComponent("\(fileName)_attachments.plist") | ||
} | ||
} | ||
|
||
private static let directoryName = Bundle.main.bundleIdentifier ?? "BacktraceCache" | ||
|
||
static func store(_ attachments: Attachments, fileName: String) throws { | ||
try store(attachments, fileName: fileName, storage: ReportMetadataStorageImpl.self, | ||
bookmarkHandler: AttachmentBookmarkHandlerImpl.self) | ||
} | ||
|
||
static func store<T: ReportMetadataStorage, U: AttachmentBookmarkHandler> | ||
(_ attachments: Attachments, fileName: String, storage: T.Type, bookmarkHandler: U.Type) throws { | ||
let config = try AttachmentsConfig(fileName: fileName) | ||
let attachmentBookmarks = try U.convertAttachmentUrlsToBookmarks(attachments) | ||
try T.storeToFile(attachmentBookmarks, config: config) | ||
BacktraceLogger.debug("Stored attachments paths at path: \(config.fileUrl)") | ||
} | ||
|
||
static func retrieve(fileName: String) throws -> Attachments { | ||
try retrieve(fileName: fileName, storage: ReportMetadataStorageImpl.self, | ||
bookmarkHandler: AttachmentBookmarkHandlerImpl.self) | ||
} | ||
|
||
static func retrieve<T: ReportMetadataStorage, U: AttachmentBookmarkHandler> | ||
(fileName: String, storage: T.Type, bookmarkHandler: U.Type) throws -> Attachments { | ||
let config = try AttachmentsConfig(fileName: fileName) | ||
let dictionary = try T.retrieveFromFile(config: config) | ||
|
||
guard let bookmarks = dictionary as? Bookmarks else { | ||
BacktraceLogger.debug("Could not convert stored dictionary to Bookmarks type") | ||
throw AttachmentsStorageError.invalidDictionary | ||
} | ||
guard let attachments = try? U.extractAttachmentUrls(bookmarks) else { | ||
BacktraceLogger.debug("Could not extract attachment URLs from stored attachments Bookmarks") | ||
throw AttachmentsStorageError.invalidBookmark | ||
} | ||
|
||
BacktraceLogger.debug("Retrieved attachment paths at path: \(config.fileUrl)") | ||
return attachments | ||
} | ||
|
||
static func remove(fileName: String) throws { | ||
try remove(fileName: fileName, storage: ReportMetadataStorageImpl.self) | ||
} | ||
|
||
static func remove<T: ReportMetadataStorage>(fileName: String, storage: T.Type) throws { | ||
let config = try AttachmentsConfig(fileName: fileName) | ||
try T.removeFile(config: config) | ||
BacktraceLogger.debug("Removed attachments paths at path: \(config.fileUrl)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.