Пример #1
0
        private async Task CreateFtpClientAsync(FtpConfig config, Func <string, Task <string> > securePasswordCallBack)
        {
            var passwd = await securePasswordCallBack.Invoke(config.Ftp.Credentials.AzureKeyVault.SecretName);

            _ftpClient = (config.Ftp.Protocol.Equals(FtpConstants.Ftps, StringComparison.InvariantCultureIgnoreCase) || config.Ftp.Port.Equals(990)) ?
                         new FluentFTP.FtpClient(config.Ftp.Host)
            {            //FTPS explicitly or Port = 990 => automatically create FTPS
                Credentials = new NetworkCredential(config.Ftp.Credentials.Username, passwd),
                Port        = config.Ftp.Port,
                EnableThreadSafeDataConnections = true,
                DataConnectionType        = config.Ftp.DataConnectionType == FtpConstants.FtpActiveMode ? FtpDataConnectionType.AutoActive : FtpDataConnectionType.AutoPassive,
                EncryptionMode            = config.Ftp.Port.Equals(990) ? FtpEncryptionMode.Implicit : FtpEncryptionMode.Explicit,
                DataConnectionReadTimeout = 30000
            }
                     :
            new FluentFTP.FtpClient(config.Ftp.Host)
            {
                Credentials = new NetworkCredential(config.Ftp.Credentials.Username, await securePasswordCallBack.Invoke(config.Ftp.Credentials.AzureKeyVault.SecretName)),
                Port        = config.Ftp.Port,
                EnableThreadSafeDataConnections = true,
                Encoding                  = Encoding.UTF8,
                DataConnectionType        = config.Ftp.DataConnectionType == FtpConstants.FtpActiveMode ? FtpDataConnectionType.AutoActive : FtpDataConnectionType.AutoPassive,
                DataConnectionReadTimeout = 30000
            };

            _ftpClient.ValidateCertificate += (control, e) =>
            {
                e.Accept = (string.IsNullOrEmpty(config.Ftp.ValidateServerCertificate) ||
                            config.Ftp.ValidateServerCertificate.Equals("false", StringComparison.InvariantCultureIgnoreCase)) ? true
                    : (e.Certificate.GetRawCertDataString() == config.Ftp.ValidateServerCertificate || e.PolicyErrors == SslPolicyErrors.None);
            };

            _ftpPolicyRegistry.GetPolicy(FtpPolicyName.FtpConnectPolicy).Execute(() => _ftpClient.Connect());
        }
        private Task DownloadVersionItemAsync(string version, string filename, string downloadPath)
        {
            FluentFTP.FtpTrace.LogToFile = "log_FluentFTP_actions.logs";

            FluentFTP.FtpTrace.LogUserName = false; // hide FTP user names
            FluentFTP.FtpTrace.LogPassword = false; // hide FTP passwords
            FluentFTP.FtpTrace.LogIP       = true;  // hide FTP server IP addresses

            //var client = new FluentFTP.FtpClient(_ftpUpdaterOptions.Host, _ftpUpdaterOptions.Username, _ftpUpdaterOptions.Password);
            //FluentFTP.FtpVerify.Retry | FluentFTP.FtpVerify.Delete | FluentFTP.FtpVerify.Throw
            var client = new FluentFTP.FtpClient(_ftpUpdaterOptions.Host, _ftpUpdaterOptions.Username, _ftpUpdaterOptions.Password);

            client.EncryptionMode = FtpEncryptionMode.None;

            if (_ftpUpdaterOptions.TrustClientCertificate)
            {
                client.ValidateCertificate += Client_ValidateCertificate;
            }
            //client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Ssl2;

            var ftpFolder        = $"{_ftpUpdaterOptions.FtpFolderHostingTheApp}/{version}";
            var ftpFolderAndFile = $"{ftpFolder}/{filename}";

            //var lst = client.GetListing();
            return(client.DownloadFileAsync(downloadPath, ftpFolderAndFile, FluentFTP.FtpLocalExists.Overwrite, FluentFTP.FtpVerify.Delete | FluentFTP.FtpVerify.Throw, defaultProgress));
        }
Пример #3
0
        public FtpClient(object options)
        {
            BlendedJSEngine.Clients.Value.Add(this);
            _client = new FluentFTP.FtpClient();

            var host = options.GetProperty("host").ToStringOrDefault();

            if (host != null)
            {
                _client.Host = host;
            }

            var port = options.GetProperty("port").ToIntOrDefault();

            if (port != null)
            {
                _client.Port = port.Value;
            }

            var user     = options.GetProperty("user").ToStringOrDefault();
            var password = options.GetProperty("password").ToStringOrDefault();

            if (user != null && password != null)
            {
                _client.Credentials = new NetworkCredential(user, password);
            }
        }
        public FtpFileSystemOperations(string address, string username, string password, Encoding encoding = null, FtpDataConnectionType connectionType = FtpDataConnectionType.AutoPassive)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            address = address.Replace("\\", "/");
            if (!address.StartsWith("ftp://", StringComparison.CurrentCultureIgnoreCase))
            {
                address = "ftp://" + address;
            }

            var url           = new Uri(address);
            var host          = url.Host;
            var rootDirectory = url.AbsolutePath;
            var port          = url.Port > 0 ? url.Port : 21;

            if (!string.IsNullOrEmpty(rootDirectory) && rootDirectory != "/")
            {
                throw new ArgumentNullException(nameof(address));
            }

            Client = new FtpClient(host, port, new NetworkCredential(username, password))
            {
                Encoding           = encoding ?? Encoding.UTF8,
                DataConnectionType = connectionType,
                //EncryptionMode = FtpEncryptionMode.Explicit,
                RetryAttempts = 3
            };
            Client.ValidateCertificate += (control, args) => args.Accept = true;
        }
Пример #5
0
        private FluentFTP.IFtpClient _getClient()
        {
            FluentFTP.FtpClient client;

            if (Uri != null)
            {
                client = new FluentFTP.FtpClient(Uri)
                {
                    Credentials     = Credentials,
                    SocketKeepAlive = true,
                    //SocketPollInterval = 1000,
                    //ConnectTimeout = 5000,
                    //DataConnectionConnectTimeout = 5000,
                };
            }
            else
            {
                client = new FluentFTP.FtpClient(Host)
                {
                    Credentials     = Credentials,
                    SocketKeepAlive = true,
                    //SocketPollInterval = 1000,
                    //ConnectTimeout = 5000,
                    //DataConnectionConnectTimeout = 5000,
                };
            }

            return(client);
        }
Пример #6
0
 /// <summary>
 /// Active FTP client
 /// </summary>
 public void Connecte()
 {
     if (!IsActiveFTPClient())
     {
         _FTPClient = new FluentFTP.FtpClient(connection.ServerName, connection.User, new ADTool.Encryption.CryptLib().Decrypt(connection.Password));
         _FTPClient.Connect();
     }
 }
Пример #7
0
 /// <summary>
 /// Disconnect FTP client and free memory used
 /// </summary>
 public void Disconnect()
 {
     if (IsActiveFTPClient())
     {
         _FTPClient.Disconnect();
     }
     _FTPClient = null;
 }
Пример #8
0
 public FtpWrapper(IEnumerable <ApplicationFileInfo> localSet, string rootDirectory = null, string[] skipFiles = null)
 {
     this.localSet      = localSet;
     this.rootDirectory = rootDirectory;
     this.skipFiles     = skipFiles;
     this.ftp           = null;
     scanLocalSet();
 }
Пример #9
0
        private async void button1_Click(object sender, EventArgs e)
        {
            FtpTrace.AddListener(log);
            ftp = new FtpClient("ftp.darkmaster.no", 21, "darkmaster.no", "styx2007");
            await ftp.ConnectAsync();

            explorer1.ftpClient    = ftp;
            explorer1.IsPcExplorer = false;
            await explorer1.RefreshCurrentFtpFolder(true);
        }
Пример #10
0
 public void Dispose()
 {
     if (ftp != null)
     {
         if (ftp.IsConnected)
         {
             ftp.Disconnect();
         }
         ftp.Dispose();
         ftp = null;
     }
 }
Пример #11
0
        private FluentFTP.IFtpClient _getClient()
        {
            var client = new FluentFTP.FtpClient(Host)
            {
                Credentials     = Credentials,
                SocketKeepAlive = true,
                //SocketPollInterval = 1000,
                //ConnectTimeout = 5000,
                //DataConnectionConnectTimeout = 5000,
            };

            return(client);
        }
Пример #12
0
        private FtpClient GetNewClient()
        {
            var _ftpClient = new FtpClient(_config.HostName);

            _ftpClient.Credentials = _config.Username == null ?
                                     new NetworkCredential() :
                                     new NetworkCredential(_config.Username, _config.Password);
            _ftpClient.SslProtocols             = _config.SslProtocols;
            _ftpClient.DataConnectionEncryption = _ftpClient.EncryptionMode != FtpEncryptionMode.None;
            _ftpClient.Connect();

            return(_ftpClient);
        }
Пример #13
0
 /// <summary>
 /// 创建ftp客户端
 /// </summary>
 private void GetFtpClient()
 {
     if (CheckPara())
     {
         try
         {
             ftpClient = new FluentFTP.FtpClient(strFtpUri, intFtpPort, strFtpUserID, strFtpPassword);
             ftpClient.RetryAttempts = intRetryTimes;
         }
         catch (Exception ex)
         {
             Log4netUtil.Log4NetHelper.Error(String.Format(@"GetFtpClient->创建ftp客户端异常:{0}", ex.Message), @"Ftp");
         }
     }
 }
Пример #14
0
        private async Task <FluentFTP.FtpClient> CreateClient(Uri uri)
        {
            if (_cacheClient == null ||
                !_cacheClient.IsConnected ||
                !_cacheClient.IsAuthenticated)
            {
                var port   = uri.Port == -1 ? 21 : uri.Port;
                var client = new FluentFTP.FtpClient(uri.Host, port, Credential?.UserName, Credential?.Password);
                client.ValidateAnyCertificate = true;
                await client.AutoConnectAsync();

                _cacheClient = client;
                _log.Information("Established connection with ftp server at {host}:{port}, encrypted: {enc}", uri.Host, port, _cacheClient.IsEncrypted);
            }
            return(_cacheClient);
        }
Пример #15
0
 public bool Connect(string host, int port, string username, string password)
 {
     try
     {
         FluentFTP.FtpTrace.EnableTracing = false;
         ftp = new FluentFTP.FtpClient(host, port, username, password);
         ftp.EnableThreadSafeDataConnections = false;
         ftp.TransferChunkSize = transferChunkSize;
         ftp.Connect();
         return(ftp.IsConnected);
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex.Message);
     }
     return(false);
 }
Пример #16
0
 /// <summary>
 /// 连接FTP
 /// </summary>
 /// <returns></returns>
 public bool Connect()
 {
     try
     {
         ftp = new FluentFTP.FtpClient(Host, Port, User, Password)
         {
             EncryptionMode         = FtpEncryptionMode.None,
             ValidateAnyCertificate = true
         };
         ftp.Connect();
         return(true);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(false);
     }
 }
Пример #17
0
        public IFtpClient ConnectToServer()
        {
            FluentFTP.FtpClient client;
            if (!string.IsNullOrWhiteSpace(UserName))
            {
                client = new FluentFTP.FtpClient(ServerUrl, GetCredential());
            }
            else
            {
                client = new FluentFTP.FtpClient(ServerUrl);
            }

            client.EncryptionMode       = FtpEncryptionMode.None;
            client.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);

            return(new FtpRapper(client)
            {
                DependencyService = DependencyService.Resolve <IResolve>()
            });
        }
Пример #18
0
 /// <summary>
 /// These flags must be copied when we quickly clone the connection.
 /// </summary>
 private void CopyStateFlags(FtpClient original)
 {
     this._EPSVNotSupported          = original._EPSVNotSupported;
     this._FileSizeASCIINotSupported = original._FileSizeASCIINotSupported;
 }
Пример #19
0
        /// <summary>
        /// Transfers a file from the source FTP Server to the destination FTP Server via the FXP protocol asynchronously.
        /// </summary>
        private async Task <bool> TransferFileFXPInternalAsync(string sourcePath, FtpClient remoteClient, string remotePath, bool createRemoteDir, FtpRemoteExists existsMode,
                                                               IProgress <FtpProgress> progress, CancellationToken token, FtpProgress metaProgress)
        {
            FtpReply reply;
            long     offset     = 0;
            bool     fileExists = false;
            long     fileSize   = 0;

            var ftpFxpSession = await OpenPassiveFXPConnectionAsync(remoteClient, token);

            if (ftpFxpSession != null)
            {
                try {
                    ftpFxpSession.SourceServer.ReadTimeout = (int)TimeSpan.FromMinutes(30.0).TotalMilliseconds;
                    ftpFxpSession.TargetServer.ReadTimeout = (int)TimeSpan.FromMinutes(30.0).TotalMilliseconds;


                    // check if the file exists, and skip, overwrite or append
                    if (existsMode == FtpRemoteExists.AppendNoCheck)
                    {
                        offset = await remoteClient.GetFileSizeAsync(remotePath, token);

                        if (offset == -1)
                        {
                            offset = 0;                             // start from the beginning
                        }
                    }
                    else
                    {
                        fileExists = await remoteClient.FileExistsAsync(remotePath, token);

                        switch (existsMode)
                        {
                        case FtpRemoteExists.Skip:

                            if (fileExists)
                            {
                                LogStatus(FtpTraceLevel.Info, "Skip is selected => Destination file exists => skipping");

                                //Fix #413 - progress callback isn't called if the file has already been uploaded to the server
                                //send progress reports
                                if (progress != null)
                                {
                                    progress.Report(new FtpProgress(100.0, 0, TimeSpan.FromSeconds(0), sourcePath, remotePath, metaProgress));
                                }

                                return(true);
                            }

                            break;

                        case FtpRemoteExists.Overwrite:

                            if (fileExists)
                            {
                                await remoteClient.DeleteFileAsync(remotePath, token);
                            }

                            break;

                        case FtpRemoteExists.Append:

                            if (fileExists)
                            {
                                offset = await remoteClient.GetFileSizeAsync(remotePath, token);

                                if (offset == -1)
                                {
                                    offset = 0;                                             // start from the beginning
                                }
                            }

                            break;
                        }
                    }

                    fileSize = await GetFileSizeAsync(sourcePath, token);

                    // ensure the remote dir exists .. only if the file does not already exist!
                    if (createRemoteDir && !fileExists)
                    {
                        var dirname = remotePath.GetFtpDirectoryName();
                        if (!await remoteClient.DirectoryExistsAsync(dirname, token))
                        {
                            await remoteClient.CreateDirectoryAsync(dirname, token);
                        }
                    }

                    if (offset == 0 && existsMode != FtpRemoteExists.AppendNoCheck)
                    {
                        // send command to tell the source server to 'send' the file to the destination server
                        if (!(reply = await ftpFxpSession.SourceServer.ExecuteAsync($"RETR {sourcePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        //Instruct destination server to store the file
                        if (!(reply = await ftpFxpSession.TargetServer.ExecuteAsync($"STOR {remotePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }
                    }
                    else
                    {
                        //tell source server to restart / resume
                        if (!(reply = await ftpFxpSession.SourceServer.ExecuteAsync($"REST {offset}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        // send command to tell the source server to 'send' the file to the destination server
                        if (!(reply = await ftpFxpSession.SourceServer.ExecuteAsync($"RETR {sourcePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        //Instruct destination server to append the file
                        if (!(reply = await ftpFxpSession.TargetServer.ExecuteAsync($"APPE {remotePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }
                    }

                    var  transferStarted = DateTime.Now;
                    long lastSize        = 0;


                    var sourceFXPTransferReply      = ftpFxpSession.SourceServer.GetReplyAsync(token);
                    var destinationFXPTransferReply = ftpFxpSession.TargetServer.GetReplyAsync(token);

                    while (!sourceFXPTransferReply.IsCompleted || !destinationFXPTransferReply.IsCompleted)
                    {
                        if (remoteClient.EnableThreadSafeDataConnections)
                        {
                            // send progress reports
                            if (progress != null && fileSize != -1)
                            {
                                offset = await remoteClient.GetFileSizeAsync(remotePath, token);

                                if (offset != -1 && lastSize <= offset)
                                {
                                    long bytesProcessed = offset - lastSize;
                                    lastSize = offset;
                                    ReportProgress(progress, fileSize, offset, bytesProcessed, DateTime.Now - transferStarted, sourcePath, remotePath, metaProgress);
                                }
                            }
                        }

                        await Task.Delay(1000);
                    }

                    FtpTrace.WriteLine(FtpTraceLevel.Info, $"FXP transfer of file {sourcePath} has completed");

                    await NoopAsync(token);

                    await remoteClient.NoopAsync(token);

                    ftpFxpSession.Dispose();

                    return(true);
                }

                // Fix: catch all exceptions and dispose off the FTP clients if one occurs
                catch (Exception ex) {
                    ftpFxpSession.Dispose();
                    throw ex;
                }
            }
            else
            {
                FtpTrace.WriteLine(FtpTraceLevel.Error, "Failed to open FXP passive Connection");
                return(false);
            }
        }
Пример #20
0
        /// <summary>
        /// Parses a line from a file listing using the first successful match in the Parsers collection.
        /// </summary>
        /// <param name="path">The source path of the file listing</param>
        /// <param name="buf">A line from the file listing</param>
        /// <param name="capabilities">Server capabilities</param>
        /// <returns>A FtpListItem object representing the parsed line, null if the line was
        /// unable to be parsed. If you have encountered an unsupported list type add a parser
        /// to the public static Parsers collection of FtpListItem.</returns>
        public static FtpListItem ParseLegacy(string path, string buf, FtpCapability capabilities, FtpClient client)
        {
            if (!string.IsNullOrEmpty(buf))
            {
                FtpListItem item;

                foreach (Parser parser in Parsers)
                {
                    if ((item = parser(buf, capabilities, client)) != null)
                    {
                        item.Input = buf;
                        return(item);
                    }
                }
            }

            return(null);
        }
Пример #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FtpListParser"/> class.
 /// </summary>
 /// <param name="client">An existing <see cref="FtpClient"/> object</param>
 public FtpListParser(FtpClient client)
 {
     this.client = client;
 }
Пример #22
0
        /// <summary>
        /// Get the full path of a given FTP Listing entry
        /// </summary>
        public static void CalculateFullFtpPath(this FtpListItem item, FtpClient client, string path, bool isVMS)
        {
            // EXIT IF NO DIR PATH PROVIDED
            if (path == null)
            {
                // check if the path is absolute
                if (IsAbsolutePath(item.Name))
                {
                    item.FullName = item.Name;
                    item.Name     = item.Name.GetFtpFileName();
                }

                return;
            }


            // ONLY IF DIR PATH PROVIDED

            // if this is a vax/openvms file listing
            // there are no slashes in the path name
            if (isVMS)
            {
                item.FullName = path + item.Name;
            }
            else
            {
                //this.client.LogStatus(item.Name);

                // remove globbing/wildcard from path
                if (path.GetFtpFileName().Contains("*"))
                {
                    path = path.GetFtpDirectoryName();
                }

                if (item.Name != null)
                {
                    // absolute path? then ignore the path input to this method.
                    if (IsAbsolutePath(item.Name))
                    {
                        item.FullName = item.Name;
                        item.Name     = item.Name.GetFtpFileName();
                    }
                    else if (path != null)
                    {
                        item.FullName = path.GetFtpPath(item.Name);                         //.GetFtpPathWithoutGlob();
                    }
                    else
                    {
                        client.LogStatus(FtpTraceLevel.Warn, "Couldn't determine the full path of this object: " +
                                         Environment.NewLine + item.ToString());
                    }
                }


                // if a link target is set and it doesn't include an absolute path
                // then try to resolve it.
                if (item.LinkTarget != null && !item.LinkTarget.StartsWith("/"))
                {
                    if (item.LinkTarget.StartsWith("./"))
                    {
                        item.LinkTarget = path.GetFtpPath(item.LinkTarget.Remove(0, 2)).Trim();
                    }
                    else
                    {
                        item.LinkTarget = path.GetFtpPath(item.LinkTarget).Trim();
                    }
                }
            }
        }
Пример #23
0
        /// <summary>
        /// Transfer the specified directory from the source FTP Server onto the remote FTP Server asynchronously using the FXP protocol.
        /// You will need to create a valid connection to your remote FTP Server before calling this method.
        /// In Update mode, we will only transfer missing files and preserve any extra files on the remote FTP Server. This is useful when you want to simply transfer missing files from an FTP directory.
        /// Currently Mirror mode is not implemented.
        /// Only transfers the files and folders matching all the rules provided, if any.
        /// All exceptions during transfer are caught, and the exception is stored in the related FtpResult object.
        /// </summary>
        /// <param name="sourceFolder">The full or relative path to the folder on the source FTP Server. If it does not exist, an empty result list is returned.</param>
        /// <param name="remoteClient">Valid FTP connection to the destination FTP Server</param>
        /// <param name="remoteFolder">The full or relative path to destination folder on the remote FTP Server</param>
        /// <param name="mode">Only Update mode is currently implemented</param>
        /// <param name="existsMode">If the file exists on disk, should we skip it, resume the download or restart the download?</param>
        /// <param name="verifyOptions">Sets if checksum verification is required for a successful download and what to do if it fails verification (See Remarks)</param>
        /// <param name="rules">Only files and folders that pass all these rules are downloaded, and the files that don't pass are skipped. In the Mirror mode, the files that fail the rules are also deleted from the local folder.</param>
        /// <param name="progress">Provide a callback to track download progress.</param>
        /// <param name="token">The token that can be used to cancel the entire process</param>
        /// <remarks>
        /// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server.  If the server does not support
        /// any hash algorithm, then verification is ignored.  If only <see cref="FtpVerify.OnlyChecksum"/> is set then the return of this method depends on both a successful
        /// upload &amp; verification.  Additionally, if any verify option is set and a retry is attempted then overwrite will automatically switch to true for subsequent attempts.
        /// If <see cref="FtpVerify.Throw"/> is set and <see cref="FtpError.Throw"/> is <i>not set</i>, then individual verification errors will not cause an exception
        /// to propagate from this method.
        /// </remarks>
        /// <returns>
        /// Returns a listing of all the remote files, indicating if they were downloaded, skipped or overwritten.
        /// Returns a blank list if nothing was transfered. Never returns null.
        /// </returns>
        public async Task <List <FtpResult> > TransferDirectoryAsync(string sourceFolder, FtpClient remoteClient, string remoteFolder, FtpFolderSyncMode mode = FtpFolderSyncMode.Update,
                                                                     FtpRemoteExists existsMode = FtpRemoteExists.Skip, FtpVerify verifyOptions = FtpVerify.None, List <FtpRule> rules = null, IProgress <FtpProgress> progress = null, CancellationToken token = default(CancellationToken))
        {
            if (sourceFolder.IsBlank())
            {
                throw new ArgumentException("Required parameter is null or blank.", "sourceFolder");
            }

            if (remoteFolder.IsBlank())
            {
                throw new ArgumentException("Required parameter is null or blank.", "remoteFolder");
            }

            // cleanup the FTP paths
            sourceFolder = sourceFolder.GetFtpPath().EnsurePostfix("/");
            remoteFolder = remoteFolder.GetFtpPath().EnsurePostfix("/");

            LogFunc(nameof(TransferDirectoryAsync), new object[] { sourceFolder, remoteClient, remoteFolder, mode, existsMode, verifyOptions, (rules.IsBlank() ? null : rules.Count + " rules") });

            var results = new List <FtpResult>();

            // if the source dir does not exist, fail fast
            if (!await DirectoryExistsAsync(sourceFolder, token))
            {
                return(results);
            }

            // flag to determine if existence checks are required
            var checkFileExistence = true;

            // ensure the remote dir exists
            if (!await remoteClient.DirectoryExistsAsync(remoteFolder, token))
            {
                await remoteClient.CreateDirectoryAsync(remoteFolder, token);

                checkFileExistence = false;
            }

            // break if task is cancelled
            token.ThrowIfCancellationRequested();

            // collect paths of the files that should exist (lowercase for CI checks)
            var shouldExist = new Dictionary <string, bool>();

            // get all the folders in the local directory
            var dirListing = (await GetListingAsync(sourceFolder, FtpListOption.Recursive, token)).Where(x => x.Type == FtpFileSystemObjectType.Directory).Select(x => x.FullName).ToArray();

            // break if task is cancelled
            token.ThrowIfCancellationRequested();

            // get all the already existing files
            var remoteListing = checkFileExistence ? await remoteClient.GetListingAsync(remoteFolder, FtpListOption.Recursive, token) : null;

            // break if task is cancelled
            token.ThrowIfCancellationRequested();

            // loop thru each folder and ensure it exists #1
            var dirsToUpload = GetSubDirectoriesToTransfer(sourceFolder, remoteFolder, rules, results, dirListing);

            // break if task is cancelled
            token.ThrowIfCancellationRequested();

            /*-------------------------------------------------------------------------------------/
            *   Cancelling after this point would leave the FTP server in an inconsistent state   *
            *-------------------------------------------------------------------------------------*/

            // loop thru each folder and ensure it exists #2
            await CreateSubDirectoriesAsync(remoteClient, dirsToUpload, token);

            // get all the files in the local directory
            var fileListing = (await GetListingAsync(sourceFolder, FtpListOption.Recursive, token)).Where(x => x.Type == FtpFileSystemObjectType.File).Select(x => x.FullName).ToArray();

            // loop thru each file and transfer it
            var filesToUpload = GetFilesToTransfer(sourceFolder, remoteFolder, rules, results, shouldExist, fileListing);

            await TransferServerFilesAsync(filesToUpload, remoteClient, existsMode, verifyOptions, progress, remoteListing, token);

            // delete the extra remote files if in mirror mode and the directory was pre-existing
            // DeleteExtraServerFiles(mode, shouldExist, remoteListing);

            return(results);
        }
Пример #24
0
        private async Task TransferServerFilesAsync(List <FtpResult> filesToTransfer, FtpClient remoteClient, FtpRemoteExists existsMode, FtpVerify verifyOptions, IProgress <FtpProgress> progress, FtpListItem[] remoteListing, CancellationToken token)
        {
            LogFunc(nameof(TransferServerFilesAsync), new object[] { filesToTransfer.Count + " files" });

            int r = -1;

            foreach (var result in filesToTransfer)
            {
                r++;

                // absorb errors
                try {
                    // skip uploading if the file already exists on the server
                    FtpRemoteExists existsModeToUse;
                    if (!CanUploadFile(result, remoteListing, existsMode, out existsModeToUse))
                    {
                        continue;
                    }

                    // create meta progress to store the file progress
                    var metaProgress = new FtpProgress(filesToTransfer.Count, r);

                    // transfer the file
                    var transferred = await TransferFileAsync(result.LocalPath, remoteClient, result.RemotePath, false, existsModeToUse, verifyOptions, progress, metaProgress, token);

                    result.IsSuccess = transferred.IsSuccess();
                    result.IsSkipped = transferred == FtpStatus.Skipped;
                }
                catch (Exception ex) {
                    LogStatus(FtpTraceLevel.Warn, "File failed to transfer: " + result.LocalPath);

                    // mark that the file failed to upload
                    result.IsFailed  = true;
                    result.Exception = ex;
                }
            }
        }
Пример #25
0
        /// <summary>
        /// Transfer the specified directory from the source FTP Server onto the remote FTP Server using the FXP protocol.
        /// You will need to create a valid connection to your remote FTP Server before calling this method.
        /// In Update mode, we will only transfer missing files and preserve any extra files on the remote FTP Server. This is useful when you want to simply transfer missing files from an FTP directory.
        /// Currently Mirror mode is not implemented.
        /// Only transfers the files and folders matching all the rules provided, if any.
        /// All exceptions during transfer are caught, and the exception is stored in the related FtpResult object.
        /// </summary>
        /// <param name="sourceFolder">The full or relative path to the folder on the source FTP Server. If it does not exist, an empty result list is returned.</param>
        /// <param name="remoteClient">Valid FTP connection to the destination FTP Server</param>
        /// <param name="remoteFolder">The full or relative path to destination folder on the remote FTP Server</param>
        /// <param name="mode">Only Update mode is currently implemented</param>
        /// <param name="existsMode">If the file exists on disk, should we skip it, resume the download or restart the download?</param>
        /// <param name="verifyOptions">Sets if checksum verification is required for a successful download and what to do if it fails verification (See Remarks)</param>
        /// <param name="rules">Only files and folders that pass all these rules are downloaded, and the files that don't pass are skipped. In the Mirror mode, the files that fail the rules are also deleted from the local folder.</param>
        /// <param name="progress">Provide a callback to track download progress.</param>
        /// <remarks>
        /// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server.  If the server does not support
        /// any hash algorithm, then verification is ignored.  If only <see cref="FtpVerify.OnlyChecksum"/> is set then the return of this method depends on both a successful
        /// upload &amp; verification.  Additionally, if any verify option is set and a retry is attempted then overwrite will automatically switch to true for subsequent attempts.
        /// If <see cref="FtpVerify.Throw"/> is set and <see cref="FtpError.Throw"/> is <i>not set</i>, then individual verification errors will not cause an exception
        /// to propagate from this method.
        /// </remarks>
        /// <returns>
        /// Returns a listing of all the remote files, indicating if they were downloaded, skipped or overwritten.
        /// Returns a blank list if nothing was transfered. Never returns null.
        /// </returns>
        public List <FtpResult> TransferDirectory(string sourceFolder, FtpClient remoteClient, string remoteFolder, FtpFolderSyncMode mode = FtpFolderSyncMode.Update,
                                                  FtpRemoteExists existsMode = FtpRemoteExists.Skip, FtpVerify verifyOptions = FtpVerify.None, List <FtpRule> rules = null, Action <FtpProgress> progress = null)
        {
            if (sourceFolder.IsBlank())
            {
                throw new ArgumentException("Required parameter is null or blank.", "sourceFolder");
            }

            if (remoteFolder.IsBlank())
            {
                throw new ArgumentException("Required parameter is null or blank.", "remoteFolder");
            }

            // cleanup the FTP paths
            sourceFolder = sourceFolder.GetFtpPath().EnsurePostfix("/");
            remoteFolder = remoteFolder.GetFtpPath().EnsurePostfix("/");

            LogFunc(nameof(TransferDirectory), new object[] { sourceFolder, remoteClient, remoteFolder, mode, existsMode, verifyOptions, (rules.IsBlank() ? null : rules.Count + " rules") });

            var results = new List <FtpResult>();

            // if the source dir does not exist, fail fast
            if (!DirectoryExists(sourceFolder))
            {
                return(results);
            }

            // flag to determine if existence checks are required
            var checkFileExistence = true;

            // ensure the remote dir exists
            if (!remoteClient.DirectoryExists(remoteFolder))
            {
                remoteClient.CreateDirectory(remoteFolder);
                checkFileExistence = false;
            }

            // collect paths of the files that should exist (lowercase for CI checks)
            var shouldExist = new Dictionary <string, bool>();

            // get all the folders in the local directory
            var dirListing = GetListing(sourceFolder, FtpListOption.Recursive).Where(x => x.Type == FtpFileSystemObjectType.Directory).Select(x => x.FullName).ToArray();

            // get all the already existing files
            var remoteListing = checkFileExistence ? remoteClient.GetListing(remoteFolder, FtpListOption.Recursive) : null;

            // loop thru each folder and ensure it exists #1
            var dirsToUpload = GetSubDirectoriesToTransfer(sourceFolder, remoteFolder, rules, results, dirListing);

            CreateSubDirectories(remoteClient, dirsToUpload);

            // get all the files in the local directory
            var fileListing = GetListing(sourceFolder, FtpListOption.Recursive).Where(x => x.Type == FtpFileSystemObjectType.File).Select(x => x.FullName).ToArray();

            // loop thru each file and transfer it
            var filesToUpload = GetFilesToTransfer(sourceFolder, remoteFolder, rules, results, shouldExist, fileListing);

            TransferServerFiles(filesToUpload, remoteClient, existsMode, verifyOptions, progress, remoteListing);

            // delete the extra remote files if in mirror mode and the directory was pre-existing
            // DeleteExtraServerFiles(mode, shouldExist, remoteListing);

            return(results);
        }
        /// <summary>
        /// Opens a FXP PASV connection between the source FTP Server and the destination FTP Server
        /// </summary>
        /// <param name="remoteClient">FtpClient instance of the destination FTP Server</param>
        /// <returns>A data stream ready to be used</returns>
        private FtpFxpSession OpenPassiveFXPConnection(FtpClient remoteClient, bool trackProgress)
        {
            FtpReply  reply, reply2;
            Match     m;
            FtpClient sourceClient      = null;
            FtpClient destinationClient = null;
            FtpClient progressClient    = null;

            // create a new connection to the source FTP server if EnableThreadSafeDataConnections is set
            if (EnableThreadSafeDataConnections)
            {
                sourceClient = CloneConnection();
                sourceClient._AutoDispose = true;
                sourceClient.CopyStateFlags(this);
                sourceClient.Connect();
                sourceClient.SetWorkingDirectory(GetWorkingDirectory());
            }
            else
            {
                sourceClient = this;
            }

            // create a new connection to the target FTP server if EnableThreadSafeDataConnections is set
            if (remoteClient.EnableThreadSafeDataConnections)
            {
                destinationClient = remoteClient.CloneConnection();
                destinationClient._AutoDispose = true;
                destinationClient.CopyStateFlags(remoteClient);
                destinationClient.Connect();
                destinationClient.SetWorkingDirectory(remoteClient.GetWorkingDirectory());
            }
            else
            {
                destinationClient = remoteClient;
            }

            // create a new connection to the target FTP server to track progress
            // if progress tracking is enabled during this FXP transfer
            if (trackProgress)
            {
                progressClient = remoteClient.CloneConnection();
                progressClient._AutoDispose = true;
                progressClient.CopyStateFlags(remoteClient);
                progressClient.Connect();
                progressClient.SetWorkingDirectory(remoteClient.GetWorkingDirectory());
            }

            sourceClient.SetDataType(sourceClient.FXPDataType);
            destinationClient.SetDataType(destinationClient.FXPDataType);

            // send PASV/CPSV commands to destination FTP server to get passive port to be used from source FTP server
            // first try with PASV - commonly supported by all servers
            if (!(reply = destinationClient.Execute("PASV")).Success)
            {
                // then try with CPSV - known to be supported by glFTPd server
                // FIXES #666 - glFTPd server - 435 Failed TLS negotiation on data channel
                if (!(reply2 = destinationClient.Execute("CPSV")).Success)
                {
                    throw new FtpCommandException(reply);
                }
                else
                {
                    // use the CPSV response and extract the port from it
                    reply = reply2;
                }
            }

            // extract port from response
            m = Regex.Match(reply.Message, @"(?<quad1>\d+)," + @"(?<quad2>\d+)," + @"(?<quad3>\d+)," + @"(?<quad4>\d+)," + @"(?<port1>\d+)," + @"(?<port2>\d+)");
            if (!m.Success || m.Groups.Count != 7)
            {
                throw new FtpException("Malformed PASV response: " + reply.Message);
            }

            // Instruct source server to open a connection to the destination Server

            if (!(reply = sourceClient.Execute($"PORT {m.Value}")).Success)
            {
                throw new FtpCommandException(reply);
            }

            // the FXP session stores the active connections used for this FXP transfer
            return(new FtpFxpSession {
                SourceServer = sourceClient,
                TargetServer = destinationClient,
                ProgressServer = progressClient,
            });
        }
Пример #27
0
 private void OnValidateCertificate(FluentFTP.FtpClient control, FtpSslValidationEventArgs e)
 {
     // add logic to test if certificate is valid here
     e.Accept = true;
 }
Пример #28
0
      private bool Save()
      {
          vm = this.DataContext as PersonViewModel;
          List <string> errors      = vm.GetModelErrors();
          var           enterErrors = errors.Where(pp => /*!pp.StartsWith("Не прикреплен скан документа") && */ !pp.EndsWith(" не заполнено"));

          if (enterErrors.Any() && MessageBox.Show(string.Join(Environment.NewLine, enterErrors) + Environment.NewLine + "Чтобы поправить, нажмите Отмена", "", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
          {
              return(false);
          }

          try
          {
              //await new ProgressRunner().RunAsync(
              //    new Action<ProgressHandler>((progressChanged) =>
              {
                  //progressChanged(1, "Сохранение");


                  //MessageBox.Show(string.Join(Environment.NewLine, errors));


                  var db = new ProvodnikContext();

                  Person p;
                  if (vm.Id.HasValue)
                  {
                      p = db.Persons.Single(pp => pp.Id == vm.Id.Value);

                      MainWindow.Mapper.Value.Map(vm, p);
                      var currents = vm.Documents.Where(pp => pp.Id.HasValue).Select(pp => pp.Id.Value).ToList();
                      var toDelete = (from pd in db.PersonDocs
                                      where pd.PersonId == p.Id && !currents.Contains(pd.Id)
                                      select pd);
                      db.PersonDocs.RemoveRange(toDelete);
                      db.SaveChanges();
                  }
                  else
                  {
                      db.Persons.Add(p = new Person());  // { Fio = vm.Fio });
                      MainWindow.Mapper.Value.Map(vm, p);
                      db.SaveChanges();
                  }


                  //progressChanged(29, "Загрузка сканов");
                  var share = 70.0 / vm.Documents.Count;
                  using (var client = new FluentFTP.FtpClient())
                  {
                      App.ConfigureFtpClient(client);
                      client.Connect();
                      foreach (var d in vm.Documents)
                      {
                          PersonDoc pd;
                          if (d.Id.HasValue)
                          {
                              pd = db.PersonDocs.Single(pp => pp.Id == d.Id.Value);
                          }
                          else
                          {
                              db.PersonDocs.Add(pd = new PersonDoc()
                                {
                                    PersonId = p.Id, IsActive = true, DocTypeId = d.DocTypeId, FileName = d.FileName
                                });
                              db.SaveChanges();
                          }

                          if (d.FileName == null)
                          {
                              ImageSource source = null;
                              Application.Current.Dispatcher.Invoke((Action)(() =>
                                {
                                    source = d.Bitmap.Source;
                                }));
                              if (source != null)
                              {
                                  var remotePath = $@"ProvodnikDocs/{p.Id.ToString()}/{DateTime.Now.Ticks}.jpg";  // "/1_Иванов";

                                  while (client.Upload(ToByteArray(source as BitmapSource), remotePath, FtpRemoteExists.Overwrite, true)
                                         != FtpStatus.Success)
                                  {
                                  }                          //, FtpVerify.Retry);
                                  pd.FileName = remotePath;
                                  d.FileName  = remotePath;  //for GetScanErrors(true)
                                                             /**/

                                  /*using (WebClient wc = new WebClient() { Credentials = new NetworkCredential(App.CurrentConfig.FtpUser, App.CurrentConfig.FtpPassw) })
                                   * {
                                   *  var fileName = $@"{DateTime.Now.Ticks}.jpg";
                                   *  System.IO.File.Copy(d.LocalFileName, fileName);
                                   *
                                   *  var remotePath = $@"ProvodnikDocs/{p.Id.ToString()}/";// "/1_Иванов";
                                   *                                                        //  wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                                   *                                                        //wc.UploadFile("http://u0920601.plsk.regruhosting.ru/" +remotePath, "STOR", fileName);
                                   *                                                        //wc.UploadData("http://u0920601.plsk.regruhosting.ru/" + remotePath, "STOR", ToByteArray(source as BitmapSource));
                                   *                                                        //HttpContent stringContent = new StringContent(paramString);
                                   *                                                        //HttpContent fileStreamContent = new StreamContent(paramFileStream);
                                   * var res= Upload("http://u0920601.plsk.regruhosting.ru/" + remotePath, ToByteArray(source as BitmapSource));
                                   * //  var ff=  res.Result;
                                   *  pd.FileName = remotePath;
                                   *  d.FileName = remotePath; //for GetScanErrors(true)
                                   * }*/
                              }
                              else
                              {
                                  pd.FileName = null;
                              }
                              db.SaveChanges();
                          }
                          pd.PrinesetK = d.PrinesetK;
                          db.SaveChanges();
                          //progressChanged(share);
                      }
                  }

                  vm.FillMessagesAndAlls(p);
                  db.SaveChanges();
              }  //));
          }
          catch (Exception ex) { MessageBox.Show(ex.Message); }

          //ProgressChanged(100);
          IsChanged = false;
          return(true);
      }
Пример #29
0
        /// <summary>
        /// Transfers a file from the source FTP Server to the destination FTP Server via the FXP protocol
        /// </summary>
        private bool TransferFileFXPInternal(string sourcePath, FtpClient remoteClient, string remotePath, bool createRemoteDir, FtpRemoteExists existsMode,
                                             Action <FtpProgress> progress, FtpProgress metaProgress)
        {
            FtpReply reply;
            long     offset     = 0;
            bool     fileExists = false;
            long     fileSize   = 0;

            var ftpFxpSession = OpenPassiveFXPConnection(remoteClient, progress != null);

            if (ftpFxpSession != null)
            {
                try {
                    ftpFxpSession.SourceServer.ReadTimeout = (int)TimeSpan.FromMinutes(30.0).TotalMilliseconds;
                    ftpFxpSession.TargetServer.ReadTimeout = (int)TimeSpan.FromMinutes(30.0).TotalMilliseconds;


                    // check if the file exists, and skip, overwrite or append
                    if (existsMode == FtpRemoteExists.ResumeNoCheck)
                    {
                        offset = remoteClient.GetFileSize(remotePath);
                        if (offset == -1)
                        {
                            offset = 0;                             // start from the beginning
                        }
                    }
                    else
                    {
                        fileExists = remoteClient.FileExists(remotePath);

                        switch (existsMode)
                        {
                        case FtpRemoteExists.Skip:

                            if (fileExists)
                            {
                                LogStatus(FtpTraceLevel.Info, "Skipping file because Skip is enabled and file already exists (Source: " + sourcePath + ", Dest: " + remotePath + ")");

                                //Fix #413 - progress callback isn't called if the file has already been uploaded to the server
                                //send progress reports
                                if (progress != null)
                                {
                                    progress(new FtpProgress(100.0, 0, 0, TimeSpan.FromSeconds(0), sourcePath, remotePath, metaProgress));
                                }

                                return(true);
                            }

                            break;

                        case FtpRemoteExists.Overwrite:

                            if (fileExists)
                            {
                                remoteClient.DeleteFile(remotePath);
                            }

                            break;

                        case FtpRemoteExists.Resume:

                            if (fileExists)
                            {
                                offset = remoteClient.GetFileSize(remotePath);
                                if (offset == -1)
                                {
                                    offset = 0;                                             // start from the beginning
                                }
                            }

                            break;
                        }
                    }

                    fileSize = GetFileSize(sourcePath);

                    // ensure the remote dir exists .. only if the file does not already exist!
                    if (createRemoteDir && !fileExists)
                    {
                        var dirname = remotePath.GetFtpDirectoryName();
                        if (!remoteClient.DirectoryExists(dirname))
                        {
                            remoteClient.CreateDirectory(dirname);
                        }
                    }

                    if (offset == 0 && existsMode != FtpRemoteExists.ResumeNoCheck)
                    {
                        // send command to tell the source server to 'send' the file to the destination server
                        if (!(reply = ftpFxpSession.SourceServer.Execute($"RETR {sourcePath}")).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        //Instruct destination server to store the file
                        if (!(reply = ftpFxpSession.TargetServer.Execute($"STOR {remotePath}")).Success)
                        {
                            throw new FtpCommandException(reply);
                        }
                    }
                    else
                    {
                        //tell source server to restart / resume
                        if (!(reply = ftpFxpSession.SourceServer.Execute($"REST {offset}")).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        // send command to tell the source server to 'send' the file to the destination server
                        if (!(reply = ftpFxpSession.SourceServer.Execute($"RETR {sourcePath}")).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        //Instruct destination server to append the file
                        if (!(reply = ftpFxpSession.TargetServer.Execute($"APPE {remotePath}")).Success)
                        {
                            throw new FtpCommandException(reply);
                        }
                    }

                    var  transferStarted = DateTime.Now;
                    long lastSize        = 0;

                    var sourceFXPTransferReply      = ftpFxpSession.SourceServer.GetReply();
                    var destinationFXPTransferReply = ftpFxpSession.TargetServer.GetReply();

                    // while the transfer is not complete
                    while (!sourceFXPTransferReply.Success || !destinationFXPTransferReply.Success)
                    {
                        // send progress reports every 1 second
                        if (ftpFxpSession.ProgressServer != null)
                        {
                            // send progress reports
                            if (progress != null && fileSize != -1)
                            {
                                offset = ftpFxpSession.ProgressServer.GetFileSize(remotePath);

                                if (offset != -1 && lastSize <= offset)
                                {
                                    long bytesProcessed = offset - lastSize;
                                    lastSize = offset;
                                    ReportProgress(progress, fileSize, offset, bytesProcessed, DateTime.Now - transferStarted, sourcePath, remotePath, metaProgress);
                                }
                            }
                        }
#if CORE14
                        Task.Delay(FXPProgressInterval);
#else
                        Thread.Sleep(FXPProgressInterval);
#endif
                    }

                    FtpTrace.WriteLine(FtpTraceLevel.Info, $"FXP transfer of file {sourcePath} has completed");

                    Noop();
                    remoteClient.Noop();

                    ftpFxpSession.Dispose();

                    return(true);
                }

                // Fix: catch all exceptions and dispose off the FTP clients if one occurs
                catch (Exception ex) {
                    ftpFxpSession.Dispose();
                    throw ex;
                }
            }
            else
            {
                FtpTrace.WriteLine(FtpTraceLevel.Error, "Failed to open FXP passive Connection");
                return(false);
            }
        }
Пример #30
0
 /// <summary>
 /// These flags must be copied when we quickly clone the connection.
 /// </summary>
 private void CopyStateFlags(FtpClient original)
 {
     _EPSVNotSupported          = original._EPSVNotSupported;
     _FileSizeASCIINotSupported = original._FileSizeASCIINotSupported;
     _RecursiveListSupported    = original._RecursiveListSupported;
 }