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

Commit d4b39f93 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Merge branch 'e1-clean-uploadFileOperation' into '1-epic-refactoring-p1'

clean UploadFileOperation.java

See merge request !89
parents 058f77ba 5d65ee2b
Loading
Loading
Loading
Loading
Loading
+56 −97
Original line number Diff line number Diff line
@@ -9,8 +9,6 @@
package foundation.e.drive.operations;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
@@ -34,51 +32,25 @@ import foundation.e.drive.utils.CommonUtils;
 * @author Vincent Bourgmayer
 * High level Operation which upload a local file to a remote cloud storage
 */
public class UploadFileOperation extends RemoteOperation implements Parcelable {
public class UploadFileOperation extends RemoteOperation {
    private final static String TAG = UploadFileOperation.class.getSimpleName();


    private int restartCounter =0;
    private long previousLastModified; //get to restore real value if all trials fails
    private Context mContext;
    private SyncedFileState mSyncedState;

    private Context context;
    private SyncedFileState syncedState;
    private long availableQuota = -1;

    protected UploadFileOperation(Parcel in) {
        restartCounter = in.readInt();
        previousLastModified = in.readLong();
        mSyncedState = in.readParcelable(SyncedFileState.class.getClassLoader());
        availableQuota = in.readLong();
    }

    public static final Creator<UploadFileOperation> CREATOR = new Creator<UploadFileOperation>() {
        @Override
        public UploadFileOperation createFromParcel(Parcel in) {
            return new UploadFileOperation(in);
        }

        @Override
        public UploadFileOperation[] newArray(int size) {
            return new UploadFileOperation[size];
        }
    };

    /**
     * Construct an upload operation with an already known syncedFileState
     * @param syncedFileState syncedFileState corresponding to file.
     */
    public UploadFileOperation (SyncedFileState syncedFileState){
        this.mSyncedState = syncedFileState;
        this.previousLastModified = mSyncedState.getLocalLastModified();
    public UploadFileOperation (SyncedFileState syncedFileState, Context context) {
        this.syncedState = syncedFileState;
        this.previousLastModified = syncedState.getLocalLastModified();
        this.context = context;
    }

    public void setContext(Context context){
        this.mContext = context;
    }



    /**
     * Execute the operation:
     *
@@ -93,23 +65,23 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable {
    @Override
    protected RemoteOperationResult run(OwnCloudClient client ) {
        //as operation isn't executed immediatly, file might have been deleted since creation of operation
        if(mSyncedState == null ){
        if (syncedState == null ) {
            Log.e(TAG, "run(client):  no syncedFileState or target path, can't perform upload operation");
            return new RemoteOperationResult(ResultCode.FORBIDDEN);
        }

        File file = new File(mSyncedState.getLocalPath());
        File file = new File(syncedState.getLocalPath());
        if (file == null || !file.exists()) {
            Log.w(TAG, "Can't get the file. It might have been deleted");
            return new RemoteOperationResult(ResultCode.FORBIDDEN);
        }

        String targetPath = mSyncedState.getRemotePath();
        final String targetPath = syncedState.getRemotePath();

        //If an Etag is already Stored and LastModified from DB is the same as real file
        if (mSyncedState.isLastEtagStored()
                && mSyncedState.getLocalLastModified() == file.lastModified()) {
            Log.d(TAG, "mySyncedState last modified: "+mSyncedState.getLocalLastModified()+" <=> mFile last modified: "+file.lastModified() +": So return sync_conflict");
        if (syncedState.isLastEtagStored()
                && syncedState.getLocalLastModified() == file.lastModified()) {
            Log.d(TAG, "syncedState last modified: "+ syncedState.getLocalLastModified()+" <=> file last modified: "+file.lastModified() +": So return sync_conflict");
            return new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
        }

@@ -120,51 +92,51 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable {
            }
        }

        UploadFileRemoteOperation uploadOperation = buildUploadOperation(file, targetPath);
        final UploadFileRemoteOperation uploadOperation = buildUploadOperation(file, targetPath);

        // Execute UploadFileOperation
        RemoteOperationResult uploadResult = uploadOperation.execute(client );
        ResultCode mResultCode;
        ResultCode resultCode;
        boolean mustRestart = true;

        //if upload is a success
        if (uploadResult.isSuccess()) {
            Object data = uploadResult.getSingleData();
            if (data != null) {
                mSyncedState.setLastETAG((String) data);
                syncedState.setLastETAG((String) data);
            }
            mSyncedState.setLocalLastModified(file.lastModified());
            mResultCode = uploadResult.getCode();
            syncedState.setLocalLastModified(file.lastModified());
            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.FILE_NOT_FOUND ) {
                resultCode = ResultCode.FILE_NOT_FOUND;
                Log.d(TAG, "Catched a File not found result for : "+file.getName()+", create missing remote path then retry");
                String remoteFoldersPath = targetPath.substring( 0, targetPath.lastIndexOf(FileUtils.PATH_SEPARATOR)+1 );
                mResultCode = ResultCode.FILE_NOT_FOUND;
                CreateFolderRemoteOperation createFolderOperation = new CreateFolderRemoteOperation( remoteFoldersPath, true );
                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) {
                        mResultCode = createFolderResult.getCode();
                        resultCode = createFolderResult.getCode();
                        Log.e(TAG, createFolderResult.getLogMessage());
                        mustRestart = false;
                        mSyncedState.setLocalLastModified( this.previousLastModified);
                        syncedState.setLocalLastModified(this.previousLastModified);
                    }
                }catch(Exception e) {
                    Log.e(TAG, e.toString() );
                    mSyncedState.setLocalLastModified( this.previousLastModified);
                    syncedState.setLocalLastModified(this.previousLastModified);
                    mustRestart = false;
                }

            } else if (uploadResult.getCode() == ResultCode.QUOTA_EXCEEDED) {
                mResultCode = ResultCode.QUOTA_EXCEEDED;
                resultCode = ResultCode.QUOTA_EXCEEDED;
                mustRestart = false;
            } else {
                //Upload failed
                Log.e(TAG, "UploadFileRemoteOperation for : " + file.getName() + " failed => code: " + uploadResult.getCode());
                mResultCode = ResultCode.UNKNOWN_ERROR;
                resultCode = ResultCode.UNKNOWN_ERROR;
                mustRestart = false;
            }
        }
@@ -175,15 +147,15 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable {
                //if we encounter more than three times same error, stop trying to download.
                return this.run(client);
            } else {
                mSyncedState.setLocalLastModified( this.previousLastModified); //Revert syncFileState to its previous state
                syncedState.setLocalLastModified(this.previousLastModified); //Revert syncFileState to its previous state
            }
        }
        // updated syncedFile in DB
        DbHelper.manageSyncedFileStateDB(mSyncedState, "UPDATE", mContext);
        DbHelper.manageSyncedFileStateDB(syncedState, "UPDATE", context);

        ArrayList<Object> datas = new ArrayList<>();
        datas.add(mSyncedState.getSyncedFolderId());
        final RemoteOperationResult finalResult = new RemoteOperationResult(mResultCode);
        datas.add(syncedState.getSyncedFolderId());
        final RemoteOperationResult finalResult = new RemoteOperationResult(resultCode);
        finalResult.setData(datas);
        return finalResult;
    }
@@ -196,10 +168,10 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable {
        String timeStamp = ((Long) (file.lastModified() / 1000) ).toString() ;

        //create UploadFileOperation
        UploadFileRemoteOperation uploadRemoteFileOperation = new UploadFileRemoteOperation( mSyncedState.getLocalPath(),
                ( targetPath != null ) ? targetPath : mSyncedState.getRemotePath(),
        UploadFileRemoteOperation uploadRemoteFileOperation = new UploadFileRemoteOperation(syncedState.getLocalPath(),
                (targetPath != null ) ? targetPath : syncedState.getRemotePath(),
                CommonUtils.getMimeType(file ),
            ( !this.mSyncedState.isMediaType() || mSyncedState.getLastETAG().isEmpty() )? null : mSyncedState.getLastETAG(), //If not null, This can cause error 412; that means remote file has change
            (!this.syncedState.isMediaType() || syncedState.getLastETAG().isEmpty() )? null : syncedState.getLastETAG(), //If not null, This can cause error 412; that means remote file has change
            timeStamp );
        uploadRemoteFileOperation.askResultEtag(true);
        return uploadRemoteFileOperation;
@@ -229,17 +201,4 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable {
            return new RemoteOperationResult(ocsResult.getCode());
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(restartCounter);
        dest.writeLong(previousLastModified);
        dest.writeParcelable(mSyncedState, flags);
        dest.writeLong(availableQuota);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -222,7 +222,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation
        switch (request.getOperationType()){
            case UPLOAD:
                final SyncedFileState sfs = request.getSyncedFileState();
                operation = new UploadFileOperation(sfs);
                operation = new UploadFileOperation(sfs, getApplicationContext());
                break;
            case DOWNLOAD:
                final DownloadRequest downloadRequest = (DownloadRequest) request;