public async Task <Stream> ReadFileContentsAsync(SystemFilepath systemPath)
        {
            var(dirname, basename) = systemPath.Split();
            var directory = directoryFactory.Open(dirname);

            return(await directory.ReadFileAsync(basename));
        }
        public IEnumerable <Either <FileSyncDirectory, FileSyncFile> > GetListing(SystemFilepath systemPath)
        {
            var directory = directoryFactory.Open(systemPath);

            foreach (var directoryInfo in directory.GetSubdirectories())
            {
                yield return(FileSyncDirectory.FromDirectoryInfo(
                                 directoryInfo,
                                 parentDirectory: systemPath,
                                 listingEndpoint: Endpoints.Listing));
            }

            foreach (var fileInfo in directory.GetFiles())
            {
                yield return(FileSyncFile.FromFileInfo(
                                 fileInfo,
                                 parentDirectory: systemPath,
                                 fileHasher,
                                 contentEndpoint: Endpoints.Content));
            }
        }
예제 #3
0
        public async Task RunAsync()
        {
            // Get the files on the client
            var filesOnClient = GetAllFilesOnClient(new SystemFilepath(".")).ToList();

            view.Verbose(new FileListViewComponent("Files on the client:", filesOnClient));

            // Call the service to get the files on it
            var filesOnService = (await GetAllFilesOnService()).ToList();

            view.Verbose(new FileListViewComponent("Files on the service:", filesOnService));

            // Find and resolve conflicts
            var compareFiles = new CompareFiles(filesOnClient, filesOnService, fileHasher);

            view.Out(new ConflictsViewComponent(compareFiles.Conflicts()));

            // Upload files to the service
            var filesToUpload = compareFiles.FilesToUpload().ToList();

            view.Verbose(new FileListViewComponent("Files to upload:", filesToUpload));

            var uploadResults = await Task.WhenAll(filesToUpload.Select(file =>
                                                                        Result.TryAsync(async() =>
            {
                var(dirname, basename) = file.RelativePath.Split();
                var directory          = directoryFactory.Open(dirname.ToSystemFilepath());
                var content            = await directory.ReadFileAsync(basename);

                await fileService.PutFileContentAsync(file.RelativePath, content);
                return(file);
            })
                                                                        .CatchAsync((HttpRequestException e) =>
            {
                view.Error(new LineViewComponent($"Error uploading file {file.RelativePath}. {e.Message}"));
                return(Task.FromResult(file));
            })));

            // Download file content from the service
            var filesToDownload = compareFiles.FilesToDownload().ToList();

            view.Verbose(new FileListViewComponent("Files to download:", filesToDownload));

            var downloadResults = await Task.WhenAll(filesToDownload.Select(file =>
                                                                            Result.TryAsync(async() =>
            {
                var(dirname, basename) = file.RelativePath.Split();
                var directory          = directoryFactory.Open(dirname.ToSystemFilepath());
                var content            = await fileService.GetFileContentAsync(file);

                await directory.WriteFileAsync(basename, content);
                return(file);
            })
                                                                            .CatchAsync((HttpRequestException e) =>
            {
                view.Error(new LineViewComponent($"Error downloading file {file.RelativePath}. {e.Message}"));
                return(Task.FromResult(file));
            })));

            // Print summary
            var compareOnFilepath = new MappedEqualityComparer <FileSyncFile, ForwardSlashFilepath>(x => x.RelativePath);
            var downloadedFiles   = downloadResults
                                    .Successes()
                                    .ToList();

            view.Out(new SummaryViewComponent(
                         sentFiles: uploadResults.Successes(),
                         newFiles: downloadedFiles.Except(filesOnClient, compareOnFilepath),
                         changedFiles: downloadedFiles.Intersect(filesOnClient, compareOnFilepath)));

            if (uploadResults.Failures().Any())
            {
                view.Error(new FileListViewComponent("Failed to upload some files:", uploadResults.Failures()));
            }

            if (downloadResults.Failures().Any())
            {
                view.Error(new FileListViewComponent("Failed to download some files:", downloadResults.Failures()));
            }
        }