Files
BetterLyrics/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/FileSystemService/Providers/FTPFileSystem.cs
2025-12-25 13:44:43 -05:00

54 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BetterLyrics.WinUI3.Models;
using BetterLyrics.WinUI3.Services.FileSystemService;
using FluentFTP;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace BetterLyrics.WinUI3.Services.FileSystemService.Providers
{
public partial class FTPFileSystem : IUnifiedFileSystem
{
private readonly AsyncFtpClient _client;
private readonly string _rootPath; // 服务器上的根路径 (例如 /pub/music)
public FTPFileSystem(string host, string user, string pass, int port, string remotePath)
{
// 如果 path 是 "192.168.1.5/Music",我们需要把 /Music 拆出来
// 但为了简单,假设 host 仅仅是 IPremotePath 才是路径
_rootPath = remotePath ?? "/";
var config = new FtpConfig { ConnectTimeout = 5000 };
_client = new AsyncFtpClient(host, user ?? "anonymous", pass ?? "", port > 0 ? port : 21, config);
}
public async Task<bool> ConnectAsync()
{
await _client.AutoConnect();
return _client.IsConnected;
}
public async Task<List<UnifiedFileItem>> GetFilesAsync(string relativePath)
{
string targetPath = Path.Combine(_rootPath, relativePath).Replace("\\", "/");
var items = await _client.GetListing(targetPath);
return items.Select(i => new UnifiedFileItem
{
Name = i.Name,
FullPath = i.FullName,
IsFolder = i.Type == FtpObjectType.Directory,
}).ToList();
}
public async Task<Stream> OpenReadAsync(string fullPath)
{
return await _client.OpenRead(fullPath);
}
public async Task DisconnectAsync() => await _client.Disconnect();
public void Dispose() => _client?.Dispose();
}
}