예제 #1
0
        protected override async Task <Action <NativeActivityContext> > ExecuteAsync(NativeActivityContext context, CancellationToken cancellationToken)
        {
            IFtpSession      ftpSession       = null;
            FtpConfiguration ftpConfiguration = new FtpConfiguration(Host.Get(context));

            ftpConfiguration.Port = Port.Expression == null ? null : (int?)Port.Get(context);
            ftpConfiguration.UseAnonymousLogin         = UseAnonymousLogin;
            ftpConfiguration.ClientCertificatePath     = ClientCertificatePath.Get(context);
            ftpConfiguration.ClientCertificatePassword = ClientCertificatePassword.Get(context);
            ftpConfiguration.AcceptAllCertificates     = AcceptAllCertificates;

            if (ftpConfiguration.UseAnonymousLogin == false)
            {
                ftpConfiguration.Username = Username.Get(context);
                ftpConfiguration.Password = Password.Get(context);

                if (string.IsNullOrWhiteSpace(ftpConfiguration.Username))
                {
                    throw new ArgumentNullException("EmptyUsernameException");
                }
            }

            if (UseSftp)
            {
                ftpSession = new SftpSession(ftpConfiguration);
            }
            else
            {
                ftpSession = new FtpSession(ftpConfiguration, FtpsMode);
            }

            await ftpSession.OpenAsync(cancellationToken);

            return((nativeActivityContext) =>
            {
                if (Body != null)
                {
                    _ftpSession = ftpSession;
                    nativeActivityContext.ScheduleAction(Body, ftpSession, OnCompleted, OnFaulted);
                }
            });
        }
예제 #2
0
        public void TestDeployingAProjectWithSpecifyingRemoteDir()
        {
            IFtpSession ftpSession = MockRepository.GenerateMock <IFtpSession>();

            ftpSessionFactory.Stub(x => x.CreateSession()).Return(ftpSession);

            projectBuilder.Stub(x => x.ListBuiltFiles(null))
            .IgnoreArguments().Return(new[] { @"builds\somewhere\file1.txt", @"builds\somewhere\dir2\file2" });

            ftpSession.Expect(x => x.BeginSession(null))
            .IgnoreArguments()
            .Callback(new Func <FtpConnectionData, bool>(x => x.Host == Server && x.Port == null && x.Credentials.UserName == "user" && x.Credentials.Password == "password"));
            ftpSession.Expect(x => x.UploadFile(@"builds\somewhere\file1.txt", "somewhere/remote/file1.txt"));
            ftpSession.Expect(x => x.UploadFile(@"builds\somewhere\dir2\file2", "somewhere/remote/dir2/file2"));

            cmd.ParseArgs(consoleEnv, new[] { BuildDir, Server, "user", "password", "-remote-dir=somewhere/remote" });
            cmd.Execute(consoleEnv);

            ftpSession.VerifyAllExpectations();
        }
예제 #3
0
        public override int Execute(IConsoleEnvironment env)
        {
            FreudeProject project = new FreudeProject();

            project.BuildDir = buildDirectory;

            using (IFtpSession ftpSession = ftpSessionFactory.CreateSession())
            {
                FtpConnectionData connData = new FtpConnectionData();

                connData.Credentials = new NetworkCredential(userName, password);
                connData.Host        = server;
                connData.Port        = port;

                log.InfoFormat(CultureInfo.InvariantCulture, "Connecting to the FTP server {0}:{1}...", connData.Host, connData.Port);

                ftpSession.BeginSession(connData);

                PathBuilder buildDirPath = new PathBuilder(buildDirectory);
                foreach (string sourceFileName in projectBuilder.ListBuiltFiles(project))
                {
                    PathBuilder sourceFileNameDebasedBuilder = buildDirPath.DebasePath(sourceFileName, false);
                    PathBuilder destinationFileName;
                    if (remoteRootDirectory != null)
                    {
                        destinationFileName = new PathBuilder(remoteRootDirectory).CombineWith(sourceFileNameDebasedBuilder);
                    }
                    else
                    {
                        destinationFileName = sourceFileNameDebasedBuilder;
                    }

                    string destinationFileNameUnixStyle = destinationFileName.ToUnixPath();

                    log.InfoFormat(CultureInfo.InvariantCulture, "Uploading file {0} to {1}...", sourceFileName, destinationFileNameUnixStyle);
                    ftpSession.UploadFile(sourceFileName, destinationFileNameUnixStyle);
                }
            }

            return(0);
        }
예제 #4
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            PropertyDescriptor ftpSessionProperty = context.DataContext.GetProperties()[WithFtpSession.FtpSessionPropertyName];
            IFtpSession        ftpSession         = ftpSessionProperty?.GetValue(context.DataContext) as IFtpSession;

            if (ftpSession == null)
            {
                throw new InvalidOperationException("FTPSessionNotFoundException");
            }

            IEnumerable <FtpObjectInfo> files = await ftpSession.EnumerateObjectsAsync(RemotePath.Get(context), Recursive, cancellationToken);

            foreach (FtpObjectInfo file in files)
            {
                Debug.WriteLine("FtpObjectInfo ---------------- : " + file.FullName);
            }

            return((asyncCodeActivityContext) =>
            {
                Files.Set(asyncCodeActivityContext, files);
            });
        }
예제 #5
0
 public void setup()
 {
     _subject = new FtpSession();
 }
예제 #6
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            PropertyDescriptor ftpSessionProperty = context.DataContext.GetProperties()[WithFtpSession.FtpSessionPropertyName];
            IFtpSession        ftpSession         = ftpSessionProperty?.GetValue(context.DataContext) as IFtpSession;

            if (ftpSession == null)
            {
                throw new InvalidOperationException("FTPSessionNotFoundException");
            }

            string remotePath = RemotePath.Get(context);
            string localPath  = LocalPath.Get(context);

            FtpObjectType objectType = await ftpSession.GetObjectTypeAsync(remotePath, cancellationToken);

            if (objectType == FtpObjectType.Directory)
            {
                if (string.IsNullOrWhiteSpace(Path.GetExtension(localPath)))
                {
                    if (!Directory.Exists(localPath))
                    {
                        if (Create)
                        {
                            Directory.CreateDirectory(localPath);
                        }
                        else
                        {
                            throw new ArgumentException("PathNotFoundException", localPath);
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("IncompatiblePathsException");
                }
            }
            else
            {
                if (objectType == FtpObjectType.File)
                {
                    if (string.IsNullOrWhiteSpace(Path.GetExtension(localPath)))
                    {
                        localPath = Path.Combine(localPath, Path.GetFileName(remotePath));
                    }

                    string directoryPath = Path.GetDirectoryName(localPath);

                    if (!Directory.Exists(directoryPath))
                    {
                        if (Create)
                        {
                            Directory.CreateDirectory(directoryPath);
                        }
                        else
                        {
                            throw new InvalidOperationException("PathNotFoundException");
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException("UnsupportedObjectTypeException");
                }
            }

            if (Overwrite)
            {
                await ftpSession.DownloadAsync(remotePath, localPath, FtpLocalExists.Overwrite, Recursive, cancellationToken);
            }
            else
            {
                await ftpSession.DownloadAsync(remotePath, localPath, FtpLocalExists.Append, Recursive, cancellationToken);
            }

            return((asyncCodeActivityContext) =>
            {
            });
        }
예제 #7
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            PropertyDescriptor ftpSessionProperty = context.DataContext.GetProperties()[WithFtpSession.FtpSessionPropertyName];
            IFtpSession        ftpSession         = ftpSessionProperty?.GetValue(context.DataContext) as IFtpSession;

            if (ftpSession == null)
            {
                throw new InvalidOperationException(Resources.FTPSessionNotFoundException);
            }

            string localPath  = LocalPath.Get(context);
            string remotePath = RemotePath.Get(context);

            if (Directory.Exists(localPath))
            {
                if (string.IsNullOrWhiteSpace(Path.GetExtension(remotePath)))
                {
                    if (!(await ftpSession.DirectoryExistsAsync(remotePath, cancellationToken)))
                    {
                        if (Create)
                        {
                            await ftpSession.CreateDirectoryAsync(remotePath, cancellationToken);
                        }
                        else
                        {
                            throw new ArgumentException(string.Format(Resources.PathNotFoundException, remotePath));
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException(Resources.IncompatiblePathsException);
                }
            }
            else
            {
                if (File.Exists(localPath))
                {
                    if (string.IsNullOrWhiteSpace(Path.GetExtension(remotePath)))
                    {
                        remotePath = FtpConfiguration.CombinePaths(remotePath, Path.GetFileName(localPath));
                    }

                    string directoryPath = FtpConfiguration.GetDirectoryPath(remotePath);

                    if (!(await ftpSession.DirectoryExistsAsync(directoryPath, cancellationToken)))
                    {
                        if (Create)
                        {
                            await ftpSession.CreateDirectoryAsync(directoryPath, cancellationToken);
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format(Resources.PathNotFoundException, directoryPath));
                        }
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format(Resources.PathNotFoundException, localPath));
                }
            }

            await ftpSession.UploadAsync(localPath, remotePath, Overwrite, Recursive, cancellationToken);

            return((asyncCodeActivityContext) =>
            {
            });
        }
예제 #8
0
 public void setup()
 {
     _subject = new FtpSession();
 }