private static SessionOptions GetSession(UploadChannel uploadChannel)
        {
            var sessionOptions = new SessionOptions()
            {
                HostName = uploadChannel.FtpAddress,
                UserName = uploadChannel.FtpUserName,
                Password = uploadChannel.FtpPassword,
            };

            switch (uploadChannel.FtpProtocol)
            {
            case FtpProtocol.FTP:
                sessionOptions.Protocol   = Protocol.Ftp;
                sessionOptions.PortNumber = 21;
                return(sessionOptions);

            case FtpProtocol.SFTP:
                sessionOptions.Protocol   = Protocol.Sftp;
                sessionOptions.PortNumber = 22;
                return(sessionOptions);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public Result <UploadReportModel> Upload(UploadChannel uploadChannel)
        {
            //lock
            Result <UploadReportModel> result = new UploadReportModel(uploadChannel.ChannelId);

            var sessionOptions = GetSession(uploadChannel);

            using (var session = new Session())
            {
                try
                {
                    session.Open(sessionOptions);
                }
                catch (SessionRemoteException e)
                {
                    result.ToFail(new WinScpConnectionError(e.ToString()));
                    return(result);
                }

                var transferResult = session.PutFiles(GetTransferSourcePath(uploadChannel), GetDestinationPath(uploadChannel),
                                                      false, GetTransferOptions()); //TODO: Should be true

                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    var fileName = transfer.FileName;
                    var uploadAt = DateTime.Now;

                    var file = new SingleFileReportModel()
                    {
                        FileName = fileName, UploadAt = uploadAt
                    };
                    result.Model.Files.Add(file);
                    Console.WriteLine("File sent: " + fileName + " at " + uploadAt);
                }

                try
                {
                    transferResult.Check();
                }
                catch (SessionRemoteException e)
                {
                    result.ToFail(new WinScpUploadError(e.ToString()));
                    return(result);
                }
            }
            return(result);
        }
Пример #3
0
        public void HandleNew(UploadChannel uploadChannel, int trial = 2)
        {
            var uploadResult = _winScpUploader.Upload(uploadChannel);

            if (uploadResult.IsSuccess)
            {
                try
                {
                    var reportBody = uploadResult.Model.GetSerializedBytesBody();
                    _channel.BasicPublish(
                        exchange: _config.RabbitMqPublishingExchangeName,
                        routingKey: "upload.success",
                        basicProperties: _rabbitMqProperties,
                        body: reportBody);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Rabbit MQ Connection Error");
                }
            }
            else if (trial > 0)
            {
                DelayedHandleNew(uploadChannel, trial - 1);
            }
            else
            {
                try
                {
                    var failResult = new UploadFailRabbitMqPackage(uploadResult);
                    _channel.BasicPublish(
                        exchange: _config.RabbitMqPublishingExchangeName,
                        routingKey: failResult.RoutingKey,
                        basicProperties: _rabbitMqProperties,
                        body: failResult.ReportBody);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Rabbit MQ Connection Error");
                }
            }
        }
Пример #4
0
        private async Task DelayedHandleNew(UploadChannel uploadChannel, int trial = 2)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            HandleNew(uploadChannel, trial);
        }
 private static string GetTransferSourcePath(UploadChannel uploadChannel) => Path.Combine(uploadChannel.SourceLocalPath, "*." + uploadChannel.ExtensionName);
 private static string GetDestinationPath(UploadChannel uploadChannel) => Path.Combine(uploadChannel.DestinationPath, "*");