Skip to content

Commit

Permalink
Stores temporary version of intent
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyLB committed Jul 16, 2021
1 parent 81c419a commit 1d3ff30
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 31 deletions.
76 changes: 45 additions & 31 deletions piwigoIntents/AutoUploadPhotosHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,71 @@ class AutoUploadPhotosHandler: NSObject, AutoUploadPhotosIntentHandling {

// First, find auto-upload requests whose transfer did fail
let failedUploads = uploadsProvider.getAutoUploadRequestsIn(states: [.uploadingError]).1
if failedUploads.count > 0, failedUploads.count < 2 {
// Will relaunch transfers with one which failed
if failedUploads.count > 0 {
// Will try to relaunch transfers
UploadManager.shared.uploadRequestsToTransfer = Set(failedUploads[..<min(UploadManager.shared.maxNberOfUploadsPerBckgTask, failedUploads.count)])
print("\(UploadUtilities.debugFormatter.string(from: Date())) >•• collected \(UploadManager.shared.uploadRequestsToTransfer.count) failed uploads")
print(" >•• collected \(UploadManager.shared.uploadRequestsToTransfer.count) failed uploads")

// Stop here?
if failedUploads.count > 5 {
let errorMsg = NSLocalizedString("AutoUploadError_Failed",
comment: "Several transfers failed and the upload queue is on hold. Please check with the app.")
completion(AutoUploadPhotosIntentResponse.failure(error: errorMsg))
return
}
}

// Second, find auto-upload requests ready for transfer
let preparedUploads = uploadsProvider.getAutoUploadRequestsIn(states: [.prepared]).1
if UploadManager.shared.uploadRequestsToTransfer.count == 0,
if UploadManager.shared.uploadRequestsToTransfer.count < 2,
preparedUploads.count > 0 {
// Will relaunch transfers with a prepared upload
// Will launch transfers of prepared files
UploadManager.shared.uploadRequestsToTransfer = UploadManager.shared.uploadRequestsToTransfer
.union(Set(preparedUploads[..<min(UploadManager.shared.maxNberOfUploadsPerBckgTask,preparedUploads.count)]))
print("\(UploadUtilities.debugFormatter.string(from: Date())) >•• collected \(min(UploadManager.shared.maxNberOfUploadsPerBckgTask,preparedUploads.count)) prepared uploads)")
}

// Finally, get list of auto-upload requests to prepare
let toTransfer = UploadManager.shared.uploadRequestsToTransfer.count
print(" >•• collected \(toTransfer) prepared uploads")

// Can we still add upload requests to the queue?
let diff = UploadManager.shared.maxNberOfUploadsPerBckgTask -
UploadManager.shared.uploadRequestsToTransfer.count
if diff > 0 {
let requestsToPrepare = uploadsProvider.getAutoUploadRequestsIn(states: [.waiting]).1
print("\(UploadUtilities.debugFormatter.string(from: Date())) >•• collected \(min(diff, requestsToPrepare.count)) uploads to prepare")
UploadManager.shared.uploadRequestsToPrepare = Set(requestsToPrepare[..<min(diff, requestsToPrepare.count)])
if diff <= 0 {
completion(AutoUploadPhotosIntentResponse.success(toPrepare: NSNumber(value: 0),
inQueue: NSNumber(value: toTransfer)))
return
}
let nberOfPhotos = UploadManager.shared.uploadRequestsToPrepare.count


// Get list of auto-upload requests to prepare
let requestsToPrepare = uploadsProvider.getAutoUploadRequestsIn(states: [.waiting]).1
UploadManager.shared.uploadRequestsToPrepare = Set(requestsToPrepare[..<min(diff, requestsToPrepare.count)])
let toPrepare = UploadManager.shared.uploadRequestsToPrepare.count
print(" >•• collected \(toPrepare) uploads to prepare")

// Create the operation queue
let uploadQueue = OperationQueue()
uploadQueue.maxConcurrentOperationCount = 1

// Add operation setting flag and selecting upload requests
let initOperation = BlockOperation {
// Decisions will be taken as for a background task
UploadManager.shared.isExecutingBackgroundUploadTask = true

// Reset variables
UploadManager.shared.countOfBytesPrepared = 0
UploadManager.shared.countOfBytesToUpload = 0
}

// Initialise list of operations
var uploadOperations = [BlockOperation]()
uploadOperations.append(initOperation)

// Resume transfers started in the foreground
let resumeOperation = BlockOperation {
UploadManager.shared.resumeTransfers()
}
resumeOperation.addDependency(uploadOperations.last!)
uploadOperations.append(resumeOperation)

// Add image preparation which will be followed by transfer operations
for _ in 0..<UploadManager.shared.maxNberOfUploadsPerBckgTask {
let uploadOperation = BlockOperation {
Expand All @@ -100,9 +126,10 @@ class AutoUploadPhotosHandler: NSObject, AutoUploadPhotosIntentHandling {

// Start the operation
print(" > Start upload operations in background task...");
uploadQueue.addOperations(uploadOperations, waitUntilFinished: true)
uploadQueue.addOperations(uploadOperations, waitUntilFinished: false)

completion(AutoUploadPhotosIntentResponse.success(nberPhotos: NSNumber(value: nberOfPhotos)))
completion(AutoUploadPhotosIntentResponse.success(toPrepare: NSNumber(value: toPrepare),
inQueue: NSNumber(value: toTransfer + toPrepare)))
}


Expand Down Expand Up @@ -149,23 +176,10 @@ class AutoUploadPhotosHandler: NSObject, AutoUploadPhotosIntentHandling {

// Determine which local images are still not considered for upload
var uploadRequestsToAppend = [UploadProperties]()
let serverFileTypes = UploadVars.serverFileTypes
fetchedImages.enumerateObjects { image, idx, stop in
// Keep images which had never been considered for upload
if !imageIDs.contains(image.localIdentifier) {
// Rejects videos if the server cannot accept them
if image.mediaType == .video {
// Retrieve image file extension (slow)
let fileName = UploadUtilities.fileName(forImageAsset: image)
let fileExt = (URL(fileURLWithPath: fileName).pathExtension).lowercased()
// Check file format
let unacceptedFileFormat = !serverFileTypes.contains(fileExt)
let mp4NotAccepted = !serverFileTypes.contains("mp4")
let notConvertible = !UploadUtilities.acceptedMovieFormats.contains(fileExt)
if unacceptedFileFormat && (mp4NotAccepted || notConvertible) { return }
}

// Format should be acceptable, create upload request
// Create new upload request
var uploadRequest = UploadProperties(localIdentifier: image.localIdentifier,
category: categoryId)
uploadRequest.markedForAutoUpload = true
Expand Down
1 change: 1 addition & 0 deletions piwigoIntents/Resources/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

"AutoUploadError_Disabled" = "Auto-uploading is disabled in the app settings.";
"AutoUploadError_Failed" = "Several transfers failed and the upload queue is on hold. Please check with the app.";
11 changes: 11 additions & 0 deletions piwigoIntents/ar.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/da.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/de.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/es.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/fr.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} sont en cours de chargement.";

"656sT6" = "Nbre de photos";

"86Lqgi" = "Impossible de lancer les chargements.";

"AaGMwg" = "Chargement automatique des photos";

"AkZxm5" = "Chargement automatique des photos comme prédéfinies dans les paramètres de l'application.";

"ohRnWU" = "Téléchargement automatique des photos";
11 changes: 11 additions & 0 deletions piwigoIntents/id.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/it.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/ja.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/nl.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/pl.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/ru.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/sv.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";
11 changes: 11 additions & 0 deletions piwigoIntents/zh-Hans.lproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"0ujcTO" = "${nberPhotos} are being uploaded.";

"656sT6" = "Nber Photos";

"86Lqgi" = "Could not launch the uploads.";

"AaGMwg" = "Auto-Upload Photos";

"AkZxm5" = "Auto-uploads photos as pre-defined in the settings of the app.";

"ohRnWU" = "Auto-Upload Photos";

0 comments on commit 1d3ff30

Please sign in to comment.