Ejemplo n.º 1
0
        private static void Main(Arguments args)
        {
            if (args.BackgroundConnectionCount < 0)
            {
                throw new ArgumentValidationException("Parallel count cannot be negative.");
            }

            if (args.BackgroundConnectionCount < 1)
            {
                throw new NotSupportedException("Current implementaton requires at least one background connection (this might be improved in the future).");
            }

            if (args.Verbose)
            {
                FtpTrace.AddListener(new ConsoleTraceListener());
            }

            var ftpUrl   = new Uri(args.FtpUrl);
            var password = Environment.GetEnvironmentVariable(args.FtpPasswordVariableName, EnvironmentVariableTarget.Process);

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentValidationException($"Password env variable '{args.FtpPasswordVariableName}' is not set for the current process (user/machine vars are ignored).");
            }

            FluentConsole.White.Line(ftpUrl);
            var started = DateTime.Now;

            var basePath    = ftpUrl.LocalPath;
            var credentials = new NetworkCredential(args.FtpUserName, password);
            var sourceInfo  = Directory.Exists(args.SourcePath) ? (FileSystemInfo) new DirectoryInfo(args.SourcePath) : new FileInfo(args.SourcePath);

            using (var mainClient = CreateFtpClient(ftpUrl, credentials, args.FtpUseActive, retry: args.InterimFtpRetryLogin))
                using (var backgroundPool = new FtpClientPool(() => CreateFtpClient(ftpUrl, credentials, args.FtpUseActive, retry: true), args.BackgroundConnectionCount))
                    using (var process = new Process(mainClient, backgroundPool, args.Excludes.AsReadOnlyList())) {
                        process.SynchronizeTopLevel(sourceInfo, basePath);
                    }

            FluentConsole.NewLine().Green.Line(@"Finished in {0:dd\.hh\:mm\:ss}.", DateTime.Now - started);
        }
Ejemplo n.º 2
0
        public FtpFileStore(
            string host,
            string username,
            string password,
            [Optional] string port = default,
            [Optional] string deleteEmptyFolders       = default,
            [Optional] string maxConcurrentConnections = default)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ArgumentException($"{nameof(host)} required.");
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException($"{nameof(username)} required.");
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException($"{nameof(password)} required.");
            }

            if (!bool.TryParse(deleteEmptyFolders ?? $"{true}", out _deleteEmptyFolders))
            {
                throw new ArgumentException(
                          $"{nameof(deleteEmptyFolders)} is invalid value `{deleteEmptyFolders}`");
            }

            if (!int.TryParse(
                    maxConcurrentConnections ?? $"{DefaultMaxConcurrentConnections}",
                    out _maxConcurrentConnections) ||
                _maxConcurrentConnections < 0)
            {
                throw new ArgumentException(
                          $"{nameof(maxConcurrentConnections)} is invalid value `{maxConcurrentConnections}`");
            }

            _host = host;

            _username = username;

            _password = password;

            ConnectionInfo connectionInfo;

            if (string.IsNullOrWhiteSpace(port))
            {
                connectionInfo = new ConnectionInfo(
                    host: _host,
                    username: _username,
                    password: _password,
                    port: 21);
            }
            else
            {
                if (!int.TryParse(port, out _port) || _port < 1 || _port > 65535)
                {
                    throw new ArgumentException($"{nameof(port)} is invalid value `{port}`");
                }

                connectionInfo = new ConnectionInfo(
                    host: _host,
                    username: _username,
                    password: _password,
                    port: _port);
            }

            _ftpClientPool = FtpClientPool.GetForHost(
                connectionInfo,
                _maxConcurrentConnections);
        }
Ejemplo n.º 3
0
 public Process(FtpClient mainClient, FtpClientPool backgroundPool, IReadOnlyCollection <string> excludes)
 {
     _mainClient     = mainClient;
     _backgroundPool = backgroundPool;
     _excludes       = excludes.Select(p => new Rule(new Minimatcher(p, MinimatcherOptions), p)).ToArray();
 }