Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 437d9d6e authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

File upload: force MKCOL request to be systematically run before PUT.

parent cbd809ef
Loading
Loading
Loading
Loading
+29 −20
Original line number Diff line number Diff line
@@ -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());
                Log.d(TAG, "Catched a conflict result for : "+ file.getName());
                mustRestart = false;
                        syncedState.setLocalLastModified(this.previousLastModified);
                    }
                }catch(Exception e) {
                    Log.e(TAG, e.toString() );
                    syncedState.setLocalLastModified(this.previousLastModified);
                    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;
    }
}