private void CrawlRemoteDocument(IDocument remoteDocument, string remotePath, string localFolder, IList<string> remoteFiles)
{
SleepWhileSuspended();
if (Utils.WorthSyncing(localFolder, repoInfo.CmisProfile.localFilename(remoteDocument), repoInfo))
{
// We use the filename of the document's content stream.
// This can be different from the name of the document.
// For instance in FileNet it is not unusual to have a document where
// document.Name is "foo" and document.ContentStreamFileName is "foo.jpg".
string remoteDocumentFileName = repoInfo.CmisProfile.localFilename(remoteDocument);
//Logger.Debug("CrawlRemote doc: " + localFolder + CmisUtils.CMIS_FILE_SEPARATOR + remoteDocumentFileName);
// If this file does not have a filename, ignore it.
// It sometimes happen on IBM P8 CMIS server, not sure why.
if (remoteDocumentFileName == null)
{
Logger.Warn("Skipping download of '" + repoInfo.CmisProfile.localFilename(remoteDocument) + "' with null content stream in " + localFolder);
return;
}
if (remoteFiles != null)
{
remoteFiles.Add(remoteDocumentFileName);
}
var paths = remoteDocument.Paths;
var pathsCount = paths.Count;
var syncItem = database.GetSyncItemFromRemotePath(remotePath);
if (null == syncItem)
{
syncItem = SyncItemFactory.CreateFromRemoteDocument(remotePath, repoInfo.CmisProfile.localFilename(remoteDocument), repoInfo, database);
}
if (syncItem.FileExistsLocal())
{
// Check modification date stored in database and download if remote modification date if different.
DateTime? serverSideModificationDate = ((DateTime)remoteDocument.LastModificationDate).ToUniversalTime();
DateTime? lastDatabaseUpdate = database.GetServerSideModificationDate(syncItem);
if (lastDatabaseUpdate == null)
{
Logger.Info("Downloading file absent from database: " + syncItem.LocalPath);
activityListener.ActivityStarted();
DownloadFile(remoteDocument, remotePath, localFolder);
activityListener.ActivityStopped();
}
else
{
// If the file has been modified since last time we downloaded it, then download again.
if (serverSideModificationDate > lastDatabaseUpdate)
{
activityListener.ActivityStarted();
if (database.LocalFileHasChanged(syncItem.LocalPath))
{
Logger.Info("Conflict with file: " + remoteDocumentFileName + ", backing up locally modified version and downloading server version");
Logger.Info("- serverSideModificationDate: " + serverSideModificationDate);
Logger.Info("- lastDatabaseUpdate: " + lastDatabaseUpdate);
Logger.Info("- Checksum in database: " + database.GetChecksum(syncItem.LocalPath));
Logger.Info("- Checksum of local file: " + Database.Database.Checksum(syncItem.LocalPath));
// Rename locally modified file.
String newFilePath = Utils.CreateConflictFilename(syncItem.LocalPath, repoInfo.User);
File.Move(syncItem.LocalPath, newFilePath);
// Download server version
DownloadFile(remoteDocument, remotePath, localFolder);
Logger.Info("- Checksum of remote file: " + Database.Database.Checksum(syncItem.LocalPath));
repo.OnConflictResolved();
// Notify the user.
string lastModifiedBy = CmisUtils.GetProperty(remoteDocument, "cmis:lastModifiedBy");
string message = String.Format(
// Properties_Resources.ResourceManager.GetString("ModifiedSame", CultureInfo.CurrentCulture),
"User {0} modified file \"{1}\" at the same time as you.",
lastModifiedBy, syncItem.LocalPath)
+ "\n\n"
// + Properties_Resources.ResourceManager.GetString("YourVersion", CultureInfo.CurrentCulture);
+ "Your version has been saved as \"" + newFilePath + "\", please merge your important changes from it and then delete it.";
Logger.Info(message);
Utils.NotifyUser(message);
}
else
{
Logger.Info("Downloading modified file: " + remoteDocumentFileName);
DownloadFile(remoteDocument, remotePath, localFolder);
}
activityListener.ActivityStopped();
}
}
}
else
{
// The remote file does not exist on the local filesystem.
// Maybe the whole synchronized folder has disappeared?
// While rare for normal filesystems, that happens rather often with mounted folders (for instance encrypted folders)
// In such a case, we should abort this synchronization rather than delete the remote file.
if ( ! Directory.Exists(repoInfo.TargetDirectory))
{
throw new Exception("Local target directory has disappeared: " + repoInfo.TargetDirectory + " , aborting synchronization");
}
if (database.ContainsLocalFile(syncItem.LocalRelativePath))
{
// The file used to be present locally (as revealed by the database), but does not exist anymore locally.
// So, it must have been deleted locally by the user.
// Thus, CmisSync must remove the file from the server too.
DeleteRemoteDocument(remoteDocument, syncItem);
}
else
{
// New remote file, download it.
Logger.Info("New remote file: " + syncItem.RemotePath);
activityListener.ActivityStarted();
DownloadFile(remoteDocument, remotePath, localFolder);
activityListener.ActivityStopped();
}
}
}
}