85 lines
2.4 KiB
C#
85 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
|
|
namespace ClipForge
|
|
{
|
|
public class ClipLibraryService
|
|
{
|
|
private FileSystemWatcher? _watcher;
|
|
public ObservableCollection<ClipFile> Clips { get; } = new();
|
|
|
|
public string ClipsDirectory { get; } = Path.Combine(
|
|
Environment.GetFolderPath(
|
|
Environment.SpecialFolder.MyVideos), "ClipForge");
|
|
|
|
public void Initialize()
|
|
{
|
|
// Create directory if it doesn't exist
|
|
if (!Directory.Exists(ClipsDirectory))
|
|
Directory.CreateDirectory(ClipsDirectory);
|
|
|
|
// Load existing clips
|
|
LoadExistingClips();
|
|
|
|
// Watch for new clips being saved
|
|
_watcher = new FileSystemWatcher(ClipsDirectory, "*.mp4")
|
|
{
|
|
EnableRaisingEvents = true,
|
|
NotifyFilter = NotifyFilters.FileName
|
|
};
|
|
|
|
_watcher.Created += OnClipCreated;
|
|
_watcher.Deleted += OnClipDeleted;
|
|
}
|
|
|
|
private void LoadExistingClips()
|
|
{
|
|
Clips.Clear();
|
|
var files = Directory.GetFiles(ClipsDirectory, "*.mp4");
|
|
|
|
// Sort newest first
|
|
Array.Sort(files, (a, b) =>
|
|
File.GetCreationTime(b).CompareTo(File.GetCreationTime(a)));
|
|
|
|
foreach (var file in files)
|
|
Clips.Add(ClipFile.FromPath(file));
|
|
}
|
|
|
|
private void OnClipCreated(object sender, FileSystemEventArgs e)
|
|
{
|
|
// Must update UI on main thread
|
|
App.MainQueue?.TryEnqueue(() =>
|
|
{
|
|
Clips.Insert(0, ClipFile.FromPath(e.FullPath));
|
|
});
|
|
}
|
|
|
|
private void OnClipDeleted(object sender, FileSystemEventArgs e)
|
|
{
|
|
App.MainQueue?.TryEnqueue(() =>
|
|
{
|
|
for (int i = Clips.Count - 1; i >= 0; i--)
|
|
{
|
|
if (Clips[i].Path == e.FullPath)
|
|
{
|
|
Clips.RemoveAt(i);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public void DeleteClip(ClipFile clip)
|
|
{
|
|
if (File.Exists(clip.Path))
|
|
File.Delete(clip.Path);
|
|
}
|
|
|
|
public void OpenInExplorer(ClipFile clip)
|
|
{
|
|
System.Diagnostics.Process.Start("explorer.exe",
|
|
$"/select,\"{clip.Path}\"");
|
|
}
|
|
}
|
|
} |