mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-07 07:46:22 +02:00
Add directory watcher functionality
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Timers;
|
||||
|
||||
namespace HyperbolicDownloaderApi.FileProcessing;
|
||||
|
||||
public class DirectoryWatcher
|
||||
{
|
||||
private readonly FilesManager filesManager;
|
||||
|
||||
private readonly List<string> directories = new();
|
||||
private readonly System.Timers.Timer timer = new(1);
|
||||
|
||||
public DirectoryWatcher(FilesManager filesManager)
|
||||
{
|
||||
this.filesManager = filesManager;
|
||||
|
||||
timer.AutoReset = false;
|
||||
timer.Elapsed += Timer_Elapsed;
|
||||
}
|
||||
|
||||
public bool TryAdd(string path, out string? errorMessage)
|
||||
{
|
||||
if (directories.Contains(path))
|
||||
{
|
||||
errorMessage = "Directory already tracked";
|
||||
return false;
|
||||
}
|
||||
|
||||
directories.Add(path);
|
||||
errorMessage = null;
|
||||
|
||||
SaveDirectories();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddRange(IEnumerable<string> directoryInfos)
|
||||
{
|
||||
foreach (string directoryInfo in directoryInfos)
|
||||
{
|
||||
if (Directory.Exists(directoryInfo) && !directories.Contains(directoryInfo))
|
||||
{
|
||||
directories.Add(directoryInfo);
|
||||
}
|
||||
}
|
||||
|
||||
SaveDirectories();
|
||||
}
|
||||
|
||||
public bool TryRemove(string path)
|
||||
{
|
||||
if (!directories.Contains(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_ = directories.Remove(path);
|
||||
|
||||
SaveDirectories();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SaveDirectories()
|
||||
{
|
||||
File.WriteAllText(ApiConfiguration.DirectoriesInfoPath, JsonSerializer.Serialize(directories));
|
||||
}
|
||||
|
||||
public List<string> ToList()
|
||||
{
|
||||
return directories;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine("Checking for new files...", NotificationMessageType.Log);
|
||||
|
||||
timer.Interval = TimeSpan.FromMinutes(1).TotalMilliseconds;
|
||||
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine($"Checking directory: {directory}", NotificationMessageType.Log);
|
||||
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
|
||||
|
||||
foreach (string file in files)
|
||||
{
|
||||
_ = filesManager.TryAdd(file, out _, out _);
|
||||
}
|
||||
}
|
||||
|
||||
ApiManager.SendNotificationMessageNewLine("Finished checking for new files.", NotificationMessageType.Log);
|
||||
|
||||
filesManager.RemoveFilesThatDontExist();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace HyperbolicDownloaderApi.FileProcessing;
|
||||
@@ -31,10 +32,16 @@ public class FilesManager
|
||||
if (Contains(hash))
|
||||
{
|
||||
fileInfo = null;
|
||||
Debug.WriteLine(filePath + "-.-" + hash);
|
||||
errorMessage = "File already tracked!";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (files.Exists(f => f.FilePath == fullPath))
|
||||
{
|
||||
_ = files.RemoveAll(f => f.FilePath == fullPath);
|
||||
}
|
||||
|
||||
fileInfo = new PrivateHyperFileInfo(hash, fullPath);
|
||||
|
||||
files.Add(fileInfo);
|
||||
@@ -91,6 +98,20 @@ public class FilesManager
|
||||
return files.ToList();
|
||||
}
|
||||
|
||||
internal void RemoveFilesThatDontExist()
|
||||
{
|
||||
List<PrivateHyperFileInfo> filesToRemove = files
|
||||
.Where(f => !File.Exists(f.FilePath))
|
||||
.ToList();
|
||||
|
||||
foreach (PrivateHyperFileInfo fileToRemove in filesToRemove)
|
||||
{
|
||||
_ = files.Remove(fileToRemove);
|
||||
}
|
||||
|
||||
SaveFiles();
|
||||
}
|
||||
|
||||
private void SaveFiles()
|
||||
{
|
||||
File.WriteAllText(ApiConfiguration.FilesInfoPath, JsonSerializer.Serialize(files));
|
||||
|
||||
Reference in New Issue
Block a user