109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace gehGassi.Web.Services
|
|
{
|
|
/// <summary>
|
|
/// Schnittstellenbeschreibung für einen Service der für alle Dateien in einem Orndern einen gemeinsamen Hash errechnet.
|
|
/// </summary>
|
|
public interface IScriptVersionCalculator
|
|
{
|
|
/// <summary>
|
|
/// Gibt eine Versionsnummer (hash) für alle Dateien in einem Ordner zurück
|
|
/// </summary>
|
|
/// <param name="absolutePath">Absoluter Pfad des Ordners</param>
|
|
/// <param name="filePattern">Datei-Pattern</param>
|
|
/// <returns>Errechnete Versionsnummer oder string.empty</returns>
|
|
string GetVersion(string absolutePath, string filePattern);
|
|
|
|
/// <summary>
|
|
/// Entfernt eine Kombination aus dem Versions-Cache, wenn vorhanden
|
|
/// </summary>
|
|
/// <param name="absolutePath">Absoluter Pfad des Ordners</param>
|
|
/// <param name="filePattern">Datei-Pattern</param>
|
|
void Clear(string absolutePath, string filePattern);
|
|
|
|
/// <summary>
|
|
/// Löscht alle Kombinationen aus dem Versions-Cache
|
|
/// </summary>
|
|
void ClearAll();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Service der für alle Dateien in einem Orndern einen gemeinsamen Hash errechnet.
|
|
/// </summary>
|
|
public class ScriptVersionCalculator : IScriptVersionCalculator
|
|
{
|
|
private Dictionary<string, string> _versions;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
public ScriptVersionCalculator()
|
|
{
|
|
_versions = new Dictionary<string, string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Versionsnummer (hash) für alle Dateien in einem Ordner zurück
|
|
/// </summary>
|
|
/// <param name="absolutePath">Absoluter Pfad des Ordners</param>
|
|
/// <param name="filePattern">Datei-Pattern</param>
|
|
/// <returns>Errechnete Versionsnummer oder string.empty</returns>
|
|
public string GetVersion(string absolutePath, string filePattern)
|
|
{
|
|
var result = string.Empty;
|
|
|
|
if (_versions.ContainsKey($"{absolutePath}/{filePattern}"))
|
|
return _versions[$"{absolutePath}/{filePattern}"];
|
|
|
|
if (Directory.Exists(absolutePath))
|
|
{
|
|
using (var md5 = MD5.Create())
|
|
{
|
|
byte[] totalBytes = { };
|
|
|
|
foreach (var file in Directory.EnumerateFiles(absolutePath, filePattern, SearchOption.AllDirectories))
|
|
{
|
|
using (var stream = File.OpenRead(file))
|
|
{
|
|
var hash = md5.ComputeHash(stream);
|
|
totalBytes = totalBytes.Concat(hash).ToArray();
|
|
}
|
|
}
|
|
if (totalBytes.Length > 0)
|
|
{
|
|
var finalHash = md5.ComputeHash(totalBytes);
|
|
result = Convert.ToBase64String(finalHash);
|
|
_versions.Add($"{absolutePath}/{filePattern}", result);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernt eine Kombination aus dem Versions-Cache, wenn vorhanden
|
|
/// </summary>
|
|
/// <param name="absolutePath">Absoluter Pfad des Ordners</param>
|
|
/// <param name="filePattern">Datei-Pattern</param>
|
|
public void Clear(string absolutePath, string filePattern)
|
|
{
|
|
if (_versions.ContainsKey($"{absolutePath}/{filePattern}"))
|
|
_versions.Remove($"{absolutePath}/{filePattern}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löscht alle Kombinationen aus dem Versions-Cache
|
|
/// </summary>
|
|
public void ClearAll()
|
|
{
|
|
_versions.Clear();
|
|
}
|
|
}
|
|
}
|