Beispiel #1
0
            private bool CrawlSyncQuery(IFolder remoteFolder, string remoteFolderPath, string localFolder)
            {
                bool success = true;

                try
                {
                    //IList<string> remoteFolders = new List<string>();

                    //IList<string> localFolders = database.getfoldersId();

                    IList <string> qFolders = queryAddedAndModifiedFolders(remoteFolder);


                    // Folders
                    foreach (string fId in qFolders)
                    {
                        try
                        {
                            IFolder folder = session.GetObject(fId) as IFolder;

                            SyncItem newFolderItem = SyncItemFactory.CreateFromRemoteFolder(folder.Path, repoInfo, database);

                            // Check if the folder already exists
                            IList <SyncItem> localfolders = database.GetAllFoldersWithCmisId(fId);
                            if (localfolders != null)
                            {
                                foreach (SyncItem oldFolderItem in localfolders)
                                {
                                    // if directory did not exists -> Move
                                    if (!Directory.Exists(newFolderItem.LocalPath))
                                    {
                                        //Directory.CreateDirectory(newFolderItem.LocalPath);
                                        //Directory.Delete(newFolderItem.LocalPath);
                                        Directory.Move(oldFolderItem.LocalPath, newFolderItem.LocalPath);
                                        database.MoveFolder(oldFolderItem, newFolderItem);
                                        //var test = database.getMetadataInfo();
                                    }

                                    /*
                                     * else
                                     * {
                                     *  // Directory already exists, move content of the old folder and update database
                                     *
                                     *
                                     *  // Files
                                     *  foreach (string filePath in Directory.GetFiles(oldFolderItem.LocalPath))
                                     *  {
                                     *      string fileName = Path.GetFileName(filePath);
                                     *      string newFilePath = Path.Combine(newFolderItem.LocalPath, fileName);
                                     *
                                     *      File.Move(filePath, newFilePath);
                                     *      SyncItem oldFileItem = database.GetSyncItemFromLocalPath(filePath);
                                     *      if (oldFileItem == null)
                                     *          oldFileItem = SyncItemFactory.CreateFromLocalPath(filePath, false, repoInfo, database);
                                     *
                                     *      SyncItem newFileItem = SyncItemFactory.CreateFromLocalPath(newFilePath, false, repoInfo, database);
                                     *
                                     *      if (Utils.IsMetadataFile(newFilePath))
                                     *      {
                                     *          database.MoveMetadataFile(oldFileItem, newFileItem, newFileItem.LocalPath + ".metadata");
                                     *      }
                                     *      else
                                     *      {
                                     *          database.MoveFile(oldFileItem, newFileItem);
                                     *      }
                                     *  }
                                     */
                                }
                            }
                            Directory.CreateDirectory(newFolderItem.LocalPath);
                        }
                        catch (CmisBaseException e)
                        {
                            ProcessRecoverableException("Could not access remote folder : ", e);
                            success = false;
                        }
                    }

                    IList <string> qDocs = queryAddedAndModifiedFiles(remoteFolder);

                    IList <string> remoteFiles = new List <string>();
                    // Files
                    foreach (string docId in qDocs)
                    {
                        try
                        {
                            IDocument doc = session.GetObject(docId) as IDocument;

                            SyncItem syncItem = SyncItemFactory.CreateFromRemoteDocument(doc.Paths[0], doc, repoInfo, database);

                            // Check if the file already exists
                            string id       = docId.Remove(docId.IndexOf(';'));
                            string filePath = database.GetFilePath(id);
                            if (filePath != null)
                            {
                                // File already exists, delete
                                File.Delete(filePath);
                                SyncItem si = database.GetSyncItemFromLocalPath(filePath);
                                database.RemoveFile(si);

                                // Delete Metadata
                                if (database.ContainsLocalMetadata(filePath + ".metadata"))
                                {
                                    File.Delete(filePath + ".metadata");
                                    database.RemoveMetadataFile(si);
                                }
                            }
                            CrawlRemoteDocument(doc, syncItem.RemotePath, syncItem.LocalPath, remoteFiles);
                        }
                        catch (CmisBaseException e)
                        {
                            ProcessRecoverableException("Could not access remote document : ", e);
                            success = false;
                        }
                    }
                    database.setLastSyncDate(DateTimeOffset.Now.ToString("O"));
                }
                catch (CmisBaseException e)
                {
                    ProcessRecoverableException("Could not access remote objects: ", e);
                    success = false;
                }
                catch (Exception e)
                {
                    Logger.Error("Error in CrawlSyncQuery", e);
                    success = false;
                }
                return(success);
            }
Beispiel #2
0
            /// <summary>
            /// Crawl remote subfolder, syncing down if needed.
            /// Meanwhile, cache all contained remote folders, they are output parameters that are used in CrawlLocalFiles/CrawlLocalFolders
            /// </summary>
            private void CrawlRemoteFolder(IFolder remoteSubFolder, string remotePath, string localFolder, IList <string> remoteFolders)
            {
                SleepWhileSuspended();

                try
                {
                    if (Utils.WorthSyncing(localFolder, remoteSubFolder.Name, repoInfo))
                    {
                        Logger.Info("CrawlRemote localFolder:\"" + localFolder + "\" remoteSubFolder.Path:\"" + remoteSubFolder.Path + "\" remoteSubFolder.Name:\"" + remoteSubFolder.Name + "\"");
                        remoteFolders.Add(remoteSubFolder.Name);
                        var subFolderItem = database.GetFolderSyncItemFromRemotePath(remoteSubFolder.Path);
                        if (null == subFolderItem)
                        {
                            subFolderItem = SyncItemFactory.CreateFromRemoteFolder(remoteSubFolder.Path, repoInfo, database);
                        }

                        // Check whether local folder exists.
                        if (Directory.Exists(subFolderItem.LocalPath))
                        {
                            try
                            {
                                activityListener.ActivityStarted();
                                // Recurse into folder.
                                CrawlSync(remoteSubFolder, remotePath, subFolderItem.LocalPath);
                            }
                            finally
                            {
                                activityListener.ActivityStopped();
                            }
                        }
                        else
                        {
                            // 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 subfolder.
                            if (!Directory.Exists(repoInfo.TargetDirectory))
                            {
                                throw new Exception("Local folder has disappeared: " + repoInfo.TargetDirectory + " , aborting synchronization");
                            }

                            // If there was previously a file with this name, delete it.
                            // TODO warn if local changes in the file.
                            if (File.Exists(subFolderItem.LocalPath))
                            {
                                try
                                {
                                    activityListener.ActivityStarted();
                                    Utils.DeleteEvenIfReadOnly(subFolderItem.LocalPath);
                                }
                                finally
                                {
                                    activityListener.ActivityStopped();
                                }
                            }

                            if (database.ContainsFolder(subFolderItem))
                            {
                                try
                                {
                                    activityListener.ActivityStarted();

                                    // If there was previously a folder with this name, it means that
                                    // the user has deleted it voluntarily, so delete it from server too.

                                    DeleteRemoteFolder(remoteSubFolder, subFolderItem, remotePath);
                                }
                                finally
                                {
                                    activityListener.ActivityStopped();
                                }
                            }
                            else
                            {
                                try
                                {
                                    // Maybe the folder has been renamed
                                    // Check the id
                                    List <SyncItem> oldSyncItems = database.GetAllFoldersWithCmisId(remoteSubFolder.Id);
                                    if (oldSyncItems != null)
                                    {
                                        foreach (SyncItem item in oldSyncItems)
                                        {
                                            RemoveFolderLocally(item.LocalPath);
                                        }
                                    }

                                    // The folder has been recently created on server, so download it.
                                    activityListener.ActivityStarted();
                                    Directory.CreateDirectory(subFolderItem.LocalPath);

                                    // Create database entry for this folder.
                                    // TODO - Yannick - Add metadata
                                    database.AddFolder(subFolderItem, remoteSubFolder.Id, remoteSubFolder.LastModificationDate);
                                    Logger.Info("Added folder to database: " + subFolderItem.LocalPath);

                                    // Recursive copy of the whole folder.
                                    RecursiveFolderCopy(remoteSubFolder, remotePath, subFolderItem.LocalPath);
                                }
                                finally
                                {
                                    activityListener.ActivityStopped();
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    activityListener.ActivityStopped();
                    ProcessRecoverableException("Could not crawl sync remote folder: " + remoteSubFolder.Path, e);
                }
            }
Beispiel #3
0
            /// <summary>
            /// Crawl remote subfolder, syncing down if needed.
            /// Meanwhile, cache all contained remote folders, they are output parameters that are used in CrawlLocalFiles/CrawlLocalFolders
            /// </summary>
            private void CrawlRemoteFolder(IFolder remoteSubFolder, string remotePath, string localFolder, IList <string> remoteFolders)
            {
                SleepWhileSuspended();

                try
                {
                    if (Utils.WorthSyncing(localFolder, remoteSubFolder.Name, repoInfo))
                    {
                        // Logger.Debug("CrawlRemote localFolder:\"" + localFolder + "\" remoteSubFolder.Path:\"" + remoteSubFolder.Path + "\" remoteSubFolder.Name:\"" + remoteSubFolder.Name + "\"");
                        remoteFolders.Add(remoteSubFolder.Name);
                        var subFolderItem = database.GetFolderSyncItemFromRemotePath(remoteSubFolder.Path);
                        if (null == subFolderItem)
                        {
                            subFolderItem = SyncItemFactory.CreateFromRemoteFolder(remoteSubFolder.Path, repoInfo, database);
                        }

                        // Check whether local folder exists.
                        if (Directory.Exists(subFolderItem.LocalPath))
                        {
                            // Recurse into folder.
                            CrawlSync(remoteSubFolder, remotePath, subFolderItem.LocalPath);
                        }
                        else
                        {
                            // 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 subfolder.
                            if (!Directory.Exists(repoInfo.TargetDirectory))
                            {
                                throw new Exception("Local folder has disappeared: " + repoInfo.TargetDirectory + " , aborting synchronization");
                            }

                            // If there was previously a file with this name, delete it.
                            // TODO warn if local changes in the file.
                            if (File.Exists(subFolderItem.LocalPath))
                            {
                                activityListener.ActivityStarted();
                                Utils.DeleteEvenIfReadOnly(subFolderItem.LocalPath);
                                activityListener.ActivityStopped();
                            }

                            if (database.ContainsFolder(subFolderItem))
                            {
                                // If there was previously a folder with this name, it means that
                                // the user has deleted it voluntarily, so delete it from server too.

                                activityListener.ActivityStarted();

                                // Delete the folder from the remote server.
                                try
                                {
                                    Logger.Debug("Removing remote folder tree: " + remoteSubFolder.Path);
                                    IList <string> failedIDs = remoteSubFolder.DeleteTree(true, null, true);
                                    if (failedIDs == null || failedIDs.Count != 0)
                                    {
                                        Logger.Error("Failed to completely delete remote folder " + remoteSubFolder.Path);
                                        // TODO Should we retry? Maybe at least once, as a manual recursion instead of a DeleteTree.
                                    }
                                }
                                catch (CmisPermissionDeniedException e)
                                {
                                    // We don't have the permission to delete this folder. Warn and recreate it.
                                    Utils.NotifyUser("You don't have the necessary permissions to delete folder " + remoteSubFolder.Path
                                                     + "\nIf you feel you should be able to delete it, please contact your server administrator");
                                    RecursiveFolderCopy(remoteSubFolder, remotePath, subFolderItem.LocalPath);
                                }

                                // Delete the folder from database.
                                database.RemoveFolder(subFolderItem);

                                activityListener.ActivityStopped();
                            }
                            else
                            {
                                // The folder has been recently created on server, so download it.
                                activityListener.ActivityStarted();
                                Directory.CreateDirectory(subFolderItem.LocalPath);

                                // Create database entry for this folder.
                                // TODO - Yannick - Add metadata
                                database.AddFolder(subFolderItem, remoteSubFolder.Id, remoteSubFolder.LastModificationDate);
                                Logger.Info("Added folder to database: " + subFolderItem.LocalPath);

                                // Recursive copy of the whole folder.
                                RecursiveFolderCopy(remoteSubFolder, remotePath, subFolderItem.LocalPath);

                                activityListener.ActivityStopped();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    activityListener.ActivityStopped();
                    ProcessRecoverableException("Could not crawl sync remote folder: " + remoteSubFolder.Path, e);
                }
            }