private void SynchronizeHashStorage(DirectoryInfo directory, Data.HashStorage storage) { if (storage.CurrentElement == null) { storage.Write(directory, DirectoryFileHash(directory).Result, this.store); } else { var hashCount = storage.CurrentElement.Attribute("count").Value; if (hashCount == directory.GetFiles().Count().ToString()) { // do nothing } else if (hashCount == "0") { var contents = DirectoryFileHash(directory).Result; storage.Write(directory, contents, this.store); } else { var files = directory.GetFiles(); var fileNodes = storage.CurrentElement.Elements(); var fileSearchResult = files.Select(x => { var result = fileNodes.Select(y => ((string)y.Attribute("name") != x.Name)).First(); if (result) { return(x); } else { return(null); } } ); var list = new List <KeyValuePair <FileInfo, byte[]> >(); foreach (var item in fileSearchResult) { if (item == null) { continue; } var result = this.ComputeHash(item).Result; list.Add(new KeyValuePair <FileInfo, byte[]>(item, result)); } storage.Write(directory, list, this.store); } } }
public bool SaveAttachmentsToFolder(Microsoft.Exchange.WebServices.Data.AttachmentCollection attachments) { if (string.IsNullOrEmpty(this.path)) { var argEx = new ArgumentNullException("File Path", "File path must be set prior to calling this method"); Logger.LoggerAsync.InstanceOf.GeneralLogger.Error(argEx); throw argEx; } var directory = new System.IO.DirectoryInfo(this.path); var xmlStorage = new Data.HashStorage(@"..\XmlStorage\hashTable.xml"); xmlStorage.Read(); var attachmentList = this.AttachmentFileHash(attachments).Result; var result = xmlStorage.MoveToProjectRfiFolder(this.store.ProjectNumber); this.SynchronizeHashStorage(directory, xmlStorage); if (result == false) { xmlStorage.MoveToProjectRfiFolder(this.store.ProjectNumber); } // test names and hashes foreach (var item in attachmentList) { var parser = new Utilities.Parser(); var isValidName = parser.IsValidRfiAttachmentName(item.Key.Name); if (isValidName == false) { return(false); } var comparisonResult = xmlStorage.Comparator(item); switch (comparisonResult) { case Enum.FileComparisonResult.NoMatch: var destination = directory.FullName + @"\" + item.Key.Name; xmlStorage.Write(directory, new List <KeyValuePair <FileInfo, byte[]> > { item }, this.store); var newFile = item.Key.CopyTo(destination); newFile.Refresh(); item.Key.Delete(); result = newFile.Exists; break; case Enum.FileComparisonResult.SameNames: case Enum.FileComparisonResult.SameNamesAndData: result = true; break; case Enum.FileComparisonResult.SameData: result = false; break; default: throw new Exception("Invalid value for FileComparisonResult"); } } return(result); }