Beispiel #1
0
        private async Task <Message> ReadAsync(string path, string fileName)
        {
            var stream   = new MemoryStream();
            var fullPath = Path.Combine(path, fileName).Replace('\\', '/');

            using (var ftpClient = new Ftp())
            {
                ftpClient.Passive      = _passive;
                ftpClient.TransferType = _transferType;

                await ftpClient.ConnectAsync(_serverAddress, _port, SslMode.None);

                await ftpClient.LoginAsync(_userName, _password);

                await ftpClient.GetFileAsync(fullPath, stream);

                await ftpClient.DisconnectAsync();
            }

            // Rewind the stream:
            stream.Position = 0;

            return(new Message {
                ContentStream = stream
            });
        }
Beispiel #2
0
        public async Task <List <ReceiveFileResult> > ReceiveFilesAsync(string path, string fileMask, bool receiveMultiple = true)
        {
            var handledFiles = new List <ReceiveFileResult>();

            using (var client = new Ftp())
            {
                client.Passive      = _passive;
                client.TransferType = _transferType;

                await client.ConnectAsync(_serverAddress, _port, SslMode.None);

                await client.LoginAsync(_userName, _password);

                // Get files from the specified path using the given file name mask:
                var items = await client.GetListAsync(path);

                var files = items.GetFiles(fileMask);

                // Ignore files that are already transfering / failed and sort the remaining by filename
                var sortedFiles = files.Where(f => !(f.StartsWith(".") && f.EndsWith(".tmp"))).OrderBy(f => f).ToList();

                if (!receiveMultiple && (sortedFiles.Count > 1))
                {
                    // only process the first file
                    var singleFile = sortedFiles.First();
                    sortedFiles = new List <string> {
                        singleFile
                    };
                }

                // Try to rename the files with temporary names:
                foreach (var file in sortedFiles)
                {
                    var result = new ReceiveFileResult
                    {
                        FileName     = file,
                        TempFileName = $".{Guid.NewGuid()}.tmp",
                        Path         = path,
                        IsError      = false
                    };

                    try
                    {
                        await client.RenameAsync(Path.Combine(path, result.FileName).Replace('\\', '/'), Path.Combine(path, result.TempFileName).Replace('\\', '/'));
                    }
                    catch (Exception ex)
                    {
                        result.IsError = true;
                        result.Message = $"Renaming the file '{result.FileName}' to '{result.TempFileName}' failed: {ex.Message}.";
                    }

                    handledFiles.Add(result);
                }

                await client.DisconnectAsync();
            }

            return(handledFiles);
        }
Beispiel #3
0
        private async Task MoveAsync(string path, string fileName, string newPathAndFileName)
        {
            var oldPathAndFileName = Path.Combine(path, fileName).Replace('\\', '/');
            var newFullPath        = newPathAndFileName.Replace('\\', '/');

            using (var ftpClient = new Ftp())
            {
                ftpClient.Passive      = _passive;
                ftpClient.TransferType = _transferType;

                await ftpClient.ConnectAsync(_serverAddress, _port, SslMode.None);

                await ftpClient.LoginAsync(_userName, _password);

                var dir = Path.GetDirectoryName(newFullPath)?.Replace('\\', '/');

                // If the directory hasn't been specified, use user home directory
                if (!string.IsNullOrEmpty(dir) && !await ftpClient.DirectoryExistsAsync(dir))
                {
                    // Examine the path step by step and create directories:
                    StringBuilder directory = null;
                    foreach (var directoryPart in dir.Split('/'))
                    {
                        // First directory should not be preceded by '/':
                        if (directory == null)
                        {
                            directory = new StringBuilder();
                            directory.Append(directoryPart);
                        }
                        else
                        {
                            directory.AppendFormat("/{0}", directoryPart);
                        }

                        // If this directory does not exist, create it and move to the next part:
                        var dirString = directory.ToString();
                        if (!string.IsNullOrWhiteSpace(dirString) && !await ftpClient.DirectoryExistsAsync(dirString))
                        {
                            await ftpClient.CreateDirectoryAsync(dirString);
                        }
                    }
                }

                if (await ftpClient.FileExistsAsync(newFullPath))
                {
                    await ftpClient.DeleteFileAsync(newFullPath);
                }

                await ftpClient.RenameAsync(oldPathAndFileName, newFullPath);

                await ftpClient.DisconnectAsync();
            }
        }
Beispiel #4
0
        public async Task DeleteFileAsync(string path, string fileName)
        {
            var fullPath = Path.Combine(path, fileName).Replace('\\', '/');

            using (var ftpClient = new Ftp())
            {
                ftpClient.Passive      = _passive;
                ftpClient.TransferType = _transferType;

                await ftpClient.ConnectAsync(_serverAddress, _port, SslMode.None);

                await ftpClient.LoginAsync(_userName, _password);

                await ftpClient.DeleteAsync(fullPath, Rebex.IO.TraversalMode.MatchFilesShallow);

                await ftpClient.DisconnectAsync();
            }
        }
Beispiel #5
0
        public async Task <string> WriteFileAsync(string path, string fileName, Stream messageStream, bool overwriteIfExists = false, bool createDirectory = false)
        {
            var tempFileName        = $".{Guid.NewGuid()}.tmp";
            var destinationFilePath = Path.Combine(path, FileNameHelper.PopulateFileNameMacros(fileName)).Replace('\\', '/');
            var tempFilePath        = Path.Combine(path, tempFileName).Replace('\\', '/');

            path = path.Replace('\\', '/');

            using (var ftpClient = new Ftp())
            {
                ftpClient.Passive      = _passive;
                ftpClient.TransferType = _transferType;

                // Connect & authenticate:
                await ftpClient.ConnectAsync(_serverAddress, _port, SslMode.None);

                await ftpClient.LoginAsync(_userName, _password);

                // If the directory does not exist, create it if allowed:
                if (!await ftpClient.DirectoryExistsAsync(path))
                {
                    if (createDirectory)
                    {
                        // Examine the path step by step and create directories:
                        StringBuilder directory = null;
                        foreach (var directoryPart in path.Split('/'))
                        {
                            // First directory should not be preceded by '/':
                            if (directory == null)
                            {
                                directory = new StringBuilder();
                                directory.Append(directoryPart);
                            }
                            else
                            {
                                directory.AppendFormat("/{0}", directoryPart);
                            }

                            // If this directory does not exist, create it and move to the next part:
                            var dirString = directory.ToString();
                            if (!string.IsNullOrWhiteSpace(dirString) && !await ftpClient.DirectoryExistsAsync(dirString))
                            {
                                await ftpClient.CreateDirectoryAsync(dirString);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception($"Directory '{path}' was not found.");
                    }
                }

                // Overwrite existing files if allowed:
                if (await ftpClient.FileExistsAsync(destinationFilePath))
                {
                    if (overwriteIfExists)
                    {
                        await ftpClient.DeleteFileAsync(destinationFilePath);
                    }
                    else
                    {
                        throw new Exception($"File '{destinationFilePath}' already exists.");
                    }
                }

                // Upload the file with a temporary file name:
                await ftpClient.PutFileAsync(messageStream, tempFilePath);

                await ftpClient.RenameAsync(tempFilePath, destinationFilePath);

                await ftpClient.DisconnectAsync();
            }

            return(tempFileName);
        }