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 {
//Get CurrentSyncedFolder
SyncedFolder syncedFolder = mSyncedFolderIterator.next();
if(syncedFolder.isScanRemote()) {
if(syncedFolder.getId() == -1) {
//persist new syncedFolder
int syncedFolderId = (int) DbHelper.insertSyncedFolder(syncedFolder, mContext);
if (syncedFolderId > 0) {
syncedFolder.setId(syncedFolderId);
}
//If folder isn't to be scan remotly, ignore it
if(!syncedFolder.isScanRemote()) continue;
if(syncedFolder.getId() == -1) {
//persist new syncedFolder
int syncedFolderId = (int) DbHelper.insertSyncedFolder(syncedFolder, mContext);
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);
RemoteOperationResult result = operation.execute(ownCloudClient);
if(result.isSuccess() ){
//is success then data can't be null
int dataSize = result.getData().size();
if(dataSize > 1){ //There is at least one subfiles
RemoteFile directory = (RemoteFile) result.getData().get(0);
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; ){
RemoteFile remoteFile = (RemoteFile) remoteFiles.get(i);
//if remoteFile is in a "media" folder and its name start with "."
// then ignore it
if(syncedFolder.isMediaType()
&& 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;
}
//Create ReadRemoteOperation
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
int dataSize = result.getData().size();
if(dataSize > 1){ //There is at least one subfiles
RemoteFile directory = (RemoteFile) result.getData().get(0);
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; ){
RemoteFile remoteFile = (RemoteFile) remoteFiles.get(i);
//if remoteFile is in a "media" folder and its name start with "."
// then ignore it
if(syncedFolder.isMediaType()
&& CommonUtils.getFileNameFromPath( remoteFile.getRemotePath() ).startsWith(".") ){
continue;
}
}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();
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);
}
}
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
finalResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.OK);
......
Markdown is supported
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