Exemplo n.º 1
0
 private void ChangeFileExtensions(TransferSettingsOptions transfer, string localFilePath)
 {
     foreach (var item in transfer.ChangeExtensions)
     {
         if (item.Source.Equals(Path.GetExtension(localFilePath).TrimStart('.').ToLower()))
         {
             var targetFileName = @$ "{transfer.LocalPath}\{Path.GetFileNameWithoutExtension(localFilePath)}.{item.Target}";
Exemplo n.º 2
0
        private async Task <bool> DownloadFileFromSourceAsync(TransferSettingsOptions transfer)
        {
            var token = new CancellationToken();

            using (var ftp = CreateFtpClient(transfer.Source))
            {
                ftp.OnLogEvent += Log;
                await ftp.ConnectAsync(token);

                var overwriteExisting = transfer.Source.OverwriteExisting ? FtpLocalExists.Overwrite : FtpLocalExists.Skip;

                var result = await ftp.DownloadFileAsync(transfer.LocalPath, transfer.Source.RemotePath, overwriteExisting);

                if (transfer.Source.DeleteOnceTransferred)
                {
                    if (result.IsSuccess())
                    {
                        try
                        {
                            await ftp.DeleteFileAsync(transfer.Source.RemotePath, token);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogWarning("Error deleting {RemotePath}: {Message}", transfer.Source.RemotePath, ex.Message);
                        }
                    }
                }

                return(result.IsSuccess());
            }
        }
Exemplo n.º 3
0
 private void ChangeFileExtensions(TransferSettingsOptions transfer, List <FtpResult> results)
 {
     foreach (var file in results.Where(x => x.Type == FtpFileSystemObjectType.File).Where(x => x.IsSuccess))
     {
         ChangeFileExtensions(transfer, file.LocalPath);
     }
 }
Exemplo n.º 4
0
        private async Task <List <FtpResult> > DownloadDirectoryFromSourceAsync(TransferSettingsOptions transfer)
        {
            var token = new CancellationToken();

            using (var ftp = CreateFtpClient(transfer.Source))
            {
                ftp.OnLogEvent += Log;
                await ftp.ConnectAsync(token);

                var rules = new List <FtpRule>();

                if (transfer.Source.FileTypesToTransfer.Count > 0)
                {
                    rules.Add(new FtpFileExtensionRule(true, transfer.Source.FileTypesToTransfer));
                }

                var overwriteExisting = transfer.Source.OverwriteExisting ? FtpLocalExists.Overwrite : FtpLocalExists.Skip;

                var results = await ftp.DownloadDirectoryAsync(transfer.LocalPath, transfer.Source.RemotePath, transfer.Source.FolderSyncMode,
                                                               overwriteExisting, FtpVerify.None, rules);

                await ValidateFtpResultList(results, ftp, transfer.Source.DeleteOnceTransferred);

                return(results);
            }
        }
Exemplo n.º 5
0
        private async Task RunSyncDirsAsync(TransferSettingsOptions transfer)
        {
            List <FtpResult> transferResults = await DownloadDirectoryFromSourceAsync(transfer);

            ChangeFileExtensions(transfer, transferResults);

            await UploadDirectoryToDestinationAsync(transfer);
        }
        public RunMode RetrieveRunMode(TransferSettingsOptions options)
        {
            bool sourceIsNull      = options.Source is null;
            bool destinationIsNull = options.Destination is null;

            // If LocalPathIsFile, mode can only be UploadFile
            if (options.LocalPathIsFile)
            {
                _logger.LogDebug("Local Path: {LocalPath} is file, RunMode determined as UploadFile", options.LocalPath);
                options.Destination.RemotePath = options.Destination.RemotePath.EnsurePathIsFile(options.LocalPath);
                return(RunMode.UploadFile);
            }

            // If both source & destination are defined in settings
            if (!sourceIsNull && !destinationIsNull)
            {
                // and the remote source is a file, mode must be SyncFile
                if (options.Source.RemotePathIsFile)
                {
                    _logger.LogDebug("Source & Destination defined, Source.RemotePath: {RemotePath} is file, RunMode determined as SyncFile", options.Source.RemotePath);
                    options.Destination.RemotePath = options.Destination.RemotePath.EnsurePathIsFile(options.Source.RemotePath);
                    options.LocalPath = options.LocalPath.EnsurePathIsFile(options.Source.RemotePath);
                    return(RunMode.SyncFile);
                }
                else
                {
                    if (options.Destination.RemotePathIsFile)
                    {
                        throw new Exception("Destination RemotePath cannot be a file if Source RemotePath is not a file");
                    }
                    _logger.LogDebug("Source & Destination defined, Source.RemotePath: {RemotePath} is directory, RunMode determined as SyncDirs", options.Source.RemotePath);
                    return(RunMode.SyncDirs);
                }
            }

            // if no source defined, and destination defined, already checked if localpathisfile, so mode must be UploadDir
            if (sourceIsNull && !destinationIsNull)
            {
                _logger.LogDebug("Only Destination defined, Destination.RemotePath: {RemotePath} is directory, RunMode determined as UploadDir", options.Destination.RemotePath);
                return(RunMode.UploadDir);
            }
            // Otherwise, all that's left is to check source
            else
            {
                if (options.Source.RemotePathIsFile)
                {
                    _logger.LogDebug("Only Source defined, Source.RemotePath: {RemotePath} is file, RunMode determined as DownloadFile", options.Source.RemotePath);
                    options.LocalPath = options.LocalPath.EnsurePathIsFile(options.Source.RemotePath);
                    return(RunMode.DownloadFile);
                }
                else
                {
                    _logger.LogDebug("Only Source defined, Source.RemotePath: {RemotePath} is directory, RunMode determined as DownloadDir", options.Source.RemotePath);
                    return(RunMode.DownloadDir);
                }
            }
        }
Exemplo n.º 7
0
        public async Task RunTransferAsync(TransferSettingsOptions transferOptions)
        {
            if (!transferOptions.LocalPathIsFile)
            {
                Directory.CreateDirectory(transferOptions.LocalPath);
            }

            try
            {
                switch (transferOptions.RunMode)
                {
                case RunMode.DownloadDir:
                    await DownloadDirectoryFromSourceAsync(transferOptions);

                    break;

                case RunMode.DownloadFile:
                    await DownloadFileFromSourceAsync(transferOptions);

                    break;

                case RunMode.UploadDir:
                    await UploadDirectoryToDestinationAsync(transferOptions);

                    break;

                case RunMode.UploadFile:
                    await UploadFileToDestinationAsync(transferOptions);

                    break;

                case RunMode.SyncDirs:
                    await RunSyncDirsAsync(transferOptions);

                    break;

                case RunMode.SyncFile:
                    await RunSyncFileAsync(transferOptions);

                    break;

                default:
                    break;
                }
                ;
            }
            catch (Exception ex)
            {
                _logger.LogError("Exception: {Message}", ex.Message);
                _logger.LogDebug("Stack Trace: {StackTrace}", ex.StackTrace);
                if (ex.InnerException is not null)
                {
                    _logger.LogDebug("Inner exception: {InnerException}", ex.InnerException);
                }
            }
        }
Exemplo n.º 8
0
        private async Task RunSyncFileAsync(TransferSettingsOptions transfer)
        {
            bool isSuccess = await DownloadFileFromSourceAsync(transfer);

            if (isSuccess)
            {
                ChangeFileExtensions(transfer, transfer.LocalPath);
            }

            await UploadFileToDestinationAsync(transfer);
        }