Skip to content

Commit

Permalink
chore: Merge branch 'master' into fix-realm-drive-sort-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Nov 14, 2024
2 parents 9ef9850 + 33bcf74 commit 4039936
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion kDriveCore/Utils/PHAsset/PHAsset+Exension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public extension PHAsset {
shouldTransformIntoJPEG = true
}

// Asset are copied when we start the Upload, thus guarantees the stability of the file
// Asset is copied when we start the Upload, thus guarantees the stability of the file
@InjectService var fileImportHelper: FileImportHelper
let targetURL = fileImportHelper.generateImportURL(for: resourceUTI)
do {
Expand All @@ -149,6 +149,7 @@ public extension PHAsset {
return targetURL
} catch {
SentryDebug.addBreadcrumb(message: error.localizedDescription, category: SentryDebug.Category.PHAsset, level: .error)
SentryDebug.capturePHAssetResourceManagerError(error)
}
return nil
}
Expand Down
23 changes: 17 additions & 6 deletions kDriveCore/Utils/PHAsset/PHAssetIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,43 @@ struct PHAssetIdentifier: PHAssetIdentifiable {
let activity = ExpiringActivity(id: uid, delegate: activityDelegate)
activity.start()

// TODO: Check iCloud behaviour
let options = PHAssetResourceRequestOptions()
options.isNetworkAccessAllowed = true
options.progressHandler = { progress in
Log.photoLibraryUploader("hashing resource \(progress * 100)% …")
}

group.enter()
var resourceManagerError: Error?
PHAssetResourceManager.default().requestData(for: bestResource,
options: options) { data in
hasher.update(data)
} completionHandler: { _ in
} completionHandler: { error in
if let error {
resourceManagerError = error
Log.photoLibraryUploader("hashing resource failed with \(error)", level: .error)
} else {
Log.photoLibraryUploader("hashing resource finished successfully")
}
hasher.finalize()
group.leave()
}
group.wait()

activity.endAll()

guard let error = activityDelegate.error else {
// All good
return hasher.digestString
// PHAssetResourceManager errors, possibly fetching an asset on iCloud failed
if let resourceManagerError {
SentryDebug.capturePHAssetResourceManagerError(resourceManagerError)
throw resourceManagerError
}

// The processing of the hash was interrupted by the system
throw error
if let activityError = activityDelegate.error {
throw activityError
}

return hasher.digestString
}
}
}
9 changes: 9 additions & 0 deletions kDriveCore/Utils/Sentry/SentryDebug+Upload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ extension SentryDebug {
SentryDebug.capture(message: EventNames.uploadCompletedSuccess, extras: metadata)
}

static func capturePHAssetResourceManagerError(_ error: Error, function: StaticString = #function) {
let metadata: [String: Any] = [
"func": function,
"error": error,
"localizedError": error.localizedDescription
]
SentryDebug.capture(message: ErrorNames.assetResourceManagerError, context: metadata, level: .error)
}

// MARK: - Upload notifications

static func uploadNotificationError(_ metadata: [String: Any]) {
Expand Down
2 changes: 2 additions & 0 deletions kDriveCore/Utils/Sentry/SentryDebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public enum SentryDebug {
static let uploadSessionErrorHandling = "UploadSessionErrorHandling"
static let uploadErrorUserNotification = "UploadErrorUserNotification"
static let viewModelNotConnectedToView = "ViewModelNotConnected"
/// An error was generated by an instance of `PHAssetResourceManager`
static let assetResourceManagerError = "PHAssetResourceManagerError"
}

static func logTokenMigration(newToken: ApiToken, oldToken: ApiToken) {
Expand Down

0 comments on commit 4039936

Please sign in to comment.