Skip to content

BugFix - Track File Upload Index #14822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ interface BackgroundJobManager {
fun startNotificationJob(subject: String, signature: String)
fun startAccountRemovalJob(accountName: String, remoteWipe: Boolean)
fun startFilesUploadJob(user: User)
fun startFilesUploadJob(user: User, totalUploadSize: Int)
fun getFileUploads(user: User): LiveData<List<JobInfo>>
fun cancelFilesUploadJob(user: User)
fun isStartFileUploadJobScheduled(user: User): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,18 +579,27 @@ internal class BackgroundJobManagerImpl(
return workManager.isWorkScheduled(startFileUploadJobTag(user))
}

override fun startFilesUploadJob(user: User) {
val data = workDataOf(FileUploadWorker.ACCOUNT to user.accountName)
override fun startFilesUploadJob(user: User) = startFilesUploadJobInternal(user, null)

override fun startFilesUploadJob(user: User, totalUploadSize: Int) =
startFilesUploadJobInternal(user, totalUploadSize)

private fun startFilesUploadJobInternal(user: User, totalUploadSize: Int?) {
val tag = startFileUploadJobTag(user)
val dataBuilder = Data.Builder()
.putString(FileUploadWorker.ACCOUNT, user.accountName)

totalUploadSize?.let {
dataBuilder.putInt(FileUploadWorker.TOTAL_UPLOAD_SIZE, it)
}

val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val request = oneTimeRequestBuilder(FileUploadWorker::class, JOB_FILES_UPLOAD, user)
.addTag(tag)
.setInputData(data)
.setInputData(dataBuilder.build())
.setConstraints(constraints)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class FileUploadHelper {
accountNames.forEach { accountName ->
val user = accountManager.getUser(accountName)
if (user.isPresent) {
backgroundJobManager.startFilesUploadJob(user.get())
backgroundJobManager.startFilesUploadJob(user.get(), failedUploads.size)
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ class FileUploadHelper {
}
}
uploadsStorageManager.storeUploads(uploads)
backgroundJobManager.startFilesUploadJob(user)
backgroundJobManager.startFilesUploadJob(user, uploads.size)
}

fun removeFileUpload(remotePath: String, accountName: String) {
Expand All @@ -230,7 +230,7 @@ class FileUploadHelper {

cancelAndRestartUploadJob(user)
} catch (e: NoSuchElementException) {
Log_OC.e(TAG, "Error cancelling current upload because user does not exist!")
Log_OC.e(TAG, "Error cancelling current upload because user does not exist!: " + e.message)
}
}

Expand All @@ -253,16 +253,22 @@ class FileUploadHelper {

try {
val user = accountManager.getUser(accountName).get()
cancelAndRestartUploadJob(user)
cancelAndRestartUploadJob(user, uploads.size)
} catch (e: NoSuchElementException) {
Log_OC.e(TAG, "Error restarting upload job because user does not exist!")
Log_OC.e(TAG, "Error restarting upload job because user does not exist!: " + e.message)
}
}

fun cancelAndRestartUploadJob(user: User) {
@JvmOverloads
fun cancelAndRestartUploadJob(user: User, totalUploadSize: Int? = null) {
backgroundJobManager.run {
cancelFilesUploadJob(user)
startFilesUploadJob(user)

if (totalUploadSize != null) {
startFilesUploadJob(user, totalUploadSize)
} else {
startFilesUploadJob(user)
}
}
}

Expand Down Expand Up @@ -377,7 +383,7 @@ class FileUploadHelper {
}
}
uploadsStorageManager.storeUploads(uploads)
backgroundJobManager.startFilesUploadJob(user)
backgroundJobManager.startFilesUploadJob(user, uploads.size)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class FileUploadWorker(

const val NOTIFICATION_ERROR_ID: Int = 413
const val ACCOUNT = "data_account"
const val TOTAL_UPLOAD_SIZE = "total_upload_size"
var currentUploadFileOperation: UploadFileOperation? = null

private const val UPLOADS_ADDED_MESSAGE = "UPLOADS_ADDED"
Expand Down Expand Up @@ -133,7 +134,8 @@ class FileUploadWorker(
private fun retrievePagesBySortingUploadsByID(): Result {
val accountName = inputData.getString(ACCOUNT) ?: return Result.failure()
var uploadsPerPage = uploadsStorageManager.getCurrentUploadsForAccountPageAscById(-1, accountName)
val totalUploadSize = uploadsStorageManager.getTotalUploadSize(accountName)
val totalUploadSize =
inputData.getInt(TOTAL_UPLOAD_SIZE, defaultValue = uploadsStorageManager.getTotalUploadSize(accountName))

Log_OC.d(TAG, "Total upload size: $totalUploadSize")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ protected ResultCode doInBackground(Object[] params) {
String[] remotePaths = (String[]) params[2];
int behaviour = (Integer) params[3];
ContentResolver leakedContentResolver = (ContentResolver) params[4];

String[] localPaths = new String[uris.length];
String[] currentRemotePaths = new String[uris.length];
String currentRemotePath;

for (int i = 0; i < uris.length; i++) {
Expand Down Expand Up @@ -179,15 +180,22 @@ protected ResultCode doInBackground(Object[] params) {
}
}

requestUpload(
user,
fullTempPath,
currentRemotePath,
behaviour
);
localPaths[i] = fullTempPath;
currentRemotePaths[i] = currentRemotePath;
fullTempPath = null;
}

FileUploadHelper.Companion.instance().uploadNewFiles(
user,
localPaths,
currentRemotePaths,
behaviour,
false, // do not create parent folder if not existent
UploadFileOperation.CREATED_BY_USER,
false,
false,
NameCollisionPolicy.ASK_USER);

result = ResultCode.OK;

} catch (ArrayIndexOutOfBoundsException e) {
Expand Down Expand Up @@ -237,19 +245,6 @@ protected ResultCode doInBackground(Object[] params) {
return result;
}

private void requestUpload(User user, String localPath, String remotePath, int behaviour) {
FileUploadHelper.Companion.instance().uploadNewFiles(
user,
new String[]{ localPath },
new String[]{ remotePath },
behaviour,
false, // do not create parent folder if not existent
UploadFileOperation.CREATED_BY_USER,
false,
false,
NameCollisionPolicy.ASK_USER);
}

@Override
protected void onPostExecute(ResultCode result) {
OnCopyTmpFilesTaskListener listener = mListener.get();
Expand Down
28 changes: 19 additions & 9 deletions app/src/main/java/com/owncloud/android/ui/helpers/UriUploader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,20 @@ class UriUploader(
.map { it as Uri }
.map { Pair(it, getRemotePathForUri(it)) }

val fileUris = uris
.filter { it.first.scheme == ContentResolver.SCHEME_FILE }
fileUris.forEach {
requestUpload(it.first.path, it.second)
val fileUris = uris.filter { it.first.scheme == ContentResolver.SCHEME_FILE }
if (fileUris.isNotEmpty()) {
val localPaths = Array<String>(fileUris.size) { "" }
val remotePaths = Array<String>(fileUris.size) { "" }

for (i in 0..<fileUris.size) {
val uri = fileUris[i]
uri.first.path?.let { path ->
localPaths[i] = path
remotePaths[i] = uri.second
}
}

requestUpload(localPaths, remotePaths)
}

val contentUrisNew = uris
Expand Down Expand Up @@ -118,14 +128,14 @@ class UriUploader(
* This is considered as acceptable, since when a file is shared from another app to OC,
* the usual workflow will go back to the original app.
*
* @param localPath Absolute path in the local file system to the file to upload.
* @param remotePath Absolute path in the current OC account to set to the uploaded file.
* @param localPaths Absolute paths in the local file system to the file to upload.
* @param remotePaths Absolute paths in the current OC account to set to the uploaded file.
*/
private fun requestUpload(localPath: String?, remotePath: String) {
private fun requestUpload(localPaths: Array<String>, remotePaths: Array<String>) {
FileUploadHelper.instance().uploadNewFiles(
user,
arrayOf(localPath ?: ""),
arrayOf(remotePath),
localPaths,
remotePaths,
mBehaviour,
// do not create parent folder if not existent
false,
Expand Down
Loading