Beispiel #1
0
        public async Task <IReadOnlyCollection <string> > SynchronizeFilesAsync()
        {
            var processedFiles = new List <string>();
            var sourceFiles    = _source.GetFiles();

            foreach (var file in sourceFiles)
            {
                string destinationPath   = _source.ToRelativePath(file.FullName);
                var    fileOnDestination = _destination.GetFile(destinationPath);

                if (fileOnDestination is null)
                {
                    // File is missing on destination
                    using var stream = file.OpenRead();

                    await _destination.WriteFileAsync(destinationPath, stream);

                    processedFiles.Add(destinationPath);
                }
                else if (fileOnDestination.ContentMD5 != MD5Hash(file.FullName))
                {
                    // File on destination is different
                    await _destination.DeleteFileAsync(destinationPath);

                    using var stream = file.OpenRead();
                    await _destination.WriteFileAsync(destinationPath, stream);

                    processedFiles.Add(destinationPath);
                }
                else
                {
                    _logger.LogInformation("File up to date: {file}", destinationPath);
                }
            }

            var destinationFiles = _destination.GetFiles();

            foreach (var file in destinationFiles)
            {
                if (!File.Exists(_source.ToFullPath(file.Path)))
                {
                    await _destination.DeleteFileAsync(file.Path);
                }
            }

            return(processedFiles);
        }