diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index 5d972a0b2b30dae77900bf53d9a66b2e31fec27a..5b4c24ae50d72f4e268accd8271cb4b476004b8e 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -95,6 +95,10 @@ public class UploadFileOperation extends RemoteOperation { } } + if (!createRemoteFolder(targetPath, client)) { + return new RemoteOperationResult(ResultCode.UNKNOWN_ERROR); + } + final UploadFileRemoteOperation uploadOperation = buildUploadOperation(file, targetPath); // Execute UploadFileOperation @@ -112,27 +116,10 @@ public class UploadFileOperation extends RemoteOperation { resultCode = uploadResult.getCode(); mustRestart = false; } else { - //Si les répértoires ou mettre le fichier n'existe pas, on les ajoutes. if (uploadResult.getCode() == ResultCode.CONFLICT ) { resultCode = ResultCode.CONFLICT; - Log.d(TAG, "Catched a conflict result for : "+file.getName()+", create missing remote path then retry"); - final String remoteFolderPath = targetPath.substring(0, targetPath.lastIndexOf(FileUtils.PATH_SEPARATOR)+1 ); - final CreateFolderRemoteOperation createFolderOperation = new CreateFolderRemoteOperation(remoteFolderPath, true ); - try{ - RemoteOperationResult createFolderResult = createFolderOperation.execute(client ); - - if (!createFolderResult.isSuccess() && createFolderResult.getCode() != ResultCode.FOLDER_ALREADY_EXISTS) { - resultCode = createFolderResult.getCode(); - Log.e(TAG, createFolderResult.getLogMessage()); - mustRestart = false; - syncedState.setLocalLastModified(this.previousLastModified); - } - }catch(Exception e) { - Log.e(TAG, e.toString() ); - syncedState.setLocalLastModified(this.previousLastModified); - mustRestart = false; - } - + Log.d(TAG, "Catched a conflict result for : "+ file.getName()); + mustRestart = false; } else if (uploadResult.getCode() == ResultCode.QUOTA_EXCEEDED) { resultCode = ResultCode.QUOTA_EXCEEDED; mustRestart = false; @@ -147,7 +134,7 @@ public class UploadFileOperation extends RemoteOperation { if (mustRestart) { if (this.restartCounter < 1) { this.restartCounter += 1; - //if we encounter more than three times same error, stop trying to download. + //if we encounter more than one time same error, stop trying to upload. return this.run(client); } else { syncedState.setLocalLastModified(this.previousLastModified); //Revert syncFileState to its previous state @@ -207,4 +194,26 @@ public class UploadFileOperation extends RemoteOperation { return new RemoteOperationResult(ocsResult.getCode()); } } + + + /** + * Create remote parent folder of the file if missing + * @param targetPath + * @param client + * @return + */ + public boolean createRemoteFolder(String targetPath, OwnCloudClient client) { + final String remoteFolderPath = targetPath.substring(0, targetPath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1); + final CreateFolderRemoteOperation createFolderOperation = new CreateFolderRemoteOperation(remoteFolderPath, true); + try{ + final RemoteOperationResult createFolderResult = createFolderOperation.execute(client); + if (createFolderResult.isSuccess() || createFolderResult.getCode() == ResultCode.FOLDER_ALREADY_EXISTS) { + return true; + } + Log.e(TAG, createFolderResult.getLogMessage()); + } catch(Exception e) { + Log.e(TAG, e.toString()); + } + return false; + } }