vpr/ShadowStream/MainWindow.xaml.cs
2025-06-11 19:01:09 +01:00

417 lines
12 KiB
C#

using System.Drawing;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using file_finder__test;
using ShadowStream;
using ShadowStream.LogHelper;
using ShadowStream.Obejeckte;
using System.IO;
using System.Threading;
using ShadowStream.ObjecktForJason;
using TagLib;
using ShadowStream.Modules;
using ShadowStream.ObjecktForJason;
using LibVLCSharp.Shared;
namespace ModuleManager;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
//Quinn and Reda and Yazan
public partial class MainWindow : Window
{
#region no dont cahnge
//adding base components used threw out the code
private static LogHelper loghelper = new LogHelper();
LogWindow log = new LogWindow(loghelper.GetEntries());
private Catagory Muvie = new Catagory("Muvie");
private Catagory Serie = new Catagory("Serie");
private Catagory Music = new Catagory("Music");
private Catagory Photo = new Catagory("Photos");
List<string> suportedVidioFiles = new List<string>();
List<string> suportedMusicFiles = new List<string>();
List<string> suportedPhotoFiles = new List<string>();
//root directory
List<string> dirs = new List<string>();
private int option =2;
int specificOption = 0;
string rootPath = "F:/";
FileScanner fileScanner;
#endregion
#region vlc logic
private LibVLC _libVLC;
private LibVLCSharp.Shared.MediaPlayer _mediaPlayer;
#endregion
//code start
public MainWindow()
{
InitializeComponent();
//Initialise but Hide
log.Hide();
//this.Hide();
#region vlc init
Core.Initialize(); // Important: load native libvlc binaries
_libVLC = new LibVLC();
_mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(_libVLC);
VideoView.MediaPlayer = _mediaPlayer;
#endregion
//Begin Login Process
var login = new LogIn();
login.Show();
#region only exdend. no remuving of code
//adding all extensions... example values added
suportedVidioFiles.Add(".mp4");
suportedVidioFiles.Add(".mkv");
suportedMusicFiles.Add(".wav");
suportedMusicFiles.Add(".mp3");
suportedPhotoFiles.Add(".jpg");
Createscan();
//dirs.Add("C:/");
//execute and wait for task completion
var tmp = fileScanner.ScanAllDrivesAsync();
//load json parallel to finding drives
//add code laiter
tmp.Wait();
foreach (var VARIABLE in tmp.Result)
{
dirs.Add(VARIABLE);
}
#endregion
}
#region dont change
void Createscan()
{
//adding all string arrays to one temp array to do the initial scan of the designated drive
int count = 0;
string[] tmp = new string[suportedMusicFiles.Count + suportedVidioFiles.Count + suportedPhotoFiles.Count];
foreach (var suportedVidioFile in suportedVidioFiles)
{
tmp[count] = suportedVidioFile;
count++;
}
foreach (var suportedMusicFile in suportedMusicFiles)
{
tmp[count] = suportedMusicFile;
count++;
}
foreach (var suportedPhotoFile in suportedPhotoFiles)
{
tmp[count] = suportedPhotoFile;
count++;
}
//initialise the file scanner with all files endings wanted
fileScanner = new FileScanner(tmp);
}
//btn logic
//example logoc to start vidio
/*void Video_Click(object sender, RoutedEventArgs e)
{
PlayVideo(@"F:\Rio.mp4");
}*/
public void PlayVideo(string filePath)
{
VideoView.Visibility = Visibility.Visible;
var media = new Media(_libVLC, filePath, FromType.FromPath);
_mediaPlayer.Play(media);
}
private async void scanButton_Click(object sender, RoutedEventArgs e)
{
try
{
MessageBox.Show("scanning...");
List<string> tmp = new List<string>();
switch (option)
{
case 0:
foreach (var VARIABLE in dirs)
{
foreach (var tmp2 in await fileScanner.ScanDriveParallel(VARIABLE))
{
tmp.Add(tmp2);
}
}
break;
case 1:
foreach (var VARIABLE in await fileScanner.ScanDriveParallel(dirs[specificOption]))
{
tmp.Add(VARIABLE);
}
break;
case 2:
foreach (var VARIABLE in await fileScanner.ScanDriveParallel(rootPath))
{
tmp.Add(VARIABLE);
}
break;
}
Console.WriteLine($"Total scanned files: {tmp.Count}");
var classifier = new FileClassifier();
var (musicFiles, videoFiles, photoFiles) = await classifier.ClassifyFilesAsync(tmp, suportedMusicFiles, suportedVidioFiles, suportedPhotoFiles);
var separator = new VideoSeparator();
var (series, movies) = await separator.SeparateVideosAsync(videoFiles);
Console.WriteLine($"musicFiles count: {musicFiles.Count}");
Console.WriteLine($"videoFiles count: {videoFiles.Count}");
Console.WriteLine($"photoFiles count: {photoFiles.Count}");
Console.WriteLine($"series count: {series.Count}");
Console.WriteLine($"movies count: {movies.Count}");
videoFiles = null;
MessageBox.Show("files sorted");
// Prepare JSON lists
List<locJason> muviesJS = new List<locJason>();
List<locJason> seriesJS = new List<locJason>();
List<locJason> photosJS = new List<locJason>();
List<locJason> mucicJS = new List<locJason>();
JasonToString jasonToString = new JasonToString();
foreach (var VARIABLE in ItemCreater(movies, "Muvie", false))
{
Muvie.addItem(VARIABLE);
muviesJS.Add(new locJason
{
path = VARIABLE.getLink(),
imageData = jasonToString.BitmapToBase64String(BitmapImageToBitmap(VARIABLE.getImage())),
type = VARIABLE.getType()
});
}
foreach (var VARIABLE in ItemCreater(series, "Serie", false))
{
Serie.addItem(VARIABLE);
seriesJS.Add(new locJason
{
path = VARIABLE.getLink(),
imageData = jasonToString.BitmapToBase64String(BitmapImageToBitmap(VARIABLE.getImage())),
type = VARIABLE.getType()
});
}
foreach (var VARIABLE in ItemCreater(photoFiles, "Photo", true))
{
Photo.addItem(VARIABLE);
photosJS.Add(new locJason
{
path = VARIABLE.getLink(),
imageData = jasonToString.BitmapToBase64String(BitmapImageToBitmap(VARIABLE.getImage())),
type = VARIABLE.getType()
});
}
foreach (var VARIABLE in ItemCreater(musicFiles, "Music", false, true))
{
Music.addItem(VARIABLE);
mucicJS.Add(new locJason
{
path = VARIABLE.getLink(),
imageData = jasonToString.BitmapToBase64String(BitmapImageToBitmap(VARIABLE.getImage())),
type = VARIABLE.getType()
});
}
MessageBox.Show("scan finished");
Jason_Writer jason_Writer = new Jason_Writer();
List<Task> tasks = new List<Task>
{
jason_Writer.SaveList("Muvies", muviesJS),
jason_Writer.SaveList("Series", seriesJS),
jason_Writer.SaveList("Photos", photosJS),
jason_Writer.SaveList("Music", mucicJS)
};
await Task.WhenAll(tasks);
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred during scanning:\n\n{ex.ToString()}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Console.WriteLine($"An error occurred during scanning:\n\n{ex.ToString()}", "Error");
}
}
List<Item> ItemCreater(List<string> path, string type,bool isFoto)
{
List<Item> items = new List<Item>();
foreach (var VARIABLE in path)
{
BitmapImage frame200;
if(!isFoto)
frame200 = VideoFrameExtractor.GetFrame200(VARIABLE);
else
{
frame200 = new BitmapImage(new Uri(VARIABLE, UriKind.Absolute));
}
items.Add(new Item(VARIABLE, type,frame200, isFoto));
}
return items;
}
List<Item> ItemCreater(List<string> paths, string type, bool isFoto, bool Music)
{
List<Item> items = new List<Item>();
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string imagePath = System.IO.Path.Combine(baseDir, "Pics", "MusicD.jpeg");
BitmapImage defaultImage;
if (System.IO.File.Exists(imagePath))
{
defaultImage = new BitmapImage(new Uri(imagePath));
defaultImage.Freeze();
}
else
{
MessageBox.Show($"Default image not found at: {imagePath}");
// Create a blank 100x100 transparent BitmapImage fallback
int width = 100;
int height = 100;
int dpi = 96;
var pixelFormat = System.Windows.Media.PixelFormats.Pbgra32;
int rawStride = (width * pixelFormat.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height]; // all zero = transparent
var bitmap = BitmapSource.Create(width, height, dpi, dpi, pixelFormat, null, rawImage, rawStride);
bitmap.Freeze();
defaultImage = new BitmapImage();
// Can't cast BitmapSource directly to BitmapImage,
// so let's use BitmapSource type instead in Item if possible
// Otherwise, fallback to null or a resource image.
// For now, assign null or handle in your Item class.
defaultImage = null;
}
foreach (var filePath in paths)
{
BitmapImage bitmapImage = defaultImage;
try
{
var file = TagLib.File.Create(filePath);
if (file.Tag.Pictures.Length > 0)
{
var pic = file.Tag.Pictures[0];
using (var ms = new MemoryStream(pic.Data.Data))
{
var img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.StreamSource = ms;
img.EndInit();
img.Freeze();
bitmapImage = img;
}
}
}
catch
{
// keep defaultImage if anything fails
}
items.Add(new Item(filePath, type, bitmapImage, isFoto));
}
return items;
}
public static BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
using (var memory = new System.IO.MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze(); // Optional: for thread safety
return bitmapImage;
}
}
public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
using (MemoryStream ms = new MemoryStream())
{
// Encode BitmapImage to stream (e.g., PNG)
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin); // Reset stream position
// Create Bitmap from stream
return new Bitmap(ms);
}
}
//scan logic
#endregion
}