Exemple #1
0
        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);
        }
Exemple #2
0
        private static string GetFileContent(DimFile file)
        {
            string content = "";

            using (var streamReader = new StreamReader(file.FilePath))
            {
                content = streamReader.ReadToEnd();
            }
            return(content);
        }
Exemple #3
0
        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;
        }
Exemple #4
0
        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);
            }
        }
Exemple #5
0
        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);
        }
Exemple #6
0
 public DimRecord(DimFile file)
 {
     this.FileName = file.FileName;
     this.FileHash = file.FileHash;
 }
Exemple #7
0
 private static string GetFileContent(DimFile file)
 {
     string content = "";
     using(var streamReader = new StreamReader(file.FilePath))
     {
         content = streamReader.ReadToEnd();
     }
     return content;
 }
Exemple #8
0
 public DimRecord(DimFile file)
 {
     this.FileName = file.FileName;
     this.FileHash = file.FileHash;
 }