Пример #1
0
        public string CreateEndPoint(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, bool createToFile)
        {
            var activityIOPath = dst.IOPath;
            var dirParts       = MakeDirectoryParts(activityIOPath, dst.PathSeperator());

            var ok = CreateDirectoriesForPath(dst, args, dirParts);

            if (!ok)
            {
                return(ResultBad);
            }



            var shouldCreateFile = dst.PathIs(dst.IOPath) == enPathType.File && createToFile;

            if (shouldCreateFile)
            {
                if (CreateFile(dst, args))
                {
                    return(ResultOk);
                }
            }
            else
            {
                return(ResultOk);
            }


            return(ResultBad);
        }
Пример #2
0
        private bool CreateDirectoriesForPath(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, IList <string> dirParts)
        {
            var maxDepth = dirParts.Count - 1;
            var pos      = 0;
            var origPath = dst.IOPath;

            try
            {
                while (pos <= maxDepth)
                {
                    var toCreate = ActivityIOFactory.CreatePathFromString(dirParts[pos], dst.IOPath.Username,
                                                                          dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile);

                    if (dst.PathExist(toCreate))
                    {
                        pos++;
                        continue;
                    }
                    dst.IOPath = toCreate;
                    if (!CreateDirectory(dst, args))
                    {
                        return(false);
                    }
                    pos++;
                }
            }
            finally
            {
                dst.IOPath = origPath;
            }
            return(true);
        }
Пример #3
0
        public bool CreateDirectory(IActivityIOPath dst, IDev2CRUDOperationTO args)
        {
            bool result = false;

            bool ok;

            if (args.Overwrite)
            {
                if (IsDirectoryAlreadyPresent(dst))
                {
                    Delete(dst);
                }
                ok = true;
            }
            else
            {
                ok = !IsDirectoryAlreadyPresent(dst);
            }

            if (ok)
            {
                result = IsStandardFtp(dst) ? CreateDirectoryStandardFtp(dst) : CreateDirectorySftp(dst);
            }
            return(result);
        }
Пример #4
0
 public string Move(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args)
 {
     Source              = src;
     Destination         = dst;
     Dev2CrudOperationTO = args;
     return("Successful");
 }
Пример #5
0
 public string Create(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, bool createToFile)
 {
     CreateToFile        = createToFile;
     Destination         = dst;
     Dev2CrudOperationTO = args;
     return("Successful");
 }
Пример #6
0
 public DoPutAction(Stream currentStream, IActivityIOPath destination, IDev2CRUDOperationTO crudArgument,
                    string whereToPut)
     : this(currentStream, destination, crudArgument, whereToPut, new LogonProvider(), new FileWrapper(),
            new FileStreamFactory(), new FilePathWrapper(), new MemoryStreamFactory(),
            ValidateAuthorization.RequiresAuth)
 {
 }
Пример #7
0
 public DoCreateDirectory(IActivityIOPath path, IDev2CRUDOperationTO args)
 {
     _logOnProvider   = new LogonProvider();
     _fileWrapper     = new FileWrapper();
     _dirWrapper      = new DirectoryWrapper();
     _path            = path;
     _crudArguments   = args;
     ImpersonatedUser = ValidateAuthorization.RequiresAuth(_path, _logOnProvider);
     _handleOverwrite = RequiresOverwrite(_crudArguments, _path, _logOnProvider);
 }
Пример #8
0
 public DoPutAction(Stream currentStream, IActivityIOPath destination, IDev2CRUDOperationTO crudArgument, string whereToPut)
 {
     _logOnProvider   = new LogonProvider();
     _pathWrapper     = new FilePathWrapper();
     _fileWrapper     = new FileWrapper();
     _currentStream   = currentStream;
     Destination      = destination;
     _arguments       = crudArgument;
     ImpersonatedUser = ValidateAuthorization.RequiresAuth(Destination, _logOnProvider);
     _whereToPut      = whereToPut;
 }
Пример #9
0
 public DoCreateDirectory(IActivityIOPath path, IDev2CRUDOperationTO args, IDev2LogonProvider dev2LogonProvider, IFile fileWrapper, IDirectory directory, ImpersonationDelegate impersonationDelegate)
     : base(impersonationDelegate)
 {
     _logOnProvider    = dev2LogonProvider;
     _fileWrapper      = fileWrapper;
     _dirWrapper       = directory;
     _path             = path;
     _crudArguments    = args;
     _impersonatedUser = _impersonationDelegate(_path, _logOnProvider);
     _handleOverwrite  = RequiresOverwrite(_crudArguments, _path, _logOnProvider);
 }
Пример #10
0
 public string Create(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, bool createToFile)
 {
     try
     {
         _common.ValidateEndPoint(dst, args);
         return(CreateEndPoint(dst, args, createToFile));
     }
     finally
     {
         _filesToDelete.ForEach(RemoveTmpFile);
     }
 }
Пример #11
0
 public DoPutAction(Stream currentStream, IActivityIOPath destination, IDev2CRUDOperationTO crudArgument, string whereToPut, IDev2LogonProvider logOnProvider, IFile fileWrapper, IFilePath pathWrapper, ImpersonationDelegate impersonationDelegate)
     : base(impersonationDelegate)
 {
     _logOnProvider    = logOnProvider;
     _pathWrapper      = pathWrapper;
     _fileWrapper      = fileWrapper;
     _currentStream    = currentStream;
     _destination      = destination;
     _arguments        = crudArgument;
     _impersonatedUser = _impersonationDelegate(_destination, _logOnProvider);
     _whereToPut       = whereToPut;
 }
Пример #12
0
 public string Create(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, bool createToFile)
 {
     try
     {
         _common.ValidateEndPoint(dst, args);
         return(_implementation.CreateEndPoint(dst, args, createToFile));
     }
     finally
     {
         RemoveAllTmpFiles();
     }
 }
Пример #13
0
        public void ValidateEndPoint(IActivityIOOperationsEndPoint endPoint, IDev2CRUDOperationTO args)
        {
            if (endPoint.IOPath?.Path.Trim().Length == 0)
            {
                throw new Exception(ErrorResource.SourceCannotBeAnEmptyString);
            }

            if (endPoint.PathExist(endPoint.IOPath) && !args.Overwrite)
            {
                throw new Exception(ErrorResource.DestinationDirectoryExist);
            }
        }
Пример #14
0
        bool CreateFile(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args)
        {
            var result = true;

            var tmp = CreateTmpFile();

            using (Stream s = new MemoryStream(_fileWrapper.ReadAllBytes(tmp)))
            {
                if (dst.Put(s, dst.IOPath, args, null, _filesToDelete) < 0)
                {
                    result = false;
                }

                s.Close();
            }

            return(result);
        }
Пример #15
0
        public int Put(Stream src, IActivityIOPath dst, IDev2CRUDOperationTO args, string whereToPut, List <string> filesToCleanup)
        {
            var result = -1;

            bool ok;

            if (args.Overwrite)
            {
                ok = true;
            }
            else
            {
                try
                {
                    using (Get(dst, filesToCleanup))
                    {
                        ok = false;
                    }
                }
                catch (Exception ex)
                {
                    Dev2Logger.Error(this, ex, GlobalConstants.WarewolfError);
                    ok = true;
                }
            }

            if (ok)
            {
                using (src)
                {
                    try
                    {
                        result = _implementation.IsStandardFtp(dst) ? _implementation.WriteToFtp(src, dst) : _implementation.WriteToSftp(src, dst);
                    }
                    catch (Exception ex)
                    {
                        Dev2Logger.Error(@"Exception in Put command", ex, GlobalConstants.WarewolfError);
                        throw;
                    }
                }
            }
            return(result);
        }
Пример #16
0
        string ValidateRenameSourceAndDesinationTypes(IActivityIOOperationsEndPoint src,
                                                      IActivityIOOperationsEndPoint dst,
                                                      IDev2CRUDOperationTO args)
        {
            if (src.PathIs(src.IOPath) != dst.PathIs(dst.IOPath))
            {
                throw new Exception(ErrorResource.SourceAndDestinationNOTFilesOrDirectory);
            }
            if (dst.PathExist(dst.IOPath))
            {
                if (!args.Overwrite)
                {
                    throw new Exception(ErrorResource.DestinationDirectoryExist);
                }
                dst.Delete(dst.IOPath);
            }

            return(Move(src, dst, args));
        }
Пример #17
0
        public int Put(Stream src, IActivityIOPath dst, IDev2CRUDOperationTO args, string whereToPut, List <string> filesToCleanup)
        {
            var result = -1;

            bool ok;

            if (args.Overwrite)
            {
                ok = true;
            }
            else
            {
                // try and fetch the file, if not found ok because we not in Overwrite mode
                try
                {
                    using (Get(dst, filesToCleanup))
                    {
                        ok = false;
                    }
                }
                catch (Exception ex)
                {
                    Dev2Logger.Error(this, ex);
                    ok = true;
                }
            }

            if (ok)
            {
                try
                {
                    result = IsStandardFtp(dst) ? WriteToFtp(src, dst) : WriteToSftp(src, dst);
                }
                catch (Exception ex)
                {
                    Dev2Logger.Error(@"Exception in Put command", ex);
                    throw;
                }
            }
            return(result);
        }
Пример #18
0
        bool CreateFile(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args)
        {
            // TODO: why create a tmp file here, surely we can just use an empty array as input to the memory stream?
            var tmp = CreateTmpFile();

            using (Stream s = new MemoryStream(_fileWrapper.ReadAllBytes(tmp)))
            {
                try
                {
                    if (dst.Put(s, dst.IOPath, args, null, _filesToDelete) >= 0)
                    {
                        return(true);
                    }
                }
                finally {
                    s.Close();
                }
            }

            return(false);
        }
Пример #19
0
        string ValidateCopySourceDestinationFileOperation(IActivityIOOperationsEndPoint src,
                                                          IActivityIOOperationsEndPoint dst,
                                                          IDev2CRUDOperationTO args,
                                                          Func <string> performAfterValidation)
        {
            var result = ResultOk;

            _common.ValidateSourceAndDestinationPaths(src, dst);
            var opStatus = CreateEndPoint(dst, args, dst.PathIs(dst.IOPath) == enPathType.Directory);

            if (!opStatus.Equals("Success"))
            {
                throw new Exception(string.Format(ErrorResource.RecursiveDirectoryCreateFailed, dst.IOPath.Path));
            }
            if (src.PathIs(src.IOPath) == enPathType.Directory)
            {
                if (!TransferDirectoryContents(src, dst, args))
                {
                    result = ResultBad;
                }
            }
            else
            {
                if (!args.Overwrite)
                {
                    EnsureFilesDontExists(src, dst);
                }

                if (!Dev2ActivityIOPathUtils.IsStarWildCard(src.IOPath.Path))
                {
                    return(performAfterValidation?.Invoke());
                }
                if (!TransferDirectoryContents(src, dst, args))
                {
                    result = ResultBad;
                }
            }

            return(result);
        }
Пример #20
0
        public bool CreateDirectory(IActivityIOPath dst, IDev2CRUDOperationTO args)
        {
            var  result = false;
            bool okayToCreate;

            if (args.Overwrite)
            {
                if (_implementation.IsDirectoryAlreadyPresent(dst))
                {
                    Delete(dst);
                }
                okayToCreate = true;
            }
            else
            {
                okayToCreate = !_implementation.IsDirectoryAlreadyPresent(dst);
            }
            if (okayToCreate)
            {
                result = _implementation.IsStandardFtp(dst) ? _implementation.CreateDirectoryStandardFtp(dst) : _implementation.CreateDirectorySftp(dst);
            }
            return(result);
        }
Пример #21
0
 public DoCreateDirectory(IActivityIOPath path, IDev2CRUDOperationTO args)
     : this(path, args, new LogonProvider(), new FileWrapper(), new DirectoryWrapper(), ValidateAuthorization.RequiresAuth)
 {
 }
Пример #22
0
        public bool CreateDirectory(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args)
        {
            var result = dst.CreateDirectory(dst.IOPath, args);

            return(result);
        }
Пример #23
0
 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);
     }
 }
Пример #24
0
        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));
                    }
                }
            }
        }
Пример #25
0
        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);
            }
        }
Пример #26
0
        public string Rename(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args)
        {
            string performRename()
            {
                if (src.PathIs(src.IOPath) != dst.PathIs(dst.IOPath))
                {
                    throw new Exception(ErrorResource.SourceAndDestinationNOTFilesOrDirectory);
                }
                if (dst.PathExist(dst.IOPath))
                {
                    if (!args.Overwrite)
                    {
                        throw new Exception(ErrorResource.DestinationDirectoryExist);
                    }
                    dst.Delete(dst.IOPath);
                }

                return(Move(src, dst, args));
            }

            try
            {
                return(performRename());
            }
            finally
            {
                RemoveAllTmpFiles();
            }
        }
Пример #27
0
        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);
        }
 public int Put(Stream src, IActivityIOPath dst, IDev2CRUDOperationTO args, string whereToPut, List <string> filesToCleanup)
 => new DoPutAction(src, dst, args, whereToPut).ExecuteOperation();
Пример #29
0
        public string Move(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args)
        {
            string result;

            try
            {
                result = Copy(src, dst, args);
                if (result.Equals(ActivityIOBrokerBaseDriver.ResultOk))
                {
                    src.Delete(src.IOPath);
                }
            }
            finally
            {
                RemoveAllTmpFiles();
            }

            return(result);
        }
 public bool CreateDirectory(IActivityIOPath dst, IDev2CRUDOperationTO args)
 => new DoCreateDirectory(dst, args).ExecuteOperation();