Exemplo n.º 1
0
        public TransmitResult TransmitFile(string localFile, string remoteServer, string remoteDirectory, string userId, string password)
        {
            var file = new FileInfo(localFile);

            if (!file.Exists)
            {
                return(new TransmitResult
                {
                    Message = string.Format("Invalid filepath: \"{0}\".", localFile),
                    Transmitted = false,
                });
            }

            var configuration = new SecureBlackboxFtpConfiguration
            {
                FtpRemoteServer    = remoteServer,
                FtpRemoteDirectory = remoteDirectory,
                FtpUsername        = userId,
                FtpPassword        = password,
                FtpPort            = 21,
                FtpSsl             = false,
            };

            try
            {
                var localDirectory = file.DirectoryName;

                var handler = new SecureBlackboxFtpHandler(configuration);
                handler.SendFiles(localDirectory, remoteDirectory,
                                  x => x.Equals(localFile, StringComparison.OrdinalIgnoreCase));

                return(new TransmitResult
                {
                    Message = "Transfer successful",
                    Transmitted = true,
                });
            }
            catch (Exception ex)
            {
                return(new TransmitResult
                {
                    Message = ex.Message,
                    Transmitted = false,
                });
            }
        }
Exemplo n.º 2
0
        public void TransportFiles(ErcotFileContext context, CspDunsPortModel port, MarketFileModel[] models, CancellationToken token)
        {
            var configuration = new SecureBlackboxFtpConfiguration
            {
                FtpRemoteServer    = port.FtpRemoteServer,
                FtpRemoteDirectory = string.Empty,
                FtpUsername        = port.FtpUserId,
                FtpPassword        = port.FtpPassword,
                FtpPort            = 21,
                FtpSsl             = false,
            };

            var ftpHandler = new SecureBlackboxFtpHandler(configuration);
            var options    = new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            };

            foreach (var marketFile in models)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                var fileName = string.Concat(marketFile.FileName, ".pgp");

                var targetPath = Path.Combine(context.DirectoryEncrypted, fileName);
                var targetInfo = new FileInfo(targetPath);
                if (!targetInfo.Exists)
                {
                    logger.WarnFormat("Unable to FTP file \"{0}\". File does not exist or has been deleted.", fileName);
                    continue;
                }

                try
                {
                    var ftpOutPath = Path.Combine(port.DirectoryOut, fileName);
                    targetInfo.CopyTo(ftpOutPath, true);

                    var ftpOutInfo = new FileInfo(ftpOutPath);
                    if (!ftpOutInfo.Exists)
                    {
                        logger.WarnFormat("Unable to FTP file \"{0}\". File could not be copied to output directory.", fileName);
                        continue;
                    }

                    logger.InfoFormat("Trasmitting file \"{0}\" to FTP Server \"{1}\".",
                                      ftpOutPath, port.FtpRemoteServer);

                    using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        ftpHandler.SendFiles("/custbill", port.DirectoryOut,
                                             name => name.Equals(fileName, StringComparison.OrdinalIgnoreCase));

                        var completedPath = Path.Combine(port.DirectoryOut, @"\Complete\", ftpOutInfo.Name);
                        var completedInfo = new FileInfo(completedPath);
                        if (completedInfo.Exists)
                        {
                            completedInfo.Delete();
                        }

                        ftpOutInfo.MoveTo(completedPath);

                        var encryptedArchivePath = Path.Combine(context.DirectoryArchive, @"\Encrypted\",
                                                                targetInfo.Name);
                        targetInfo.MoveTo(encryptedArchivePath);

                        marketFile.Status      = MarketFileStatusOptions.Transmitted;
                        marketFile.ProcessDate = DateTime.Now;
                        marketDataAccess.UpdateMarketFile(marketFile);

                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat(ex, "Unknown error occurred while transmitting file \"{0}\".", fileName);
                }
            }
        }