mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-08 15:56:23 +02:00
Add directory watcher functionality
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using HyperbolicDownloaderApi.FileProcessing;
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
|
||||
namespace HyperbolicDownloaderApi.Commands;
|
||||
|
||||
public class DirectoryCommands
|
||||
{
|
||||
private readonly DirectoryWatcher directoryWatcher;
|
||||
|
||||
public DirectoryCommands(DirectoryWatcher directoryWatcher)
|
||||
{
|
||||
this.directoryWatcher = directoryWatcher;
|
||||
}
|
||||
|
||||
public void AddDirectory(string directoryPath)
|
||||
{
|
||||
if (directoryWatcher.TryAdd(directoryPath, out string? message))
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine($"Added directory: {directoryPath}", NotificationMessageType.Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine(message!, NotificationMessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveDirectory(string args)
|
||||
{
|
||||
if (int.TryParse(args, out int index))
|
||||
{
|
||||
List<string> fileInfos = directoryWatcher.ToList();
|
||||
|
||||
if (index < 1 || index > fileInfos.Count)
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine("Invalid index!", NotificationMessageType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
args = fileInfos[index - 1];
|
||||
}
|
||||
|
||||
if (directoryWatcher.TryRemove(args))
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine($"Removed directory: {args}", NotificationMessageType.Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine("Directory is not tracked!", NotificationMessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
public void ListDirectories(string searchString)
|
||||
{
|
||||
int index = 0;
|
||||
int directoryCount = 0;
|
||||
List<string> directoryInfos = directoryWatcher.ToList();
|
||||
|
||||
if (directoryInfos.Count == 0)
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine("No tracked directories!", NotificationMessageType.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string directoryInfo in directoryInfos)
|
||||
{
|
||||
index++;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(searchString) && !directoryInfo.Contains(searchString, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
directoryCount++;
|
||||
|
||||
ApiManager.SendNotificationMessageNewLine($"{index}) {directoryInfo}");
|
||||
ApiManager.SendNotificationMessageNewLine(string.Empty);
|
||||
}
|
||||
|
||||
if (directoryCount == 0)
|
||||
{
|
||||
ApiManager.SendNotificationMessage($"No directories found containing \"{searchString}\".", NotificationMessageType.Warning);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.CursorTop--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ public class DownloadCommands
|
||||
if (!File.Exists(fullPath))
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine("Invalid file path!", NotificationMessageType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
string json = File.ReadAllText(fullPath);
|
||||
|
||||
@@ -161,6 +161,8 @@ public class FileCommands
|
||||
return;
|
||||
}
|
||||
|
||||
ApiManager.SendNotificationMessageNewLine($"Requesting file list from {ipAddress}:{port}...");
|
||||
|
||||
Task<List<HyperFileDto>?> sendTask = NetworkClient.SendAsync<List<HyperFileDto>>(ipAddress, port, "GetFilesList", searchString);
|
||||
|
||||
_ = sendTask.Wait(1000);
|
||||
@@ -183,6 +185,8 @@ public class FileCommands
|
||||
return;
|
||||
}
|
||||
|
||||
index = 0;
|
||||
|
||||
foreach (HyperFileDto fileInfo in fileInfos)
|
||||
{
|
||||
index++;
|
||||
@@ -227,7 +231,7 @@ public class FileCommands
|
||||
string fileName = Path.GetFileName(localHyperFileInfo!.FilePath);
|
||||
string filePath = Path.Combine(directoryPath, $"{fileName}.hyper");
|
||||
|
||||
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(args);
|
||||
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(localHyperFileInfo.Hash);
|
||||
|
||||
NetworkSocket? localHost = ApiManager.GetLocalSocket();
|
||||
if (localHost is null)
|
||||
@@ -280,7 +284,7 @@ public class FileCommands
|
||||
string fileName = Path.GetFileName(localHyperFileInfo!.FilePath);
|
||||
string filePath = Path.Combine(directoryPath, $"{fileName}.hyper");
|
||||
|
||||
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(args);
|
||||
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(localHyperFileInfo.Hash);
|
||||
|
||||
NetworkSocket? localHost = ApiManager.GetLocalSocket();
|
||||
if (localHost is null)
|
||||
@@ -303,7 +307,7 @@ public class FileCommands
|
||||
|
||||
Console.CursorLeft = 0;
|
||||
|
||||
Task<bool> sendTask = NetworkClient.SendAsync<bool>(ipAddress!, host.Port, "HasFile", args);
|
||||
Task<bool> sendTask = NetworkClient.SendAsync<bool>(ipAddress!, host.Port, "HasFile", localHyperFileInfo.Hash);
|
||||
|
||||
_ = sendTask.Wait(1000);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user