Эх сурвалжийг харах

store & re-use token for retry of failed encrypted upload

tobiasKaminsky 7 жил өмнө
parent
commit
ade5bfe226

+ 3 - 1
src/main/java/com/owncloud/android/datamodel/UploadsStorageManager.java

@@ -123,7 +123,7 @@ public class UploadsStorageManager extends Observable {
         cv.put(ProviderTableMeta.UPLOADS_CREATED_BY, ocUpload.getCreadtedBy());
         cv.put(ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY, ocUpload.isWhileChargingOnly() ? 1 : 0);
         cv.put(ProviderTableMeta.UPLOADS_IS_WIFI_ONLY, ocUpload.isUseWifiOnly() ? 1 : 0);
-
+        cv.put(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN, ocUpload.getFolderUnlockToken());
         Uri result = getDB().insert(ProviderTableMeta.CONTENT_URI_UPLOADS, cv);
 
         Log_OC.d(TAG, "storeUpload returns with: " + result + " for file: " + ocUpload.getLocalPath());
@@ -155,6 +155,7 @@ public class UploadsStorageManager extends Observable {
         cv.put(ProviderTableMeta.UPLOADS_LAST_RESULT, ocUpload.getLastResult().getValue());
         cv.put(ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP, ocUpload.getUploadEndTimestamp());
         cv.put(ProviderTableMeta.UPLOADS_FILE_SIZE, ocUpload.getFileSize());
+        cv.put(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN, ocUpload.getFolderUnlockToken());
 
         int result = getDB().update(ProviderTableMeta.CONTENT_URI_UPLOADS,
                 cv,
@@ -379,6 +380,7 @@ public class UploadsStorageManager extends Observable {
             upload.setUseWifiOnly(c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_WIFI_ONLY)) == 1);
             upload.setWhileChargingOnly(c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY))
                     == 1);
+            upload.setFolderUnlockToken(c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN)));
         }
         return upload;
     }

+ 17 - 1
src/main/java/com/owncloud/android/db/OCUpload.java

@@ -112,7 +112,12 @@ public class OCUpload implements Parcelable {
      */
     private boolean mIsWhileChargingOnly;
 
-     /**
+    /**
+     * Token to unlock E2E folder
+     */
+    private String mFolderUnlockToken;
+
+    /**
      * Main constructor.
      *
      * @param localPath         Absolute path in the local file system to the file to be uploaded.
@@ -162,6 +167,7 @@ public class OCUpload implements Parcelable {
         mCreatedBy = UploadFileOperation.CREATED_BY_USER;
         mIsUseWifiOnly = true;
         mIsWhileChargingOnly = false;
+        mFolderUnlockToken = "";
     }
 
     // Getters & Setters
@@ -372,6 +378,14 @@ public class OCUpload implements Parcelable {
         return mIsWhileChargingOnly;
     }
 
+    public void setFolderUnlockToken(String token) {
+        mFolderUnlockToken = token;
+    }
+
+    public String getFolderUnlockToken() {
+        return mFolderUnlockToken;
+    }
+
     /**
      * Reconstruct from parcel
      *
@@ -403,6 +417,7 @@ public class OCUpload implements Parcelable {
         mCreatedBy = source.readInt();
         mIsUseWifiOnly = (source.readInt() == 1);
         mIsWhileChargingOnly = (source.readInt() == 1);
+        mFolderUnlockToken = source.readString();
     }
 
     @Override
@@ -425,6 +440,7 @@ public class OCUpload implements Parcelable {
         dest.writeInt(mCreatedBy);
         dest.writeInt(mIsUseWifiOnly ? 1 : 0);
         dest.writeInt(mIsWhileChargingOnly ? 1 : 0);
+        dest.writeString(mFolderUnlockToken);
     }
 
     enum CanUploadFileNowStatus {NOW, LATER, FILE_GONE, ERROR}

+ 1 - 0
src/main/java/com/owncloud/android/db/ProviderMeta.java

@@ -186,6 +186,7 @@ public class ProviderMeta {
         public static final String UPLOADS_DEFAULT_SORT_ORDER = ProviderTableMeta._ID + " collate nocase desc";
         public static final String UPLOADS_IS_WHILE_CHARGING_ONLY = "is_while_charging_only";
         public static final String UPLOADS_IS_WIFI_ONLY = "is_wifi_only";
+        public static final String UPLOADS_FOLDER_UNLOCK_TOKEN = "folder_unlock_token";
 
         // Columns of synced folder table
         public static final String SYNCED_FOLDER_LOCAL_PATH = "local_path";

+ 26 - 6
src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -113,6 +113,7 @@ public class UploadFileOperation extends SyncOperation {
      */
     private OCFile mOldFile;
     private String mRemotePath = null;
+    private String mFolderUnlockToken;
     private boolean mChunked = false;
     private boolean mRemoteFolderToBeCreated = false;
     private boolean mForceOverwrite = false;
@@ -141,6 +142,7 @@ public class UploadFileOperation extends SyncOperation {
     protected RequestEntity mEntity = null;
 
     private Account mAccount;
+    private OCUpload mUpload;
     private UploadsStorageManager uploadsStorageManager;
 
     public static OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType) {
@@ -192,6 +194,7 @@ public class UploadFileOperation extends SyncOperation {
         }
 
         mAccount = account;
+        mUpload = upload;
         if (file == null) {
             mFile = obtainNewOCFileToUpload(
                     upload.getRemotePath(),
@@ -214,6 +217,7 @@ public class UploadFileOperation extends SyncOperation {
         mRemoteFolderToBeCreated = upload.isCreateRemoteFolder();
         // Ignore power save mode only if user explicitly created this upload
         mIgnoringPowerSaveMode = (mCreatedBy == CREATED_BY_USER);
+        mFolderUnlockToken = upload.getFolderUnlockToken();
     }
 
     public boolean getIsWifiRequired() {
@@ -362,19 +366,29 @@ public class UploadFileOperation extends SyncOperation {
             }
         }
 
-        /// check the existence of the parent folder for the file to upload
+        // check the existence of the parent folder for the file to upload
         String remoteParentPath = new File(getRemotePath()).getParent();
         remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
                 remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
+
+        OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
+        mFile.setParentId(parent.getFileId());
+
+        if (parent.isEncrypted()) {
+            UnlockFileOperation unlockFileOperation = new UnlockFileOperation(parent.getLocalId(), mFolderUnlockToken);
+            RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client);
+
+            if (!unlockFileOperationResult.isSuccess()) {
+                return unlockFileOperationResult;
+            }
+        }
+
         RemoteOperationResult result = grantFolderExistence(remoteParentPath, client);
 
         if (!result.isSuccess()) {
             return result;
         }
 
-        OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
-        mFile.setParentId(parent.getFileId());
-
         if (parent.isEncrypted()) {
             Log_OC.d(TAG, "encrypted upload");
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@@ -619,6 +633,11 @@ public class UploadFileOperation extends SyncOperation {
 //                }
 //            }
 
+//            boolean test = true;
+//            if (test) {
+//                throw new Exception("test");
+//            }
+            
             result = mUploadOperation.execute(client);
 //            if (result == null || result.isSuccess() && mUploadOperation != null) {
 //                result = mUploadOperation.execute(client);
@@ -677,8 +696,9 @@ public class UploadFileOperation extends SyncOperation {
         } finally {
             mUploadStarted.set(false);
 
-            // unlock folder
-            unlockFolder(parentFile, client, token);
+            // if something fails, store token and retry again
+            mUpload.setFolderUnlockToken(token);
+            uploadsStorageManager.updateUpload(mUpload);
 
             if (fileLock != null) {
                 try {

+ 17 - 0
src/main/java/com/owncloud/android/providers/FileContentProvider.java

@@ -1557,6 +1557,23 @@ public class FileContentProvider extends ContentProvider {
             }
 
 
+            if (!upgraded) {
+                Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
+            }
+
+            if (oldVersion < 26 && newVersion >= 26) {
+                Log_OC.i(SQL, "Entering in the #26 Adding token to ocUpload");
+                db.beginTransaction();
+                try {
+                    db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
+                            ADD_COLUMN + ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN + " TEXT ");
+                    upgraded = true;
+                    db.setTransactionSuccessful();
+                } finally {
+                    db.endTransaction();
+                }
+            }
+
             if (!upgraded) {
                 Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
             }