Fix PseudoHash being able to generate invalid filenames

This commit is contained in:
n00mkrad
2026-05-14 01:40:22 +02:00
parent 8ec4414392
commit 8dbb5300d6

View File

@@ -11,18 +11,26 @@ namespace Flowframes.Data
public static string GetHash(string path, bool b64 = true) public static string GetHash(string path, bool b64 = true)
{ {
bool isDir = Directory.Exists(path); bool isDir = Directory.Exists(path);
string hash = "";
if (isDir) if (isDir)
{ {
var dir = new DirectoryInfo(path); var dir = new DirectoryInfo(path);
var files = IoUtils.GetFileInfosSorted(path); var files = IoUtils.GetFileInfosSorted(path);
string dirHash = $"{dir.Name}{files.Sum(f => f.Length)}{dir.LastWriteTime.ToString("yyyyMMddHHmmss")}"; hash = $"{dir.Name}{files.Sum(f => f.Length)}{dir.LastWriteTime.ToString("yyyyMMddHHmmss")}";
return b64 ? Convert.ToBase64String(Encoding.UTF8.GetBytes(dirHash)).TrimEnd('=') : dirHash; }
else
{
var file = new FileInfo(path);
hash = $"{file.Name}{file.Length}{file.LastWriteTime.ToString("yyyyMMddHHmmss")}";
} }
var file = new FileInfo(path); if (!b64)
string hash = $"{file.Name}{file.Length}{file.LastWriteTime.ToString("yyyyMMddHHmmss")}"; return hash;
return b64 ? Convert.ToBase64String(Encoding.UTF8.GetBytes(hash)).TrimEnd('=') : hash;
string hashB64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(hash));
hashB64 = new string(hashB64.TrimEnd('=').Where(c => !Path.GetInvalidFileNameChars().Contains(c)).ToArray()); // Ensure no invalid chars in b64 hash
return hashB64;
} }
} }
} }