Initial commit
This commit is contained in:
145
Valheim Dashboard/FtpClient.cs
Normal file
145
Valheim Dashboard/FtpClient.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Reactive;
|
||||
using System.Threading.Tasks;
|
||||
using ReactiveUI;
|
||||
using Valheim_Dashboard.ViewModels;
|
||||
|
||||
namespace Valheim_Dashboard
|
||||
{
|
||||
|
||||
public class FtpItem
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public bool IsDirectory { get; set; }
|
||||
}
|
||||
public class FtpClient
|
||||
{
|
||||
private readonly string _ftpServer;
|
||||
private readonly string _username;
|
||||
private readonly string _password;
|
||||
|
||||
public FtpClient(string ftpServer, string username, string password)
|
||||
{
|
||||
_ftpServer = ftpServer.EndsWith("/") ? ftpServer : ftpServer + "/";
|
||||
_username = username;
|
||||
_password = password;
|
||||
FolderClickedCommand = ViewModel.FolderClickedCommand;
|
||||
}
|
||||
private FtpViewModel ViewModel;
|
||||
|
||||
|
||||
|
||||
|
||||
public ReactiveCommand<FtpItem, Unit> FolderClickedCommand { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Listet die Inhalte eines Ordners (Dateien und Unterordner) auf (Async-Version).
|
||||
/// </summary>
|
||||
|
||||
|
||||
public async Task<List<FtpItem>> ListDirectoryDetailsAsync(string path = "")
|
||||
{
|
||||
var request = CreateFtpWebRequest(path, WebRequestMethods.Ftp.ListDirectoryDetails);
|
||||
|
||||
using var response = (FtpWebResponse)await request.GetResponseAsync();
|
||||
using var reader = new StreamReader(response.GetResponseStream());
|
||||
|
||||
var result = new List<FtpItem>();
|
||||
string line;
|
||||
while ((line = await reader.ReadLineAsync()) != null)
|
||||
{
|
||||
// Beispiel: In Unix-Listings beginnt eine Zeile mit "d", wenn es ein Verzeichnis ist.
|
||||
bool isDirectory = line.StartsWith("d");
|
||||
|
||||
// Der Dateiname befindet sich üblicherweise am Ende der Zeile.
|
||||
// Dies kann je nach FTP-Server variieren.
|
||||
string[] tokens = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string fileName = tokens[tokens.Length - 1];
|
||||
|
||||
result.Add(new FtpItem { Name = fileName, IsDirectory = isDirectory });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Erstellt einen Ordner auf dem FTP-Server (Async).
|
||||
/// </summary>
|
||||
public async Task CreateDirectoryAsync(string path)
|
||||
{
|
||||
var request = CreateFtpWebRequest(path, WebRequestMethods.Ftp.MakeDirectory);
|
||||
using var response = (FtpWebResponse)await request.GetResponseAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lädt eine Datei vom FTP-Server herunter (Async).
|
||||
/// </summary>
|
||||
public async Task DownloadFileAsync(string remoteFilePath, string localFilePath)
|
||||
{
|
||||
var request = CreateFtpWebRequest(remoteFilePath, WebRequestMethods.Ftp.DownloadFile);
|
||||
|
||||
using var response = (FtpWebResponse)await request.GetResponseAsync();
|
||||
using var responseStream = response.GetResponseStream();
|
||||
using var fileStream = new FileStream(localFilePath, FileMode.Create);
|
||||
|
||||
await responseStream.CopyToAsync(fileStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lädt eine Datei auf den FTP-Server hoch (Async).
|
||||
/// </summary>
|
||||
public async Task UploadFileAsync(string localFilePath, string remoteFilePath)
|
||||
{
|
||||
var request = CreateFtpWebRequest(remoteFilePath, WebRequestMethods.Ftp.UploadFile);
|
||||
request.ContentLength = new FileInfo(localFilePath).Length;
|
||||
|
||||
using var requestStream = await request.GetRequestStreamAsync();
|
||||
using var fileStream = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
|
||||
|
||||
await fileStream.CopyToAsync(requestStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Löscht eine Datei vom FTP-Server (Async).
|
||||
/// </summary>
|
||||
public async Task DeleteFileAsync(string remoteFilePath)
|
||||
{
|
||||
var request = CreateFtpWebRequest(remoteFilePath, WebRequestMethods.Ftp.DeleteFile);
|
||||
using var response = (FtpWebResponse)await request.GetResponseAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Löscht einen Ordner vom FTP-Server (Async).
|
||||
/// </summary>
|
||||
public async Task DeleteDirectoryAsync(string remoteDirectoryPath)
|
||||
{
|
||||
var request = CreateFtpWebRequest(remoteDirectoryPath, WebRequestMethods.Ftp.RemoveDirectory);
|
||||
using var response = (FtpWebResponse)await request.GetResponseAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hilfsmethode zum Erstellen eines FtpWebRequest.
|
||||
/// </summary>
|
||||
private FtpWebRequest CreateFtpWebRequest(string path, string method)
|
||||
{
|
||||
var uri = new Uri(_ftpServer + path);
|
||||
var request = (FtpWebRequest)WebRequest.Create(uri);
|
||||
request.Method = method;
|
||||
request.Credentials = new NetworkCredential(_username, _password);
|
||||
request.UseBinary = true;
|
||||
request.UsePassive = true;
|
||||
request.KeepAlive = false;
|
||||
return request;
|
||||
}
|
||||
|
||||
public async Task<List<FtpItem>> OpenFolderAsync(string folderPath)
|
||||
{
|
||||
return await ListDirectoryDetailsAsync(folderPath);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user