Skip to content

Commit

Permalink
Merge pull request #1339 from Infomaniak/fix-photo-sync-iCloud-offload
Browse files Browse the repository at this point in the history
fix: Pictures no longer on device but only on iCloud should be uploaded as well.
  • Loading branch information
adrien-coye authored Nov 14, 2024
2 parents e12c9ed + c72428a commit 33bcf74
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 33bcf74

Please sign in to comment.