public static void ExecuteFile(DimFile file, bool dryRun, Action successCallback = null, Action<string> failureCallback = null) { Exception exception = null; if(!dryRun) { try { DatabaseProvider.Manager.Execute(GetFileContent(file)); } catch (Exception ex) { exception = ex; } if(exception == null) { var record = DatabaseProvider.RecordRepository.FindByFileName(file.FileName); if(record == null) record = new DimRecord(); record.FileName = file.FileName; record.FileHash = GetFileHash(file); record.Executed =DateTime.Now; DatabaseProvider.RecordRepository.Save(record); } } if(successCallback != null && exception == null) successCallback(); if(failureCallback != null && exception != null) failureCallback(exception.Message); }
private static string GetFileContent(DimFile file) { string content = ""; using (var streamReader = new StreamReader(file.FilePath)) { content = streamReader.ReadToEnd(); } return(content); }
public static string GetFileHash(DimFile file) { var fileContent = GetFileContent(file); // Get byte array for file contents and hash using sha1. byte[] contentBytes = UTF8Encoding.UTF8.GetBytes(fileContent); byte[] sha1Bytes = new SHA1Managed().ComputeHash(contentBytes); // Convert to hexidecimal string from sha1'd bytes. var hex = ""; StringBuilder builder = new StringBuilder(sha1Bytes.Length * 2); foreach(var b in sha1Bytes) builder.Append(b.ToString("x2")); hex = builder.ToString(); // Return the hash string return hex; }
public static void ExecuteFile(DimFile file, bool dryRun, Action successCallback = null, Action <string> failureCallback = null) { Exception exception = null; if (!dryRun) { try { DatabaseProvider.Manager.Execute(GetFileContent(file)); } catch (Exception ex) { exception = ex; } if (exception == null) { var record = DatabaseProvider.RecordRepository.FindByFileName(file.FileName); if (record == null) { record = new DimRecord(); } record.FileName = file.FileName; record.FileHash = GetFileHash(file); record.Executed = DateTime.Now; DatabaseProvider.RecordRepository.Save(record); } } if (successCallback != null && exception == null) { successCallback(); } if (failureCallback != null && exception != null) { failureCallback(exception.Message); } }
public static string GetFileHash(DimFile file) { var fileContent = GetFileContent(file); // Get byte array for file contents and hash using sha1. byte[] contentBytes = UTF8Encoding.UTF8.GetBytes(fileContent); byte[] sha1Bytes = new SHA1Managed().ComputeHash(contentBytes); // Convert to hexidecimal string from sha1'd bytes. var hex = ""; StringBuilder builder = new StringBuilder(sha1Bytes.Length * 2); foreach (var b in sha1Bytes) { builder.Append(b.ToString("x2")); } hex = builder.ToString(); // Return the hash string return(hex); }
public DimRecord(DimFile file) { this.FileName = file.FileName; this.FileHash = file.FileHash; }
private static string GetFileContent(DimFile file) { string content = ""; using(var streamReader = new StreamReader(file.FilePath)) { content = streamReader.ReadToEnd(); } return content; }