Exemple #1
0
        public async Task SaveFileAsync(Filesystem.File file, CancellationToken cancellationToken)
        {
            this.logger.Info("Uploading file: {value1}", file.FullPath(Separator));

            var directories = new List <Directory>();
            var currentDir  = file.Directory;

            while (currentDir != null)
            {
                directories.Add(currentDir);
                currentDir = currentDir.Directory;
            }

            directories.Reverse();

            foreach (var directory in directories)
            {
                var createDirectoryRequest = this.CreateRequest(directory.FullPath((Separator)));
                createDirectoryRequest.Method     = WebRequestMethods.Ftp.MakeDirectory;
                using var createDirectoryRegister = cancellationToken.Register(() => createDirectoryRequest.Abort());
                try
                {
                    using var createDirectoryResponse = await createDirectoryRequest.GetResponseAsync();
                }
                catch (WebException ex)
                {
                    if (ex.Response is FtpWebResponse makeDirectoryResponse)
                    {
                        // If File is unavailable the folder already exists
                        if (makeDirectoryResponse.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable)
                        {
                            this.logger.Error(ex);
                        }
                    }
                    else
                    {
                        this.logger.Error(ex);
                    }
                }
            }

            var request = this.CreateRequest(file.FullPath(Separator));

            request.Method = WebRequestMethods.Ftp.UploadFile;
            using var cancellationRegister = cancellationToken.Register(() => request.Abort());

            await using var stream = await request.GetRequestStreamAsync();

            await using var fileStream = await file.GetStreamAsync(cancellationToken);

            await fileStream.CopyToAsync(stream, cancellationToken);

            using var response = await request.GetResponseAsync();

            this.logger.Info("Uploaded file: {value1}", file.FullPath(Separator));
        }
Exemple #2
0
        public async Task <Filesystem.File> GetFileAsync(Filesystem.File file, CancellationToken cancellationToken)
        {
            this.logger.Info("Downloading File: {value1}", file.FullPath(Separator));
            var request = this.CreateRequest(file.FullPath(Separator));

            request.Method = WebRequestMethods.Ftp.DownloadFile;
            using var cancellationRegister = cancellationToken.Register(() => request.Abort());

            using var response = await request.GetResponseAsync();

            await using var responseStream = response.GetResponseStream();

            if (responseStream != null)
            {
                System.IO.Directory.CreateDirectory(this.tempFolder ?? throw new InvalidOperationException());

                async Task <Stream> getStream(CancellationToken cancellationToken)
                => await Task.FromResult(new FileStream(Path.Combine(this.tempFolder, file.Name), FileMode.OpenOrCreate));

                void onDispose()
                {
                    if (File.Exists(Path.Combine(this.tempFolder, file.Name)))
                    {
                        File.Delete(Path.Combine(this.tempFolder, file.Name));
                    }
                }

                using (var stream = await getStream(cancellationToken))
                {
                    await responseStream.CopyToAsync(stream, cancellationToken);
                }

                this.logger.Info("Downloaded File: {value1}", file.FullPath());
                return(file.ReplaceContent(getStream, onDispose));
            }

            return(null);
        }