static internal void Execute(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst) { var sourceParts = VerifyAndCleanInputs(src, dst); if (IsDestinationSubdirectoryOfSource(dst, sourceParts)) { if (dst.PathIs(dst.IOPath) == enPathType.Directory) { var strings = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lastPart = strings.Last(); dst.IOPath.Path = src.PathIs(src.IOPath) == enPathType.Directory ? Path.Combine(dst.IOPath.Path, lastPart) : dst.IOPath.Path.Replace(lastPart, ""); } } else { if (dst.PathIs(dst.IOPath) == enPathType.Directory && src.PathIs(src.IOPath) == enPathType.Directory) { var strings = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lastPart = strings.Last(); dst.IOPath.Path = dst.Combine(lastPart); } } }
public string Copy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { string status; try { status = ValidateCopySourceDestinationFileOperation(src, dst, args, () => { if (src.RequiresLocalTmpStorage()) { if (dst.PathIs(dst.IOPath) == enPathType.Directory) { dst.IOPath.Path = dst.Combine(GetFileNameFromEndPoint(src)); } using (var s = src.Get(src.IOPath, _filesToDelete)) { dst.Put(s, dst.IOPath, args, Path.IsPathRooted(src.IOPath.Path) ? Path.GetDirectoryName(src.IOPath.Path) : null, _filesToDelete); s.Close(); s.Dispose(); } } else { var sourceFile = new FileInfo(src.IOPath.Path); if (dst.PathIs(dst.IOPath) == enPathType.Directory) { dst.IOPath.Path = dst.Combine(sourceFile.Name); } using (var s = src.Get(src.IOPath, _filesToDelete)) { if (sourceFile.Directory != null) { dst.Put(s, dst.IOPath, args, sourceFile.Directory.ToString(), _filesToDelete); } } } return(ResultOk); }); } finally { _filesToDelete.ForEach(RemoveTmpFile); } return(status); }
public void AddMissingFileDirectoryParts(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst) { if (src.IOPath.Path.Trim().Length == 0) { throw new Exception(ErrorResource.SourceCannotBeAnEmptyString); } var sourceParts = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); if (dst.IOPath.Path.Trim().Length == 0) { dst.IOPath.Path = src.IOPath.Path; } else { if (!Path.IsPathRooted(dst.IOPath.Path) && IsNotFtpTypePath(dst.IOPath) && IsUncFileTypePath(dst.IOPath)) { var lastPart = sourceParts.Last(); dst.IOPath.Path = Path.Combine(src.PathIs(dst.IOPath) == enPathType.Directory ? src.IOPath.Path : src.IOPath.Path.Replace(lastPart, ""), dst.IOPath.Path); } } var destinationParts = dst.IOPath.Path.Split(dst.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); while (destinationParts.Count > sourceParts.Count) { destinationParts.Remove(destinationParts.Last()); } if (destinationParts.OrderBy(i => i).SequenceEqual(sourceParts.OrderBy(i => i))) { if (dst.PathIs(dst.IOPath) == enPathType.Directory) { var strings = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lastPart = strings.Last(); dst.IOPath.Path = src.PathIs(src.IOPath) == enPathType.Directory ? Path.Combine(dst.IOPath.Path, lastPart) : dst.IOPath.Path.Replace(lastPart, ""); } } else { if (dst.PathIs(dst.IOPath) == enPathType.Directory && src.PathIs(src.IOPath) == enPathType.Directory) { var strings = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lastPart = strings.Last(); dst.IOPath.Path = dst.Combine(lastPart); } } }
private string CopyRequiresLocalTmpStorage(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { if (dst.PathIs(dst.IOPath) == enPathType.Directory) { dst.IOPath.Path = dst.Combine(_implementation.GetFileNameFromEndPoint(src)); } using (var s = src.Get(src.IOPath, _filesToDelete)) { var result = dst.Put(s, dst.IOPath, args, Path.IsPathRooted(src.IOPath.Path) ? Path.GetDirectoryName(src.IOPath.Path) : null, _filesToDelete); s.Close(); return(result == -1 ? ActivityIOBrokerBaseDriver.ResultBad : ActivityIOBrokerBaseDriver.ResultOk); } }
string ValidateZipSourceDestinationFileOperation(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args, Func <string> performAfterValidation) { _common.AddMissingFileDirectoryParts(src, dst); if (dst.PathIs(dst.IOPath) == enPathType.Directory) { var sourcePart = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last(); if (src.PathIs(src.IOPath) == enPathType.File) { var fileInfo = new FileInfo(sourcePart); dst.IOPath.Path = dst.Combine(sourcePart.Replace(fileInfo.Extension, ".zip")); } else { dst.IOPath.Path = dst.IOPath.Path + ".zip"; } } else { var sourcePart = dst.IOPath.Path.Split(dst.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last(); var fileInfo = new FileInfo(sourcePart); dst.IOPath.Path = dst.IOPath.Path.Replace(fileInfo.Extension, ".zip"); } if (!args.Overwrite && dst.PathExist(dst.IOPath)) { throw new Exception(ErrorResource.DestinationFileAlreadyExists); } var opStatus = CreateEndPoint(dst, new Dev2CRUDOperationTO(args.Overwrite), dst.PathIs(dst.IOPath) == enPathType.Directory); if (!opStatus.Equals(ResultOk)) { throw new Exception(string.Format(ErrorResource.RecursiveDirectoryCreateFailed, dst.IOPath.Path)); } return(performAfterValidation()); }
void RecursiveCopy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { try { var srcContentsFolders = src.ListFoldersInDirectory(src.IOPath); Task.WaitAll(srcContentsFolders.Select(sourcePath => Task.Run(() => { var sourceEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(sourcePath); IList <string> dirParts = sourceEndPoint.IOPath.Path.Split(sourceEndPoint.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var destinationPath = ActivityIOFactory.CreatePathFromString(dst.Combine(dirParts.Last()), dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var destinationEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(destinationPath); dst.CreateDirectory(destinationPath, args); TransferDirectoryContents(sourceEndPoint, destinationEndPoint, args); })).ToArray()); } catch (AggregateException e) { var message = e.InnerExceptions.Where(exception => !string.IsNullOrEmpty(exception?.Message)).Aggregate(string.Empty, (current, exception) => current + exception.Message + "\r\n"); throw new Exception(message, e); } }
public string Copy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { string status; try { status = _validator.ValidateCopySourceDestinationFileOperation(src, dst, args, () => { if (src.RequiresLocalTmpStorage()) { return(CopyRequiresLocalTmpStorage(src, dst, args)); } else { var sourceFile = _fileWrapper.Info(src.IOPath.Path); if (dst.PathIs(dst.IOPath) == enPathType.Directory) { dst.IOPath.Path = dst.Combine(sourceFile.Name); } using (var s = src.Get(src.IOPath, _filesToDelete)) { if (sourceFile.Directory != null) { var result = dst.Put(s, dst.IOPath, args, sourceFile.Directory.ToString(), _filesToDelete); return(result == -1 ? ActivityIOBrokerBaseDriver.ResultBad : ActivityIOBrokerBaseDriver.ResultOk); } } } return(ActivityIOBrokerBaseDriver.ResultBad); }); } finally { RemoveAllTmpFiles(); } return(status); }
void ValidateSourceAndDestinationContents(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { if (!args.Overwrite) { var srcContentsFolders = src.ListFoldersInDirectory(src.IOPath); foreach (var sourcePath in srcContentsFolders) { var sourceEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(sourcePath); IList <string> dirParts = sourceEndPoint.IOPath.Path.Split(sourceEndPoint.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var directory = dirParts.Last(); var destinationPath = ActivityIOFactory.CreatePathFromString(dst.Combine(directory), dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var destinationEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(destinationPath); if (destinationEndPoint.PathExist(destinationEndPoint.IOPath)) { ValidateSourceAndDestinationContents(sourceEndPoint, destinationEndPoint, args); } } var srcContents = src.ListFilesInDirectory(src.IOPath); var dstContents = dst.ListFilesInDirectory(dst.IOPath); var sourceFileNames = srcContents.Select(srcFile => GetFileNameFromEndPoint(src, srcFile)).ToList(); var destinationFileNames = dstContents.Select(dstFile => GetFileNameFromEndPoint(dst, dstFile)).ToList(); if (destinationFileNames.Count > 0) { var commonFiles = sourceFileNames.Where(destinationFileNames.Contains).ToList(); if (commonFiles.Count > 0) { var fileNames = commonFiles.Aggregate("", (current, commonFile) => current + "\r\n" + commonFile); throw new Exception(string.Format(ErrorResource.FileExistInDestinationFolder, fileNames)); } } } }
/// <summary> /// Transfer the contents of the directory /// </summary> /// <param name="src"></param> /// <param name="dst"></param> /// <param name="args"></param> void ValidateSourceAndDestinationContents(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args) { if(!args.Overwrite) { var srcContentsFolders = src.ListFoldersInDirectory(src.IOPath); foreach(var sourcePath in srcContentsFolders) { var sourceEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(sourcePath); IList<string> dirParts = sourceEndPoint.IOPath.Path.Split(sourceEndPoint.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var directory = dirParts.Last(); var destinationPath = ActivityIOFactory.CreatePathFromString(dst.Combine(directory), dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var destinationEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(destinationPath); if(destinationEndPoint.PathExist(destinationEndPoint.IOPath)) { ValidateSourceAndDestinationContents(sourceEndPoint, destinationEndPoint, args); } } var srcContents = src.ListFilesInDirectory(src.IOPath); var dstContents = dst.ListFilesInDirectory(dst.IOPath); var sourceFileNames = srcContents.Select(srcFile => GetFileNameFromEndPoint(src, srcFile)).ToList(); var destinationFileNames = dstContents.Select(dstFile => GetFileNameFromEndPoint(dst, dstFile)).ToList(); if(destinationFileNames.Count > 0) { var commonFiles = sourceFileNames.Where(destinationFileNames.Contains).ToList(); if(commonFiles.Count > 0) { var fileNames = commonFiles.Aggregate("", (current, commonFile) => current + "\r\n" + commonFile); throw new Exception( "The following file(s) exist in the destination folder and overwrite is set to false:- " + fileNames); } } } }
void RecursiveCopy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args) { try { // List directory contents var srcContentsFolders = src.ListFoldersInDirectory(src.IOPath); Task.WaitAll(srcContentsFolders.Select(sourcePath => Task.Run(() => { var sourceEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(sourcePath); IList<string> dirParts = sourceEndPoint.IOPath.Path.Split(sourceEndPoint.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var destinationPath = ActivityIOFactory.CreatePathFromString(dst.Combine(dirParts.Last()), dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var destinationEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(destinationPath); dst.CreateDirectory(destinationPath, args); TransferDirectoryContents(sourceEndPoint, destinationEndPoint, args); })).ToArray()); } catch(AggregateException e) { var message = e.InnerExceptions.Where(exception => exception != null && !string.IsNullOrEmpty(exception.Message)).Aggregate("", (current, exception) => current + exception.Message + "\r\n"); throw new Exception(message, e); } }
public string Copy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args) { string status; try { status = ValidateCopySourceDestinationFileOperation(src, dst, args, () => { if(src.RequiresLocalTmpStorage()) { if(dst.PathIs(dst.IOPath) == enPathType.Directory) { dst.IOPath.Path = dst.Combine(GetFileNameFromEndPoint(src)); } using(var s = src.Get(src.IOPath, _filesToDelete)) { // for flips sake quite putting short-hand notation in-line it causes bugs!!! ;) dst.Put(s, dst.IOPath, args, Path.IsPathRooted(src.IOPath.Path) ? Path.GetDirectoryName(src.IOPath.Path) : null, _filesToDelete); s.Close(); s.Dispose(); } } else { var sourceFile = new FileInfo(src.IOPath.Path); if(dst.PathIs(dst.IOPath) == enPathType.Directory) { dst.IOPath.Path = dst.Combine(sourceFile.Name); } using(var s = src.Get(src.IOPath, _filesToDelete)) { if(sourceFile.Directory != null) { dst.Put(s, dst.IOPath, args, sourceFile.Directory.ToString(), _filesToDelete); } } } return ResultOk; }); } finally { _filesToDelete.ForEach(RemoveTmpFile); } return status; }
void AddMissingFileDirectoryParts(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst) { if(src.IOPath.Path.Trim().Length == 0) { throw new Exception("Source can not be an empty string"); } var sourceParts = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); if(dst.IOPath.Path.Trim().Length == 0) { dst.IOPath.Path = src.IOPath.Path; } else { if(!Path.IsPathRooted(dst.IOPath.Path) && IsNotFtpTypePath(dst.IOPath) && IsUncFileTypePath(dst.IOPath)) { var lastPart = sourceParts.Last(); dst.IOPath.Path = Path.Combine(src.PathIs(dst.IOPath) == enPathType.Directory ? src.IOPath.Path : src.IOPath.Path.Replace(lastPart, ""), dst.IOPath.Path); } } var destinationParts = dst.IOPath.Path.Split(dst.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); while(destinationParts.Count > sourceParts.Count) { destinationParts.Remove(destinationParts.Last()); } if(destinationParts.OrderBy(i => i).SequenceEqual(sourceParts.OrderBy(i => i))) { if(dst.PathIs(dst.IOPath) == enPathType.Directory) { var strings = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lastPart = strings.Last(); dst.IOPath.Path = src.PathIs(src.IOPath) == enPathType.Directory ? Path.Combine(dst.IOPath.Path, lastPart) : dst.IOPath.Path.Replace(lastPart, ""); } } else { if(dst.PathIs(dst.IOPath) == enPathType.Directory && src.PathIs(src.IOPath) == enPathType.Directory) { var strings = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lastPart = strings.Last(); dst.IOPath.Path = dst.Combine(lastPart); } } }
string ValidateZipSourceDestinationFileOperation(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2ZipOperationTO args, Func<string> performAfterValidation) { AddMissingFileDirectoryParts(src, dst); if(dst.PathIs(dst.IOPath) == enPathType.Directory) { var sourcePart = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last(); if(src.PathIs(src.IOPath) == enPathType.File) { var fileInfo = new FileInfo(sourcePart); dst.IOPath.Path = dst.Combine(sourcePart.Replace(fileInfo.Extension, ".zip")); } else { dst.IOPath.Path = dst.IOPath.Path + ".zip"; } } else { var sourcePart = dst.IOPath.Path.Split(dst.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last(); var fileInfo = new FileInfo(sourcePart); dst.IOPath.Path = dst.IOPath.Path.Replace(fileInfo.Extension, ".zip"); } if(!args.Overwrite && dst.PathExist(dst.IOPath)) { throw new Exception("Destination file already exists and overwrite is set to false"); } //ensures destination folder structure exists var opStatus = CreateEndPoint(dst, new Dev2CRUDOperationTO(args.Overwrite), dst.PathIs(dst.IOPath) == enPathType.Directory); if(!opStatus.Equals("Success")) { throw new Exception("Recursive Directory Create Failed For [ " + dst.IOPath.Path + " ]"); } return performAfterValidation(); }