Commit 2cf1ffc5 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

reorganise run code of listFileRemoteOperation

parent 6de07962
Pipeline #20869 passed with stage
in 2 minutes and 22 seconds
...@@ -64,86 +64,86 @@ public class ListFileRemoteOperation extends RemoteOperation { ...@@ -64,86 +64,86 @@ public class ListFileRemoteOperation extends RemoteOperation {
//Get CurrentSyncedFolder //Get CurrentSyncedFolder
SyncedFolder syncedFolder = mSyncedFolderIterator.next(); SyncedFolder syncedFolder = mSyncedFolderIterator.next();
if(syncedFolder.isScanRemote()) { //If folder isn't to be scan remotly, ignore it
if(!syncedFolder.isScanRemote()) continue;
if(syncedFolder.getId() == -1) {
//persist new syncedFolder if(syncedFolder.getId() == -1) {
int syncedFolderId = (int) DbHelper.insertSyncedFolder(syncedFolder, mContext); //persist new syncedFolder
if (syncedFolderId > 0) { int syncedFolderId = (int) DbHelper.insertSyncedFolder(syncedFolder, mContext);
syncedFolder.setId(syncedFolderId); if (syncedFolderId > 0) {
} syncedFolder.setId(syncedFolderId);
}else{
mSyncedFolderIterator.remove();
Log.w(TAG, "syncedFolder "+syncedFolder.getRemoteFolder()+" doesn't have a valid ID");
} }
if(syncedFolder.getId() > 0){ }
//Create ReadRemoteOperation
LightReadFolderRemoteOperation operation = new LightReadFolderRemoteOperation(syncedFolder.getRemoteFolder(), DEPTH_1, false); //Create ReadRemoteOperation
RemoteOperationResult result = operation.execute(ownCloudClient); LightReadFolderRemoteOperation operation = new LightReadFolderRemoteOperation(syncedFolder.getRemoteFolder(), DEPTH_1, false);
RemoteOperationResult result = operation.execute(ownCloudClient);
if(result.isSuccess() ){
//is success then data can't be null if(result.isSuccess() ){
int dataSize = result.getData().size(); //is success then data can't be null
if(dataSize > 1){ //There is at least one subfiles int dataSize = result.getData().size();
RemoteFile directory = (RemoteFile) result.getData().get(0); if(dataSize > 1){ //There is at least one subfiles
if(!directory.getEtag().equals(syncedFolder.getLastEtag() )){ //if etag differs RemoteFile directory = (RemoteFile) result.getData().get(0);
List<Object> remoteFiles = result.getData().subList( 1, dataSize ); //get list of subfiles if(!directory.getEtag().equals(syncedFolder.getLastEtag() )){ //if etag differs
List<Object> remoteFiles = result.getData().subList( 1, dataSize ); //get list of subfiles
//loop through subelements
for (int i = -1, remoteFilesSize = remoteFiles.size(); ++i < remoteFilesSize; ){ //loop through subelements
RemoteFile remoteFile = (RemoteFile) remoteFiles.get(i); for (int i = -1, remoteFilesSize = remoteFiles.size(); ++i < remoteFilesSize; ){
RemoteFile remoteFile = (RemoteFile) remoteFiles.get(i);
//if remoteFile is in a "media" folder and its name start with "."
// then ignore it //if remoteFile is in a "media" folder and its name start with "."
if(syncedFolder.isMediaType() // then ignore it
&& CommonUtils.getFileNameFromPath( remoteFile.getRemotePath() ).startsWith(".") ){ if(syncedFolder.isMediaType()
continue; && CommonUtils.getFileNameFromPath( remoteFile.getRemotePath() ).startsWith(".") ){
} continue;
if( remoteFile.getMimeType().equals("DIR") ) {
String suffixPath = remoteFile.getRemotePath().substring( syncedFolder.getRemoteFolder().length() );
//but is it already known as SyncedFolder?
SyncedFolder subSyncedFolder = new SyncedFolder(syncedFolder, suffixPath, 0L, "" ); //need to set empty etag to allow it to be scan
mSyncedFolderIterator.add(subSyncedFolder);
mSyncedFolderIterator.previous();
}else {
//If it's a file just add it to mRemoteFiles.
mRemoteFiles.add(remoteFile);
}
}
syncedFolder.setLastEtag(directory.getEtag() ).setToSync(true);
atLeastOneDirAsChanged = true;
} }
}else if(dataSize == 1){ //Empty folder if( remoteFile.getMimeType().equals("DIR") ) {
RemoteFile directory = (RemoteFile) result.getData().get(0); String suffixPath = remoteFile.getRemotePath().substring( syncedFolder.getRemoteFolder().length() );
if(!directory.getEtag().equals(syncedFolder.getLastEtag())) {
syncedFolder.setLastEtag(directory.getEtag()).setToSync(true); //but is it already known as SyncedFolder?
atLeastOneDirAsChanged = true; SyncedFolder subSyncedFolder = new SyncedFolder(syncedFolder, suffixPath, 0L, "" ); //need to set empty etag to allow it to be scan
} mSyncedFolderIterator.add(subSyncedFolder);
}//Last else correspond to error 404 at LightReadFolderRemoteOperation (see below) mSyncedFolderIterator.previous();
}else{ //Result isn't a success
if(result.getHttpCode() == 404){ //File not found }else {
atLeastOneDirAsChanged = true; //If it's a file just add it to mRemoteFiles.
syncedFolder.setToSync(true); mRemoteFiles.add(remoteFile);
//If there is no remote file, then try to delete local one if empty. Finally remove Synced Folder from DB.
File localFolder = new File(syncedFolder.getLocalFolder());
if(localFolder.exists() && localFolder.listFiles().length == 0){
localFolder.delete();
}
if( !localFolder.exists() ) {
if (syncedFolder.getId() > this.initialFolderNumber/*-1*/) { //does the synced folder has been persisted?
//remove it from DB
int deleteResult = DbHelper.deleteSyncedFolder(syncedFolder.getId(), mContext);
Log.d(TAG, "syncedFolder Id: "+syncedFolder.getId() + " deletion from db return " + deleteResult + " row affected");
}
mSyncedFolderIterator.remove();
} }
} }
Log.w(TAG, "LightReadFolderRemoteOperation failed : http " + result.getHttpCode() + ", " + result.getLogMessage()+" => Ignored"); syncedFolder.setLastEtag(directory.getEtag() ).setToSync(true);
atLeastOneDirAsChanged = true;
}
}else if(dataSize == 1){ //Empty folder
RemoteFile directory = (RemoteFile) result.getData().get(0);
if(!directory.getEtag().equals(syncedFolder.getLastEtag())) {
syncedFolder.setLastEtag(directory.getEtag()).setToSync(true);
atLeastOneDirAsChanged = true;
}
}//Last else correspond to error 404 at LightReadFolderRemoteOperation (see below)
}else{ //Result isn't a success
if(result.getHttpCode() == 404){ //File not found
atLeastOneDirAsChanged = true;
syncedFolder.setToSync(true);
//If there is no remote file, then try to delete local one if empty. Finally remove Synced Folder from DB.
File localFolder = new File(syncedFolder.getLocalFolder());
if(localFolder.exists() && localFolder.listFiles().length == 0){
localFolder.delete();
}
if( !localFolder.exists() ) {
if (syncedFolder.getId() > this.initialFolderNumber/*-1*/) { //does the synced folder has been persisted?
//remove it from DB
int deleteResult = DbHelper.deleteSyncedFolder(syncedFolder.getId(), mContext);
Log.d(TAG, "syncedFolder Id: "+syncedFolder.getId() + " deletion from db return " + deleteResult + " row affected");
}
mSyncedFolderIterator.remove();
} }
}else{
mSyncedFolderIterator.remove();
Log.w(TAG, "syncedFolder "+syncedFolder.getRemoteFolder()+" doesn't have a valid ID");
} }
} //Then its folder is not scanRemote Log.w(TAG, "LightReadFolderRemoteOperation failed : http " + result.getHttpCode() + ", " + result.getLogMessage()+" => Ignored");
}
} //End of loop } //End of loop
finalResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.OK); finalResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.OK);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment