feat: add library scanning mechanic

This commit is contained in:
2026-01-26 04:35:12 +01:00
parent 3a68531fb4
commit d46a2573c4
7 changed files with 355 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
using SharpExifTool;
using System.Security.Cryptography;
namespace Shadow.Tools;
public static class MetadataExtractor
{
private readonly static ExifTool exifTool = new();
private readonly static GeneralUseHelpers guhf = new();
public async static Task<Dictionary<string, string>> ExtractAsync(string fullPath)
{
// Get all relevant metadata
Dictionary<string, string> fileMetadata = new(await
exifTool.ExtractAllMetadataAsync(fullPath));
// Add in a MD5 hint
string md5sum = await GetFileMD5Async(fullPath);
fileMetadata?.Add("_shadow:fileHash", md5sum);
return fileMetadata ?? [];
}
/// <summary>
/// Compute MD5 checksum of a file
/// </summary>
/// <param name="fullPath">Input file absolute path</param>
/// <returns>MD5 hexstring</returns>
public static async Task<string> GetFileMD5Async(string fullPath)
{
string fallbackValue = String.Empty;
try
{
if (File.Exists(fullPath))
using (MD5 md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(fullPath))
{
byte[] hashBytes = await md5.ComputeHashAsync(stream);
string hashString = Convert.ToHexStringLower(hashBytes);
return hashString;
}
}
else return fallbackValue;
}
catch
{
return fallbackValue;
}
}
/// <summary>
/// Compute MD5 checksum of a string
/// </summary>
/// <param name="input">Input string for the MD5 hash</param>
/// <returns>MD5 hexstring</returns>
public static string GetStringMD5(string input)
{
// https://stackoverflow.com/a/24031467
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = MD5.HashData(inputBytes);
string hashString = Convert.ToHexStringLower(hashBytes);
return hashString;
}
}