applying new struckture and working vlc
This commit is contained in:
11
file finder test/Modules/DataBaseModules/DataBase.cs
Normal file
11
file finder test/Modules/DataBaseModules/DataBase.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace file_finder__test.DataBaseModules{
|
||||
|
||||
//Alloufi Yazan
|
||||
public class DataBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
60
file finder test/Modules/FileMangerModules/FileClassifier.cs
Normal file
60
file finder test/Modules/FileMangerModules/FileClassifier.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace file_finder__test;
|
||||
|
||||
public class FileClassifier
|
||||
{
|
||||
//returns 2 seperat lists music and vidio
|
||||
public async Task<(List<string> musicFiles, List<string> videoFiles, List<string> photoFiles)> ClassifyFilesAsync(
|
||||
//3 lists. all file paths and a list for vidio and music ext
|
||||
List<string> allFiles,
|
||||
List<string> musicExtensions,
|
||||
List<string> videoExtensions,
|
||||
List<string> photoExtensions)
|
||||
{
|
||||
//limit the max paralel tasks and split the total amount of files by the limit set
|
||||
int coreCount = Environment.ProcessorCount/2;
|
||||
int totalFiles = allFiles.Count;
|
||||
int chunkSize = (int)Math.Ceiling((double)totalFiles / coreCount);
|
||||
//concurrent bag instead of lists due to multi tasks
|
||||
var musicBag = new ConcurrentBag<string>();
|
||||
var videoBag = new ConcurrentBag<string>();
|
||||
var photoBag = new ConcurrentBag<string>();
|
||||
//a list of tasks. needed to check if all are done
|
||||
var tasks = new List<Task>();
|
||||
|
||||
for (int i = 0; i < coreCount; i++)
|
||||
{
|
||||
int start = i * chunkSize;
|
||||
int end = Math.Min(start + chunkSize, totalFiles);
|
||||
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
for (int j = start; j < end; j++)
|
||||
{
|
||||
string file = allFiles[j];
|
||||
string ext = Path.GetExtension(file)?.ToLowerInvariant();
|
||||
|
||||
if (ext == null)
|
||||
continue;
|
||||
|
||||
if (musicExtensions.Contains(ext))
|
||||
musicBag.Add(file);
|
||||
else if (videoExtensions.Contains(ext))
|
||||
videoBag.Add(file);
|
||||
else if (photoExtensions.Contains(ext))
|
||||
{
|
||||
photoBag.Add(file);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
//wait till all tasks are done and return 2 lists
|
||||
await Task.WhenAll(tasks);
|
||||
return (new List<string>(musicBag), new List<string>(videoBag), new List<string>(photoBag));
|
||||
}
|
||||
}
|
82
file finder test/Modules/FileMangerModules/FileScanner.cs
Normal file
82
file finder test/Modules/FileMangerModules/FileScanner.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class FileScanner
|
||||
{
|
||||
//all extensions and cuncurrent bag as a list wich works threw out multiple threads
|
||||
private readonly string[] _extensions;
|
||||
private readonly ConcurrentBag<string> _foundFiles = new ConcurrentBag<string>();
|
||||
|
||||
public FileScanner(string[] extensions)
|
||||
{
|
||||
_extensions = extensions.Select(e => e.ToLower()).ToArray();
|
||||
}
|
||||
//return a list of all coneckted drives
|
||||
public async Task<List<string>> ScanAllDrivesAsync()
|
||||
{
|
||||
var drives = new List<string>();
|
||||
|
||||
foreach (var drive in DriveInfo.GetDrives())
|
||||
{
|
||||
if (!drive.IsReady) continue;
|
||||
|
||||
Console.WriteLine($"Scanning {drive.Name} ...");
|
||||
|
||||
string root = drive.RootDirectory.FullName;
|
||||
drives.Add(root);
|
||||
}
|
||||
|
||||
return drives.ToList();
|
||||
}
|
||||
//scan teh designated drive or folder
|
||||
public async Task<List<string>> ScanDriveParallel(string rootPath)
|
||||
{
|
||||
//all sub direcktorys will be also added to this bag
|
||||
var folderQueue = new ConcurrentQueue<string>();
|
||||
folderQueue.Enqueue(rootPath);
|
||||
//all aktive skanners are placed here
|
||||
var folderWorkers = new List<Task>();
|
||||
//keep max tasks at half teh amounts of cores
|
||||
int maxWorkers = Environment.ProcessorCount/2;
|
||||
|
||||
for (int i = 0; i < maxWorkers; i++)
|
||||
{
|
||||
folderWorkers.Add(Task.Run(() =>
|
||||
{
|
||||
while (folderQueue.TryDequeue(out string currentPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check files
|
||||
foreach (var file in Directory.GetFiles(currentPath))
|
||||
{
|
||||
if (_extensions.Any(ext => file.EndsWith(ext, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
//Console.WriteLine(file);
|
||||
_foundFiles.Add(file);
|
||||
}
|
||||
}
|
||||
|
||||
// Enqueue subdirectories
|
||||
foreach (var dir in Directory.GetDirectories(currentPath))
|
||||
{
|
||||
folderQueue.Enqueue(dir);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Skip inaccessible folders silently
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
//wait till all tasks are done
|
||||
Task.WaitAll(folderWorkers.ToArray());
|
||||
//return the found paths as string
|
||||
return _foundFiles.ToList();
|
||||
}
|
||||
}
|
49
file finder test/Modules/FileMangerModules/VideoSeparator.cs
Normal file
49
file finder test/Modules/FileMangerModules/VideoSeparator.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class VideoSeparator
|
||||
{
|
||||
private readonly Regex episodePattern = new Regex(@"E\d{1,3}", RegexOptions.IgnoreCase);
|
||||
|
||||
public async Task<(List<string> seriesFiles, List<string> movieFiles)> SeparateVideosAsync(List<string> videoFiles)
|
||||
{
|
||||
int coreCount = Environment.ProcessorCount;
|
||||
int totalFiles = videoFiles.Count;
|
||||
int chunkSize = (int)Math.Ceiling((double)totalFiles / coreCount);
|
||||
|
||||
var seriesBag = new ConcurrentBag<string>();
|
||||
var movieBag = new ConcurrentBag<string>();
|
||||
var tasks = new List<Task>();
|
||||
|
||||
for (int i = 0; i < coreCount; i++)
|
||||
{
|
||||
int start = i * chunkSize;
|
||||
int end = Math.Min(start + chunkSize, totalFiles);
|
||||
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
for (int j = start; j < end; j++)
|
||||
{
|
||||
string path = videoFiles[j];
|
||||
string filename = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
if (filename == null)
|
||||
continue;
|
||||
|
||||
if (episodePattern.IsMatch(filename))
|
||||
seriesBag.Add(path);
|
||||
else
|
||||
movieBag.Add(path);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
return (new List<string>(seriesBag), new List<string>(movieBag));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user