Commit 940af700 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

cherry pick 2cf1ffc5

parent 79b3e0c6
Pipeline #20902 passed with stage
in 2 minutes and 10 seconds
...@@ -64,87 +64,84 @@ public class ListFileRemoteOperation extends RemoteOperation { ...@@ -64,87 +64,84 @@ public class ListFileRemoteOperation extends RemoteOperation {
//Get CurrentSyncedFolder //Get CurrentSyncedFolder
SyncedFolder syncedFolder = mSyncedFolderIterator.next(); SyncedFolder syncedFolder = mSyncedFolderIterator.next();
if(syncedFolder.isScanRemote()) { if(!syncedFolder.isScanRemote()) continue;
if(syncedFolder.getId() == -1) { if(syncedFolder.getId() == -1) {
//persist new syncedFolder //persist new syncedFolder
int syncedFolderId = (int) DbHelper.insertSyncedFolder(syncedFolder, mContext); int syncedFolderId = (int) DbHelper.insertSyncedFolder(syncedFolder, mContext);
if (syncedFolderId > 0) { if (syncedFolderId > 0) {
syncedFolder.setId(syncedFolderId); syncedFolder.setId(syncedFolderId);
} }else {
mSyncedFolderIterator.remove();
Log.w(TAG, "syncedFolder "+syncedFolder.getRemoteFolder()+" doesn't have a valid ID");
} }
if(syncedFolder.getId() > 0){ }
//Create ReadRemoteOperation //Create ReadRemoteOperation
LightReadFolderRemoteOperation operation = new LightReadFolderRemoteOperation(syncedFolder.getRemoteFolder(), DEPTH_1, false); LightReadFolderRemoteOperation operation = new LightReadFolderRemoteOperation(syncedFolder.getRemoteFolder(), DEPTH_1, false);
RemoteOperationResult result = operation.execute(ownCloudClient); RemoteOperationResult result = operation.execute(ownCloudClient);
if(result.isSuccess() ){ if(result.isSuccess() ){
//is success then data can't be null //is success then data can't be null
int dataSize = result.getData().size(); int dataSize = result.getData().size();
if(dataSize > 1){ //There is at least one subfiles if(dataSize > 1){ //There is at least one subfiles
RemoteFile directory = (RemoteFile) result.getData().get(0); RemoteFile directory = (RemoteFile) result.getData().get(0);
if(!directory.getEtag().equals(syncedFolder.getLastEtag() )){ //if etag differs if(!directory.getEtag().equals(syncedFolder.getLastEtag() )){ //if etag differs
List<Object> remoteFiles = result.getData().subList( 1, dataSize ); //get list of subfiles List<Object> remoteFiles = result.getData().subList( 1, dataSize ); //get list of subfiles
//loop through subelements //loop through subelements
for (int i = -1, remoteFilesSize = remoteFiles.size(); ++i < remoteFilesSize; ){ for (int i = -1, remoteFilesSize = remoteFiles.size(); ++i < remoteFilesSize; ){
RemoteFile remoteFile = (RemoteFile) remoteFiles.get(i); RemoteFile remoteFile = (RemoteFile) remoteFiles.get(i);
//if remoteFile is in a "media" folder and its name start with "." //if remoteFile is in a "media" folder and its name start with "."
// then ignore it // then ignore it
if(syncedFolder.isMediaType() if(syncedFolder.isMediaType()
&& CommonUtils.getFileNameFromPath( remoteFile.getRemotePath() ).startsWith(".") ){ && CommonUtils.getFileNameFromPath( remoteFile.getRemotePath() ).startsWith(".") ){
continue; 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
RemoteFile directory = (RemoteFile) result.getData().get(0);
if(!directory.getEtag().equals(syncedFolder.getLastEtag())) {
syncedFolder.setLastEtag(directory.getEtag()).setToSync(true);
atLeastOneDirAsChanged = true;
} }
if( remoteFile.getMimeType().equals("DIR") ) {
String suffixPath = remoteFile.getRemotePath().substring( syncedFolder.getRemoteFolder().length() );
}//Last else correspond to error 404 at LightReadFolderRemoteOperation (see below) //but is it already known as SyncedFolder?
}else{ //Result isn't a success SyncedFolder subSyncedFolder = new SyncedFolder(syncedFolder, suffixPath, 0L, "" ); //need to set empty etag to allow it to be scan
if(result.getHttpCode() == 404){ //File not found mSyncedFolderIterator.add(subSyncedFolder);
atLeastOneDirAsChanged = true; mSyncedFolderIterator.previous();
syncedFolder.setToSync(true);
//If there is no remote file, then try to delete local one if empty. Finally remove Synced Folder from DB. }else {
File localFolder = new File(syncedFolder.getLocalFolder()); //If it's a file just add it to mRemoteFiles.
if(localFolder.exists() && localFolder.listFiles().length == 0){ mRemoteFiles.add(remoteFile);
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