示例#1
0
        public override IAsyncOperation <BaseStorageFile> CreateFileAsync(string desiredName, CreationCollisionOption options)
        {
            return(AsyncInfo.Run <BaseStorageFile>(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                using var stream = new MemoryStream();
                var result = await ftpClient.UploadAsync(stream, $"{FtpPath}/{desiredName}", options == CreationCollisionOption.ReplaceExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip);

                if (result == FtpStatus.Success)
                {
                    return new FtpStorageFile(new StorageFileWithPath(null, $"{Path}/{desiredName}"));
                }

                if (result == FtpStatus.Skipped)
                {
                    if (options == CreationCollisionOption.FailIfExists)
                    {
                        throw new IOException("File already exists.");
                    }

                    return null;
                }

                throw new IOException($"Failed to create file {FtpPath}/{desiredName}.");
            }));
        }
示例#2
0
        public override IAsyncOperation <BaseStorageFolder> CreateFolderAsync(string desiredName, CreationCollisionOption options)
        {
            return(AsyncInfo.Run <BaseStorageFolder>(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    throw new IOException($"Failed to connect to FTP server.");
                }

                if (ftpClient.DirectoryExists($"{FtpPath}/{desiredName}"))
                {
                    return new FtpStorageFolder(new StorageFileWithPath(null, $"{Path}/{desiredName}"));
                }

                if (!await ftpClient.CreateDirectoryAsync($"{FtpPath}/{desiredName}",
                                                          options == CreationCollisionOption.ReplaceExisting,
                                                          cancellationToken))
                {
                    throw new IOException($"Failed to create folder {desiredName}.");
                }

                return new FtpStorageFolder(new StorageFileWithPath(null, $"{Path}/{desiredName}"));
            }));
        }
示例#3
0
        public override IAsyncOperation <IReadOnlyList <IStorageItem> > GetItemsAsync()
        {
            return(AsyncInfo.Run(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                var items = new List <IStorageItem>();
                var list = await ftpClient.GetListingAsync(FtpPath);
                foreach (var item in list)
                {
                    if (item.Type == FtpFileSystemObjectType.File)
                    {
                        items.Add(new FtpStorageFile(Path, item));
                    }
                    else if (item.Type == FtpFileSystemObjectType.Directory)
                    {
                        items.Add(new FtpStorageFolder(Path, item));
                    }
                }
                ;
                return (IReadOnlyList <IStorageItem>)items;
            }));
        }
示例#4
0
        public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption option)
        {
            return(AsyncInfo.Run(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return;
                }

                if (!await ftpClient.MoveDirectoryAsync(FtpPath,
                                                        $"{PathNormalization.GetParentDir(FtpPath)}/{desiredName}",
                                                        option == NameCollisionOption.ReplaceExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip,
                                                        cancellationToken))
                {
                    if (option == NameCollisionOption.GenerateUniqueName)
                    {
                        // TODO: handle name generation
                    }
                }
            }));
        }
示例#5
0
        private async void FtpDataStreamingHandler(StreamedFileDataRequest request)
        {
            try
            {
                using var ftpClient   = new FtpClient();
                ftpClient.Host        = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port        = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    request.FailAndClose(StreamedFileFailureMode.CurrentlyUnavailable);
                    return;
                }

                using (var outStream = request.AsStreamForWrite())
                {
                    await ftpClient.DownloadAsync(outStream, FtpPath);

                    await outStream.FlushAsync();
                }
                request.Dispose();
            }
            catch
            {
                request.FailAndClose(StreamedFileFailureMode.Incomplete);
            }
        }
示例#6
0
        public override IAsyncOperation <IStorageItem> GetItemAsync(string name)
        {
            return(AsyncInfo.Run <IStorageItem>(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                var item = await ftpClient.GetObjectInfoAsync(FtpHelpers.GetFtpPath(PathNormalization.Combine(Path, name)));
                if (item != null)
                {
                    if (item.Type == FtpFileSystemObjectType.File)
                    {
                        return new FtpStorageFile(Path, item);
                    }
                    else if (item.Type == FtpFileSystemObjectType.Directory)
                    {
                        return new FtpStorageFolder(Path, item);
                    }
                }
                return null;
            }));
        }
示例#7
0
        public override IAsyncOperation <BaseStorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option)
        {
            return(AsyncInfo.Run(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                BaseStorageFolder destFolder = destinationFolder.AsBaseStorageFolder();
                BaseStorageFile file = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
                var stream = await file.OpenStreamForWriteAsync();

                if (await ftpClient.DownloadAsync(stream, FtpPath, token: cancellationToken))
                {
                    return file;
                }

                return null;
            }));
        }
示例#8
0
        public override IAsyncOperation <IRandomAccessStream> OpenAsync(FileAccessMode accessMode)
        {
            return(AsyncInfo.Run <IRandomAccessStream>(async(cancellationToken) =>
            {
                var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                if (accessMode == FileAccessMode.Read)
                {
                    var inStream = await ftpClient.OpenReadAsync(FtpPath, cancellationToken);
                    return new NonSeekableRandomAccessStreamForRead(inStream, (ulong)inStream.Length)
                    {
                        DisposeCallback = () => ftpClient.Dispose()
                    };
                }
                else
                {
                    return new NonSeekableRandomAccessStreamForWrite(await ftpClient.OpenWriteAsync(FtpPath, cancellationToken))
                    {
                        DisposeCallback = () => ftpClient.Dispose()
                    };
                }
            }));
        }
示例#9
0
        private FtpClient GetFtpClient()
        {
            string host        = FtpHelpers.GetFtpHost(Path);
            ushort port        = FtpHelpers.GetFtpPort(Path);
            var    credentials = FtpManager.Credentials.Get(host, FtpManager.Anonymous);

            return(new(host, port, credentials));
        }
示例#10
0
        public override IAsyncAction DeleteAsync()
        {
            return(AsyncInfo.Run(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return;
                }

                await ftpClient.DeleteDirectoryAsync(FtpPath, cancellationToken);
            }));
        }
示例#11
0
        public override IAsyncOperation <IInputStream> OpenSequentialReadAsync()
        {
            return(AsyncInfo.Run <IInputStream>(async(cancellationToken) =>
            {
                var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                var inStream = await ftpClient.OpenReadAsync(FtpPath, cancellationToken);
                return new InputStreamWithDisposeCallback(inStream)
                {
                    DisposeCallback = () => ftpClient.Dispose()
                };
            }));
        }
示例#12
0
        public override IAsyncOperation <BaseBasicProperties> GetBasicPropertiesAsync()
        {
            return(AsyncInfo.Run <BaseBasicProperties>(async(cancellationToken) =>
            {
                using var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return new BaseBasicProperties();
                }

                var item = await ftpClient.GetObjectInfoAsync(FtpPath);
                if (item != null)
                {
                    return new FtpFolderBasicProperties(item);
                }
                return new BaseBasicProperties();
            }));
        }
示例#13
0
        public override IAsyncOperation <IRandomAccessStreamWithContentType> OpenReadAsync()
        {
            return(AsyncInfo.Run <IRandomAccessStreamWithContentType>(async(cancellationToken) =>
            {
                var ftpClient = new FtpClient();
                ftpClient.Host = FtpHelpers.GetFtpHost(Path);
                ftpClient.Port = FtpHelpers.GetFtpPort(Path);
                ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);

                if (!await ftpClient.EnsureConnectedAsync())
                {
                    return null;
                }

                var inStream = await ftpClient.OpenReadAsync(FtpPath, cancellationToken);
                var nsStream = new NonSeekableRandomAccessStreamForRead(inStream, (ulong)inStream.Length)
                {
                    DisposeCallback = () => ftpClient.Dispose()
                };
                return new StreamWithContentType(nsStream);
            }));
        }