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 f4ec50aeb17a968c023e1c3c954a3a10068ffa4f..e0fdfe24069d3e037d5f3bc4fb26bcb357101857 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -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 CREATOR = new Creator() { - @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 void setContext(Context context){ - this.mContext = context; + public UploadFileOperation (SyncedFileState syncedFileState, Context context) { + this.syncedState = syncedFileState; + this.previousLastModified = syncedState.getLocalLastModified(); + this.context = context; } - - /** * Execute the operation: * @@ -91,99 +63,99 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable { * hasn't change since last update or "forbidden" if no remote path can be fetch. */ @Override - protected RemoteOperationResult run( OwnCloudClient client ) { + 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()); - if(file == null || !file.exists()){ + 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); } - if(this.availableQuota == -1){ + if (this.availableQuota == -1) { RemoteOperationResult checkQuotaResult = checkAvailableSpace(client, file.length()); - if( checkQuotaResult.getCode() != ResultCode.OK ){ + if (checkQuotaResult.getCode() != ResultCode.OK) { return new RemoteOperationResult(checkQuotaResult.getCode()); } } - UploadFileRemoteOperation uploadOperation = buildUploadOperation(file, targetPath); + final UploadFileRemoteOperation uploadOperation = buildUploadOperation(file, targetPath); // Execute UploadFileOperation - RemoteOperationResult uploadResult = uploadOperation.execute( client ); - ResultCode mResultCode; + RemoteOperationResult uploadResult = uploadOperation.execute(client ); + ResultCode resultCode; boolean mustRestart = true; //if upload is a success - if( uploadResult.isSuccess() ){ + if (uploadResult.isSuccess()) { Object data = uploadResult.getSingleData(); - if(data != null){ - mSyncedState.setLastETAG((String) data); + if (data != null) { + syncedState.setLastETAG((String) data); } - mSyncedState.setLocalLastModified(file.lastModified()); - mResultCode = uploadResult.getCode(); + syncedState.setLocalLastModified(file.lastModified()); + resultCode = uploadResult.getCode(); mustRestart = false; - }else{ + } else { //Si les répértoires ou mettre le fichier n'existe pas, on les ajoutes. - if( uploadResult.getCode() == ResultCode.FILE_NOT_FOUND ){ + 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 ); + RemoteOperationResult createFolderResult = createFolderOperation.execute(client ); - if(!createFolderResult.isSuccess() && createFolderResult.getCode() != ResultCode.FOLDER_ALREADY_EXISTS){ - mResultCode = createFolderResult.getCode(); + if (!createFolderResult.isSuccess() && createFolderResult.getCode() != ResultCode.FOLDER_ALREADY_EXISTS) { + resultCode = createFolderResult.getCode(); Log.e(TAG, createFolderResult.getLogMessage()); mustRestart = false; - mSyncedState.setLocalLastModified( this.previousLastModified); + syncedState.setLocalLastModified(this.previousLastModified); } - }catch(Exception e){ + }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; + } else if (uploadResult.getCode() == ResultCode.QUOTA_EXCEEDED) { + resultCode = ResultCode.QUOTA_EXCEEDED; mustRestart = false; - }else{ + } else { //Upload failed Log.e(TAG, "UploadFileRemoteOperation for : " + file.getName() + " failed => code: " + uploadResult.getCode()); - mResultCode = ResultCode.UNKNOWN_ERROR; + resultCode = ResultCode.UNKNOWN_ERROR; mustRestart = false; } } - if(mustRestart) { + if (mustRestart) { if (this.restartCounter < 1) { this.restartCounter += 1; //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 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; } @@ -192,14 +164,14 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable { * Build the operation to put the file on server * @return the operation to execute */ - private UploadFileRemoteOperation buildUploadOperation(File file, String targetPath){ - String timeStamp = ( (Long) ( file.lastModified() / 1000) ).toString() ; + private UploadFileRemoteOperation buildUploadOperation(File file, String targetPath) { + String timeStamp = ((Long) (file.lastModified() / 1000) ).toString() ; //create UploadFileOperation - UploadFileRemoteOperation uploadRemoteFileOperation = new UploadFileRemoteOperation( mSyncedState.getLocalPath(), - ( targetPath != null ) ? targetPath : mSyncedState.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 + UploadFileRemoteOperation uploadRemoteFileOperation = new UploadFileRemoteOperation(syncedState.getLocalPath(), + (targetPath != null ) ? targetPath : syncedState.getRemotePath(), + CommonUtils.getMimeType(file ), + (!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; @@ -211,35 +183,22 @@ public class UploadFileOperation extends RemoteOperation implements Parcelable { * @return RemoteOperationResult */ @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - public RemoteOperationResult checkAvailableSpace(OwnCloudClient client, long fileSize){ + public RemoteOperationResult checkAvailableSpace(OwnCloudClient client, long fileSize) { GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); - if(ocsResult.isSuccess() && ocsResult.getData() != null){ + if (ocsResult.isSuccess() && ocsResult.getData() != null) { UserInfo userInfo = (UserInfo) ocsResult.getData().get(0); this.availableQuota = userInfo.getQuota().getFree(); - if( ((UserInfo) ocsResult.getData().get(0)).getQuota().getFree() < fileSize ) { + if (((UserInfo) ocsResult.getData().get(0)).getQuota().getFree() < fileSize ) { Log.w(TAG, "quota exceeded!"); return new RemoteOperationResult(ResultCode.QUOTA_EXCEEDED); - }else{ + } else { Log.d(TAG, "Quota Okay"); return new RemoteOperationResult(ResultCode.OK); } - }else{ + } else { Log.w(TAG, "getRemoteUserInfoOperation failed: "+ocsResult.getHttpCode() ); 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); - } } diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index e03d4a55e343dfd6403ef4bf0300b1837963a8ae..cd4de9feda26533c6e24d479c04b6a3009767b91 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -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;