Exemplo n.º 1
0
        // Synchronises with Azure Blob Storage
        public void SyncAzureStorage()
        {
            var common = new Common();

            var localFiles = common.GetLocalFiles(m_syncEncryptedFilesOnly);

            var blobItems = m_azureStorageHelper.GetBlobItems();

            var syncHelper = new SyncHelper(m_remoteItemClass, m_isDebug);

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

                if (delete.Count > 0)
                {
                    syncHelper.DeleteFromAzureStorage(delete.Select(d => d), m_syncEncryptedFilesOnly);
                    // Get new blobItems list
                    blobItems = m_azureStorageHelper.GetBlobItems();
                }
            }

            var remoteFetchHelper = new RemoteFetchHelper(m_remoteItemClass);

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

            var filesToDeleteOnRemoteStorage = new List <RemoteItem>(blobItems.Count);
            var filesToCopyToRemoteStorage   = new List <FileInfo>(localFiles.Count);
            var filesToCopyFromRemoteStorage = new List <RemoteItem>(blobItems.Count);

            // Exists on RemoteStorage, does not exist on local - fetch from Remote Storage
            foreach (var azureFile in blobItems)
            {
                var localMatch = m_isRunningOnWindows ? azureFile.Name.Replace(@"\", @"/") : azureFile.Name;

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

                if (localFile.Key != null && localFile.Value != null && localFile.Value.Contains(localMatch))
                {
                    if (localFile.Key.Length != azureFile.Size)
                    {
                        filesToCopyFromRemoteStorage.Add(azureFile);
                    }
                }
                else
                {
                    filesToCopyFromRemoteStorage.Add(azureFile);
                }
            }

            // Exists on RemoteStorage, does not exist on local - delete from RemoteStorage
            filesToDeleteOnRemoteStorage.AddRange(from azureFile in blobItems
                                                  where !localFiles.ContainsValue(m_isRunningOnWindows ? azureFile.Name.Replace(@"\", @"/") : azureFile.Name)
                                                  select azureFile);

            // Exists on local, does not exist on RemoteStorage - copy to RemoteStorage
            filesToCopyToRemoteStorage.AddRange(from localFile in localFiles
                                                where !blobItems.Exists(x => (m_isRunningOnWindows ? x.Name.Replace(@"/", @"\") : x.Name) == localFile.Value)
                                                select localFile.Key);

            if (m_isDebug)
            {
                return;
            }

            if (m_copyFilesToRemoteStorage)
            {
                syncHelper.CopyToAzureStorage(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(blobItem => blobItem.Name.Contains(m_explicitFilesToFetchMatchingString))
                                                : filesToCopyFromRemoteStorage, m_itemSortOrder);
            }

            if (m_deleteMissingFilesFromRemoteStorage)
            {
                syncHelper.DeleteFromAzureStorage(filesToDeleteOnRemoteStorage, m_syncEncryptedFilesOnly);
            }
            else
            {
                Console.WriteLine("No files will be deleted from Remote Storage.");
            }
        }
Exemplo n.º 2
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.");
            }
        }