public void GetsFiles() { _engine = new AutomationEngineInstance(null); _getFiles = new GetFilesCommand(); string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName; string inputPath = Path.Combine(projectDirectory, @"Resources"); VariableMethods.CreateTestVariable(inputPath, _engine, "inputPath", typeof(string)); VariableMethods.CreateTestVariable(null, _engine, "output", typeof(List <>)); _getFiles.v_SourceFolderPath = "{inputPath}"; _getFiles.v_OutputUserVariableName = "{output}"; _getFiles.RunCommand(_engine); List <string> fileList = (List <string>) "{output}".ConvertUserVariableToObject(_engine, typeof(List <>)); List <string> filenames = new List <string>(); foreach (string file in fileList) { output.WriteLine(file); string[] splitPath = file.Split('\\'); string filename = splitPath[splitPath.Length - 1]; filenames.Add(filename); } Assert.Contains("compressed.zip", filenames); Assert.Contains("toCompress.txt", filenames); }
public IEnumerable <FileDto> GetFiles(GetFilesCommand command) { if (command.ReferenceId == Guid.Empty) { return(null); } var files = FileRepository.GetFilesByReferenceId(command.ReferenceId); if (!string.IsNullOrWhiteSpace(command.ContentType)) { return(files.Where(f => f.ContentType.Contains(command.ContentType)).Select(FileAdapter.ToFileDto).AsEnumerable()); } var fileDtoList = new List <FileDto>(); Parallel.ForEach(files, file => { var url = AzureContainer.GetFileUrl($"{file.Id}_{file.Name}"); var fileDto = new FileDto() { Id = file.Id, Size = file.Size, ContentType = file.ContentType, Name = file.Name, Content = url }; fileDtoList.Add(fileDto); }); return(fileDtoList.AsEnumerable()); }
public void HandlesInvalidFilepath() { _engine = new AutomationEngineInstance(null); _getFiles = new GetFilesCommand(); string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName; string inputPath = Path.Combine(projectDirectory, @"Resources\toDelete.txt"); VariableMethods.CreateTestVariable(inputPath, _engine, "inputPath", typeof(string)); VariableMethods.CreateTestVariable(null, _engine, "output", typeof(List <>)); _getFiles.v_SourceFolderPath = "{inputPath}"; _getFiles.v_OutputUserVariableName = "{output}"; Assert.Throws <DirectoryNotFoundException>(() => _getFiles.RunCommand(_engine)); }
private async Task <StepResult> DoCopyDirectoryAsync(CopyFileStep step, IList <FileMetadata> sourceFiles, IList <FileMetadata> targetFiles) { // Retrieve source files var files = new List <PackedFile>(); bool SourceIdenticalToTarget(FileMetadata src) { foreach (var dst in targetFiles) { if (dst.RelativePath == src.RelativePath) { return(dst.Size == src.Size && dst.LastWriteTimeUtc == src.LastWriteTimeUtc); } } return(false); } var filesToGet = new List <string>(); foreach (var src in sourceFiles) { if (!src.IsDirectory && !(step.SkipIfNotModified && SourceIdenticalToTarget(src))) { filesToGet.Add(src.RelativePath); } } if (filesToGet.Count > 0) { if (step.Direction == FileCopyDirection.RemoteToLocal) { var command = new GetFilesCommand { RootPath = step.SourcePath, Paths = filesToGet.ToArray(), UseCompression = step.UseCompression }; var response = await _channel.SendWithReplyAsync <GetFilesResponse>(command, _controller.CancellationToken); if (response.Status != GetFilesStatus.Successful) { return(new StepResult(false, $"Unable to copy files from the remote machine", "The following files were requested:\r\n" + string.Join("; ", filesToGet) + "\r\n")); } files.AddRange(response.Files); } else { files.AddRange(PackedFile.PackFiles(step.SourcePath, filesToGet)); } } // Include empty directories foreach (var src in sourceFiles) { // ./ indicates the root directory if (src.IsDirectory && src.RelativePath != "./" && !(step.SkipIfNotModified && SourceIdenticalToTarget(src))) { files.Add(new PackedFile(Array.Empty <byte>(), src.RelativePath, src.LastWriteTimeUtc)); } } // Write source files and directories to the target directory if (files.Count == 0) { return(new StepResult(true, "", "No files were copied. Sizes and modification times are identical on the source and target sides.\r\n")); } if (step.Direction == FileCopyDirection.LocalToRemote) { ICommand command = new PutDirectoryCommand { Files = files.ToArray(), Path = step.TargetPath, PreserveTimestamps = step.PreserveTimestamps }; if (step.UseCompression) { command = new CompressedCommand(command); } var response = await _channel.SendWithReplyAsync <PutDirectoryResponse>(command, _controller.CancellationToken); if (response.Status == PutDirectoryStatus.TargetPathIsFile) { return(new StepResult(false, $"Directory \"{step.SourcePath}\" could not be copied to the remote machine: the target path is a file.", "")); } if (response.Status == PutDirectoryStatus.PermissionDenied) { return(new StepResult(false, $"Access to path \"{step.TargetPath}\" on the remote machine is denied", "")); } if (response.Status == PutDirectoryStatus.OtherIOError) { return(new StepResult(false, $"Directory \"{step.SourcePath}\" could not be copied to the remote machine", "")); } } else { if (File.Exists(step.TargetPath)) { return(new StepResult(false, $"Directory \"{step.TargetPath}\" could not be copied to the local machine: the target path is a file.", "")); } try { PackedFile.UnpackFiles(step.TargetPath, files, step.PreserveTimestamps); } catch (UnauthorizedAccessException) { return(new StepResult(false, $"Access to path \"{step.TargetPath}\" on the local machine is denied", "")); } catch (IOException) { return(new StepResult(false, $"Directory \"{step.TargetPath}\" could not be copied to the local machine", "")); } } return(new StepResult(true, "", "")); }
public GetFilesHandler(GetFilesCommand command) { _command = command; }