Ejemplo n.º 1
0
        public RestoreHelper(ItemClass itemClass)
        {
            m_remoteItemClass = itemClass;

            if (m_remoteItemClass == ItemClass.Blob)
            {
                m_azureHelper = new AzureStorageHelper();
            }
            else
            {
                m_fileHelper = new FileStorageHelper();
            }
        }
Ejemplo n.º 2
0
        public SyncHelper(ItemClass itemClass, bool debug)
        {
            m_remoteItemClass = itemClass;
            m_common          = new Common();

            m_isDebug = debug;

            if (m_remoteItemClass == ItemClass.Blob)
            {
                m_azureStorageHelper = m_azureStorageHelper ?? new AzureStorageHelper();
            }
            else
            {
                m_fileStorageHelper = m_fileStorageHelper ?? new FileStorageHelper();
                m_remoteStorage     = new DirectoryInfo(ConfigHelper.GetConfigurationValue("RemoteStorage"));
                m_remoteUsername    = ConfigHelper.GetFileStorageUsername();
                m_remotePassword    = ConfigHelper.GetFileStoragePassword();
            }

            m_isRunningOnWindows = Path.DirectorySeparatorChar.Equals('\\') ? true : false;

            // Get the application settings
            m_localDirectory = new DirectoryInfo(ConfigHelper.GetConfigurationValue("LocalPath"));
            m_remoteStorage  = new DirectoryInfo(ConfigHelper.GetConfigurationValue("RemoteStorage"));

            m_copyFilesToRemoteStorage             = ConfigHelper.GetConfigurationBoolean("CopyFilesToRemoteStorage");
            m_deleteExplicitFilesFromRemoteStorage = ConfigHelper.GetConfigurationBoolean("DeleteExplicitFilesFromRemoteStorage");
            m_fetchExplicitFilesFromRemoteStorage  = ConfigHelper.GetConfigurationBoolean("FetchExplicitFilesFromRemoteStorage");
            m_deleteMissingFilesFromRemoteStorage  = ConfigHelper.GetConfigurationBoolean("DeleteMissingFilesFromRemoteStorage");
            m_deleteMissingFilesFromLocalStorage   = ConfigHelper.GetConfigurationBoolean("DeleteMissingFilesFromLocalStorage");
            m_fetchFilesFromRemoteStorage          = ConfigHelper.GetConfigurationBoolean("FetchFilesFromRemoteStorage");
            m_explicitFilesToDeleteMatchingString  = ConfigHelper.GetConfigurationValue("ExplicitFilesToDeleteMatchingString");
            m_explicitFilesToFetchMatchingString   = ConfigHelper.GetConfigurationValue("ExplicitFilesToFetchMatchingString");
            m_itemSortOrder = ConfigHelper.GetConfigurationValue("ItemSortOrder")
                              .Equals("Size", StringComparison.InvariantCultureIgnoreCase)
                                ? ItemSortOrder.Size
                                : ItemSortOrder.Name;
            var compressAndEncrypt = ConfigHelper.GetConfigurationBoolean("CompressAndEncrypt");
            var encryptOnly        = ConfigHelper.GetConfigurationBoolean("EncryptOnly");


            if (string.IsNullOrEmpty(m_explicitFilesToDeleteMatchingString))
            {
                // Don't delete any explicit files from Remote Path
                m_deleteExplicitFilesFromRemoteStorage = false;
                m_explicitFilesToDeleteMatchingString  = string.Empty;
            }

            if (string.IsNullOrEmpty(m_explicitFilesToFetchMatchingString))
            {
                // Don't delete any explicit files from Remote Path
                m_fetchExplicitFilesFromRemoteStorage = false;
                m_explicitFilesToFetchMatchingString  = string.Empty;
            }

            // Override the debug value
            m_isDebug = !ConfigHelper.GetConfigurationValue("DebugOverride")
                        .Equals("True", StringComparison.InvariantCultureIgnoreCase);

            // Encrypt and Compress settings -- only one can be true, default to EncryptOnly if set
            if (compressAndEncrypt && encryptOnly)
            {
                compressAndEncrypt = false;
            }

            // Will only synchronise files that have the EncryptedFileExtension file type
            // And this is only set if either of the encryption settings is set
            m_syncEncryptedFilesOnly = compressAndEncrypt || encryptOnly;
            m_encryptedFileExtension = ConfigHelper.GetConfigurationValue("EncryptedFileExtension") ?? ".absz";
        }
Ejemplo n.º 3
0
        // Synchronises with Remote File Storage
        public void SyncFileStorage()
        {
            var fileHelper = new FileStorageHelper();

            var localFiles = GetLocalFiles(m_syncEncryptedFilesOnly);
            // var localFileNames = localFiles.Select(localFile => localFile.FullName.Replace(m_localDirectory.Name, string.Empty)).ToList();

            var remoteFiles = fileHelper.GetFileItems();

            var syncHelper = new SyncHelper(m_remoteItemClass, m_isDebug);

            if (m_deleteExplicitFilesFromRemoteStorage && !string.IsNullOrEmpty(m_explicitFilesToDeleteMatchingString))
            {
                // Now only explicitly delete files from Remote File Storage if this setting is set
                var delete = remoteFiles.Where(x => x.Name.Contains(m_explicitFilesToDeleteMatchingString)).ToList();

                if (delete.Count > 0)
                {
                    syncHelper.DeleteFromFileStorage(delete, m_syncEncryptedFilesOnly);
                    // Get new FileInfos list
                    remoteFiles = fileHelper.GetFileItems();
                }
            }

            var remoteFetchHelper = new RemoteFetchHelper(m_remoteItemClass);

            Console.WriteLine("Starting file comparison ...");

            var filesToDeleteRemotely        = new List <RemoteItem>(remoteFiles.Count);
            var filesToDeleteLocally         = new List <FileInfo>(localFiles.Count);
            var filesToCopyToRemoteStorage   = new List <FileInfo>(localFiles.Count);
            var filesToCopyFromRemoteStorage = new List <RemoteItem>(remoteFiles.Count);

            // Exists on Remote Path, does not exist on local - fetch from Remote Path
            foreach (var remoteFile in remoteFiles)
            {
                var localMatch = remoteFile.Name;

                var localFile = localFiles.FirstOrDefault(x => x.Name == localMatch);

                if (localFile != null && localFile.Name.Contains(localMatch))
                {
                    if (localFile.Length != remoteFile.Size)
                    {
                        filesToCopyFromRemoteStorage.Add(remoteFile);
                    }
                }
                else
                {
                    filesToCopyFromRemoteStorage.Add(remoteFile);
                }
            }

            // Exists on File Path, does not exist on local - delete from File Path
            filesToDeleteRemotely.AddRange(from remoteFile in remoteFiles
                                           where !localFiles.Exists(x => x.Name == remoteFile.Name)
                                           select remoteFile);

            // Exists on local, does not exist on File Path - delete from local
            filesToDeleteLocally.AddRange(from localFile in localFiles
                                          where !remoteFiles.Exists(x => x.Name == localFile.Name)
                                          select localFile);

            // Exists on local, does not exist on File Path - copy to File Path
            filesToCopyToRemoteStorage.AddRange(from localFile in localFiles
                                                where !remoteFiles.Exists(x => x.Name == localFile.Name)
                                                select localFile);

            if (m_copyFilesToRemoteStorage)
            {
                syncHelper.CopyToFileStorage(filesToCopyToRemoteStorage);
            }

            if (m_fetchFilesFromRemoteStorage)
            {
                // If explicit matching string is set for files to fetch,
                // then fetch those only, otherwise fetch everything
                remoteFetchHelper.FetchItemsFromRemoteStorage(
                    m_fetchExplicitFilesFromRemoteStorage
                                                ? filesToCopyFromRemoteStorage.Where(remoteFile => remoteFile.Name.Contains(m_explicitFilesToFetchMatchingString))
                                                : filesToCopyFromRemoteStorage, m_itemSortOrder);
            }

            if (m_deleteMissingFilesFromLocalStorage)
            {
                syncHelper.DeleteFromLocalStorage(filesToDeleteLocally, m_syncEncryptedFilesOnly);
            }
            else
            {
                Console.WriteLine("No files will be deleted from Local File Storage.");
            }

            if (m_deleteMissingFilesFromRemoteStorage)
            {
                syncHelper.DeleteFromFileStorage(filesToDeleteRemotely, m_syncEncryptedFilesOnly);
            }
            else
            {
                Console.WriteLine("No files will be deleted from Remote File Storage.");
            }
        }