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

Commit 9f052686 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

replace propfind in uploadOperation by reading data from result header

parent ff516d14
Loading
Loading
Loading
Loading
Loading

android-nc-lib @ 14d81aba

Original line number Diff line number Diff line
Subproject commit 5e679bf04650877adec0f9e11a6cb482e70dc162
Subproject commit 14d81abab7dec100c6a23fd33f7cc0cdf557a3db
+0 −2
Original line number Diff line number Diff line
@@ -53,8 +53,6 @@ public class CreateInitialRemoteFolderOperation extends RemoteOperation {
        CreateFolderRemoteOperation createFolderOperation = new CreateFolderRemoteOperation(mRemotePath, mCreateFullPath);
        RemoteOperationResult createOperationResult  = createFolderOperation.execute(client, true);

        Log.d(TAG, createOperationResult.getLogMessage()+" \n"+createOperationResult.getCode()+
                " \n"+createOperationResult.getHttpCode()+" \n"+createOperationResult.getAuthenticateHeaders().toString());
        if(createOperationResult.isSuccess() || createOperationResult.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS ){
            if(DbHelper.insertSyncedFolder(mSyncedFolder, mContext) >= 0 ) {
                Log.d(TAG, "Insertion in DB succeed");
+8 −35
Original line number Diff line number Diff line
@@ -17,14 +17,11 @@ import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
import com.owncloud.android.lib.resources.files.model.RemoteFile;
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import io.eelo.drive.database.DbHelper;
import io.eelo.drive.models.SyncedFileState;
import io.eelo.drive.utils.CommonUtils;
@@ -151,7 +148,7 @@ public class UploadFileOperation extends RemoteOperation implements ComparableOp
                CommonUtils.getMimeType( mFile ),
                ( !checkEtag || mSyncedState.getLastETAG().isEmpty() )? null : mSyncedState.getLastETAG(), //If not null, This can cause error 412; that means remote file has change
                timeStamp );

        uploadRemoteFileOperation.askResultEtag(true);

        // Execute UploadFileOperation
        RemoteOperationResult uploadResult = uploadRemoteFileOperation.execute( client );
@@ -161,39 +158,15 @@ public class UploadFileOperation extends RemoteOperation implements ComparableOp
        //if upload is a success
        if( uploadResult.isSuccess() ){
            // get new Etag of the file on the server
            ReadFileRemoteOperation getEtagOp = new ReadFileRemoteOperation( mTargetPath );
            RemoteOperationResult readRemoteOpResult = getEtagOp.execute( client );

            //if readRemoteFile is a success
            if ( readRemoteOpResult.isSuccess() ) {
                List<Object> datas = readRemoteOpResult.getData();
                if(datas != null) {

                    //extract new Etag and update SyncedFileState;
                    RemoteFile rf = (RemoteFile) datas.get(0);

                    //If length differs then there is an issue during transfer
                    if( rf.getLength() != this.mFile.length() ){
                        Log.e(TAG, "Local and remote file doesn't have the same size.\n"+restartCounter
                                +" unsuccessfull trial(s) of uploading file "
                                +rf.getRemotePath() );
                        mResultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE;

                    }else {
                        mSyncedState.setLastETAG(rf.getEtag());
                        mSyncedState.setLocalLastModified(mFile.lastModified());
                        mResultCode = RemoteOperationResult.ResultCode.OK;
                        mustRestart = false;
                    }
                }else{
                    Log.e(TAG, "No data in readRemoteResult ");
                    mResultCode = RemoteOperationResult.ResultCode.CONFLICT;
                }
            }else{
                //readRemoteOperation failed
                Log.e( TAG, "ReadRemoteFileOperation => Uploaded file not found on server " );
                mResultCode = RemoteOperationResult.ResultCode.CONFLICT;
            Object data = uploadResult.getSingleData();
            if(data != null){
                String etag = (String) data;
                Log.d(TAG, "got new etag from PUT result: "+etag);
                mSyncedState.setLastETAG(etag);
            }
            mSyncedState.setLocalLastModified(mFile.lastModified());
            mResultCode = uploadResult.getCode();
        }else{
            //Si les répértoires ou mettre le fichier n'existe pas, on les ajoutes.
            if( uploadResult.getCode() == RemoteOperationResult.ResultCode.FILE_NOT_FOUND ){
+178 −71
Original line number Diff line number Diff line
@@ -8,16 +8,15 @@
package io.eelo.drive.Test;

import android.support.annotation.NonNull;

import com.owncloud.android.lib.resources.files.model.ISynchronizableContent;
import com.owncloud.android.lib.resources.files.model.RemoteFile;
import com.owncloud.android.lib.resources.files.model.SynchronizableFile;
import com.owncloud.android.lib.resources.files.model.SynchronizableFileInfo;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -32,21 +31,12 @@ import io.eelo.drive.models.Operation;
 */

public class ObserverServiceUnitTest {


    List<RemoteFile> previousRemoteFileList;
    List<RemoteFile> newRemoteFileList;
    List<SynchronizableFile> newLocalFileList;
    List<SynchronizableFile> oldLocalFileList;
    List<String> pathListToIgnore;
    List<SyncedFolder> initialFolder;


    @Before
    public void initForBuildOperationList(){
        this.newLocalFileList = getNewLocalFileList();

    }
    private List<RemoteFile> previousRemoteFileList;
    private List<RemoteFile> newRemoteFileList;
    private List<SynchronizableFileInfo> newLocalFileList;
    private List<SynchronizableFileInfo> oldLocalFileList;
    private List<String> pathListToIgnore;
    private List<SyncedFolder> initialFolder;

    @Before
    public void initForModifDetection(){
@@ -64,31 +54,29 @@ public class ObserverServiceUnitTest {
        this.pathListToIgnore = new ArrayList<>();
        Assert.assertTrue(pathListToIgnore.isEmpty());
        Assert.assertNotNull(pathListToIgnore);


    }

    @Test
    public void FindModificationUnitTest(){

        System.out.println("-----Test method which detech change-----");
        //Test with two new file, one deleted file and one ignored path
        System.out.println("TEST 1 :");
        System.out.println("Description : Test with two new file, one deleted file and one ignored path");
        findRemoteModification();
        findModification(previousRemoteFileList, newRemoteFileList);
        Assert.assertTrue("wrong number of path to ignore", pathListToIgnore.size() == 1);
        Assert.assertTrue("doesn't contains the expected path", pathListToIgnore.get(0).equals("/Videos/"));
        Assert.assertTrue("Wrong number of removed file", previousRemoteFileList.size() == 1);
        Assert.assertTrue("Wrong number of new files (and folder) ", newRemoteFileList.size() == 3);
        showResult();
        showRemoteResult();

        //Test a case where there is already a '/photos/' to ignore. So What is expected is that new list is empty.
        System.out.println("\nTEST 2 :");
        System.out.println("Description: Test with already a '/Photos/' in PathToIgnoreList");
        initForModifDetection();
        pathListToIgnore.add("/Photos/");
        findRemoteModification();
        findModification(previousRemoteFileList, newRemoteFileList);
        Assert.assertTrue("wrong number of new file", newRemoteFileList.isEmpty());
        showResult();
        showRemoteResult();

        //Test the case where nothing change
        System.out.println("\nTEST 3 :");
@@ -96,8 +84,8 @@ public class ObserverServiceUnitTest {
        pathListToIgnore.clear();
        previousRemoteFileList = getRemoteOldList();
        newRemoteFileList = getRemoteOldList();
        findRemoteModification();
        showResult();
        findModification(previousRemoteFileList, newRemoteFileList);
        showRemoteResult();

        //Test with an empty new List
        System.out.println("\nTEST 4 :");
@@ -105,8 +93,8 @@ public class ObserverServiceUnitTest {
        pathListToIgnore.clear();
        newRemoteFileList.clear();
        previousRemoteFileList = getRemoteOldList();
        findRemoteModification();
        showResult();
        findModification(previousRemoteFileList, newRemoteFileList);
        showRemoteResult();

        //Test with an empty old List
        System.out.println("\nTEST 5 :");
@@ -114,8 +102,8 @@ public class ObserverServiceUnitTest {
        pathListToIgnore.clear();
        previousRemoteFileList.clear();
        newRemoteFileList = getRemoteNewList();
        findRemoteModification();
        showResult();
        findModification(previousRemoteFileList, newRemoteFileList);
        showRemoteResult();

        //Test with /Photos/DCIM in previousRemoteFileList too
        System.out.println("\nTEST 6 :");
@@ -125,23 +113,95 @@ public class ObserverServiceUnitTest {
        instance.setEtag("yLd1");
        instance.setMimeType("DIR");
        previousRemoteFileList.add(instance);
        findRemoteModification();
        findModification(previousRemoteFileList, newRemoteFileList);
        Assert.assertFalse("New list contain wrong file", newRemoteFileList.contains(instance));
        Assert.assertFalse("previous list contain wrong file", previousRemoteFileList.contains(instance));
        showResult();
        showRemoteResult();

        //Test with /Photos/DCIM/fleur.png in pathlistToIgnore
        System.out.println("\nTEST 7 :");
        System.out.println("Description : '/photos/DCIM/fleur.png' is in pathListToIgnore at the begining");
        initForModifDetection();
        pathListToIgnore.add("/Photos/DCIM/fleur.png");
        findRemoteModification();
        showResult();
        findModification(previousRemoteFileList, newRemoteFileList);
        showRemoteResult();
        Assert.assertFalse("new list contain file that should be ignored", newRemoteFileList.contains(instance));

    }


    public void findModification(List<? extends ISynchronizableContent> oldList, List<? extends ISynchronizableContent> newList){

        if(newList.isEmpty() || oldList.isEmpty()) return;

        //pathListToIgnore.add("/Photos/");
        if(oldList.get(0).getHash().equals(newList.get(0).getHash() )){
            System.out.println("Nothing has change");
            pathListToIgnore.add(oldList.get(0).getPath());
            oldList.clear();
            newList.clear();
            return;
        }

        oldList.remove(0);
        newList.remove(0);

        ListIterator<? extends ISynchronizableContent> oldIterator = oldList.listIterator();

        while (oldIterator.hasNext()){
            ISynchronizableContent oldversion = oldIterator.next();

            String oldPath = oldversion.getPath();
            boolean ignoreOld = false;

            ListIterator<? extends ISynchronizableContent> newIterator = newList.listIterator();
            boolean equivalentFound = false;

            while (!equivalentFound && newIterator.hasNext()){
                boolean ignoreNew = false;
                ISynchronizableContent newVersion = newIterator.next();

                for(int i = -1, size = pathListToIgnore.size(); ++i < size;){
                    String pathToIgnore = pathListToIgnore.get(i);
                    if(!ignoreOld && oldPath.startsWith(pathToIgnore))
                        ignoreOld = true;
                    if(newVersion.getPath().startsWith(pathToIgnore)) {
                        ignoreNew = true;
                        i = size;
                        newIterator.remove();
                    }
                }

                if(!ignoreNew && newVersion.getPath().equals(oldPath )){

                    if(oldversion.isDir()) {

                        //This is a folder
                        if (oldversion.getHash().equals(newVersion.getHash())) {
                            //Which hasn't change'
                            pathListToIgnore.add(oldPath);
                        }
                        newIterator.remove();
                        oldIterator.remove();
                    }else {
                        //This is a file
                        if (oldversion.getHash().equals(newVersion.getHash())) {
                            //Which hasn't change'
                            newIterator.remove();
                            oldIterator.remove();
                        }
                    }
                    equivalentFound = true;
                }
            }
            if(ignoreOld ) oldIterator.remove();
        }
    }


    /**
     * First implementation of the method. Only work for remote file
     */
    public void findRemoteModification(){

        if(newRemoteFileList.isEmpty() || previousRemoteFileList.isEmpty()) return;
@@ -210,7 +270,10 @@ public class ObserverServiceUnitTest {
        }
    }

    private void showResult(){
    /**
     * print oldRemoteFileListe and newRemoteFileListe
     */
    private void showRemoteResult(){
        System.out.println("Folder that hasn't changed: "+pathListToIgnore);
        String newListDisplay ="";
        for(RemoteFile o : newRemoteFileList){
@@ -333,15 +396,21 @@ public class ObserverServiceUnitTest {
        initialFolder.add(new SyncedFolder("Music", "/sdcard/Musics/", "/Musics/", true));
        initialFolder.add(new SyncedFolder("Settings", "/user/0/", "/Settings/", true));


        try {
            System.out.println(new File("/sdcard/videos/").getCanonicalPath());
        }
        catch (IOException e){
            System.out.println(e.toString());
        }
        Assert.assertTrue("Wrong number of initialFOlder", initialFolder.size() == 4);
    }


    @Test
    public void getOtherPathUnitTest(){
        System.out.println("\n----Test for getOtherPath method -----");
        initForGetOtherPath();


        System.out.println("TEST 1");
        System.out.println("Description: simple case");
        String path = "/Musics/metallica/blackAlbum/nothingElseMatters.ogg";
@@ -399,23 +468,13 @@ public class ObserverServiceUnitTest {
    public String getOtherPath(@NonNull ISynchronizableContent file) throws IllegalArgumentException{
        if(file == null) throw new IllegalArgumentException();

        String path ;
        if(file.getClass().equals(RemoteFile.class)){
            RemoteFile rf = (RemoteFile) file;
             path = rf.getRemotePath();
        }else{
            SynchronizableFile sf = (SynchronizableFile) file;
            try {
                 path = sf.getCanonicalPath();
            }catch (Exception e){
                 path = sf.getAbsolutePath();
            }
        }
        String path  = file.getPath();
        if(path == null) throw new IllegalArgumentException();

        for(SyncedFolder sf : initialFolder){
            String FolderPath;
            String newPath;
            if(file.getClass().equals(SynchronizableFile.class) ){
            if(file.getClass().equals(SynchronizableFileInfo.class) ){
                FolderPath = sf.getLocalFolder();
                newPath = sf.getRemoteFolder();
            }else{
@@ -431,14 +490,30 @@ public class ObserverServiceUnitTest {


    /* --------------------------------Test list of file to list of operation to do----------------------------------------*/
    @Before
    public void initForBuildOperationList(){
        this.newLocalFileList = getNewLocalFileList();
        this.oldLocalFileList = getOldLocalFileList();
    }


    @Test
    public void handleChangementFoundUnitTest(){

        System.out.println("----Test conversion of changement to operation-----");
        initForBuildOperationList();
        initForModifDetection();
        findRemoteModification();
        showResult();
        findModification(previousRemoteFileList, newRemoteFileList);
        System.out.println("Show remote modif: ");
        showRemoteResult();


        findModification(oldLocalFileList, newLocalFileList);
        System.out.println("Show local modif: ");
        showLocalResult();



        System.out.println("\n");
        HashMap<String, Operation> result = buildOperationList();
        Assert.assertTrue("Wrong size", result.size() == 3);
@@ -469,24 +544,24 @@ public class ObserverServiceUnitTest {

        //Handle change found on device

        List<SynchronizableFile> newLocalFileList = new ArrayList<>();
        for(SynchronizableFile sf : newLocalFileList){
        List<SynchronizableFileInfo> newLocalFileList = new ArrayList<>();
        for(SynchronizableFileInfo sf : newLocalFileList){
            try {
                String localPath = sf.getCanonicalPath();
                String localPath = sf.getPath();
                Operation operation = operationToDo.get(localPath);
                if (operation != null && operation.getFile().getLastModified() <= sf.getLastModified()) {
                    operationToDo.remove(localPath);
                    operationToDo.put(getOtherPath(sf), new Operation(sf, sf.isDirectory()? "MKCol":"Upload"));
                    operationToDo.put(getOtherPath(sf), new Operation(sf, sf.isDir()? "MKCol":"Upload"));
                }
            }catch(Exception e){
                //Todo
            }
        }

        List<SynchronizableFile> oldLocalFileList = new ArrayList<>();
        for(SynchronizableFile sf : oldLocalFileList){
        List<SynchronizableFileInfo> oldLocalFileList = new ArrayList<>();
        for(SynchronizableFileInfo sf : oldLocalFileList){
            try {
                String localPath = sf.getCanonicalPath();
                String localPath = sf.getPath();
                Operation operation = operationToDo.get(localPath);
                if (operation != null && operation.getFile().getLastModified() <= sf.getLastModified()) {
                    operationToDo.remove(localPath);
@@ -507,24 +582,56 @@ public class ObserverServiceUnitTest {
    }

    /**
     * Private method that mock the work done on device
     *  method that mock the file done on device
     * @return
     */
    private List<SynchronizableFile> getNewLocalFileList(){
        ArrayList<SynchronizableFile> result = new ArrayList<>();
    private List<SynchronizableFileInfo> getNewLocalFileList(){
        ArrayList<SynchronizableFileInfo> result = new ArrayList<>();
        File t;
        result.add( new SynchronizableFile("/sdcard/Photos/"));
        result.add( new SynchronizableFile("/sdcard/Photos/OpenCamera/"));
        result.add( new SynchronizableFile("/sdcard/Photos/OpenCamera/photo1.jpg"));
        result.add( new SynchronizableFile("/sdcard/videos/"));
        result.add( new SynchronizableFile("/sdcard/Musics/"));
        result.add( new SynchronizableFile("/sdcard/Musics/Metallica/"));
        result.add(new SynchronizableFile("/sdcard/Musics/Metallica/SaintAnger/"));
        result.add(new SynchronizableFile("/sdcard/Musics/Metallica/SaintAnger/TU3.ogg"));
        result.add( new SynchronizableFileInfo("aaaa", "/sdcard/Photos/", 44556677, true));
        result.add( new SynchronizableFileInfo("a1b0","/sdcard/Photos/OpenCamera/", 44556678, true));
        result.add( new SynchronizableFileInfo("1a0b","/sdcard/Photos/OpenCamera/photo1.jpg", 44556678, false));
        result.add( new SynchronizableFileInfo("ab12","/sdcard/videos/", 22334455, true));
        result.add( new SynchronizableFileInfo("cq16","/sdcard/Musics/", 55667788, true));
        result.add( new SynchronizableFileInfo("cq18","/sdcard/Musics/Metallica/", 55667789, true));
        result.add(new SynchronizableFileInfo("tvtv","/sdcard/Musics/Metallica/SaintAnger/", 55667810, true));
        result.add(new SynchronizableFileInfo("qvxz","/sdcard/Musics/Metallica/SaintAnger/TU3.ogg", 55667810, false));
        Assert.assertTrue("Wrong size", result.size() == 8);
        return result;
    }

    /**
     * method that mock the previous observation of file on device
     * @return
     */
    private List<SynchronizableFileInfo> getOldLocalFileList(){
        ArrayList<SynchronizableFileInfo> result = new ArrayList<>();
        File t;
        result.add( new SynchronizableFileInfo("p136", "/sdcard/Photos/", 33112244, true));
        result.add( new SynchronizableFileInfo("tt52", "/sdcard/Photos/OpenCamera/", 33112245, true));
        result.add( new SynchronizableFileInfo("28dc", "/sdcard/Photos/OpenCamera/photo0.jpg", 33112245, false));
        result.add( new SynchronizableFileInfo("ab12", "/sdcard/videos/", 22334455, true));
        result.add( new SynchronizableFileInfo("cq16", "/sdcard/Musics/", 55667788, true));
        result.add( new SynchronizableFileInfo("cq18", "/sdcard/Musics/Metallica/", 55667789, true));
        result.add(new SynchronizableFileInfo("tvtv", "/sdcard/Musics/Metallica/SaintAnger/",55667810 , true));
        result.add(new SynchronizableFileInfo("qvxz", "/sdcard/Musics/Metallica/SaintAnger/TU3.ogg", 55667810, false));
        Assert.assertTrue("Wrong size", result.size() == 8);

        return result;
    }

    private void showLocalResult(){
        System.out.println("Folder that hasn't changed: "+pathListToIgnore);
        String newListDisplay ="";
        for(SynchronizableFileInfo o : newLocalFileList){
            newListDisplay+=o.getPath()+", ";
        }
        System.out.println("New List / New or updated file : ["+newListDisplay+"]");

        String oldListDisplay ="";
        for(SynchronizableFileInfo o : oldLocalFileList){
            oldListDisplay+=o.getPath()+", ";
        }
        System.out.println("OldList / File that have been removed : ["+oldListDisplay+"]");
    }
}