private static void SaveInventoryModel(ArchiveModelList model) { var formatter = new BinaryFormatter(); var inventoryModelFileName = Path.Combine(GetTempDirectory(), InventoryModelFileName); File.Delete(inventoryModelFileName); var stream = new FileStream(inventoryModelFileName, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, model); stream.Close(); Debug.WriteLine("Saved inventory model"); }
/// <summary> /// Get any downloaded inventory file of the Glacier vault, /// build a view model with statuses of any Archive jobs and return /// </summary> public static ArchiveModelList GetArchiveModel() { // Look for downloaded inventory var inventoryFile = Path.Combine(GetTempDirectory(), InventoryFileName); if (File.Exists(inventoryFile)) { // Found inventory, return model with the statuses of any related topics found using (var file = File.Open(inventoryFile, FileMode.Open)) { Debug.WriteLine("Getting Archive Model"); var s = new DataContractJsonSerializer(typeof(Inventory)); var inventory = (Inventory)s.ReadObject(file); var dt = DateTime.Parse(inventory.InventoryDate); var model = new ArchiveModelList { InventoryDate = dt }; foreach (var archive in inventory.ArchiveList) { var topicFile = GetArchiveTopicFileName(archive.ArchiveId); var status = GetExistingTopic(topicFile)?.Status ?? GlacierResult.NoJob; model.Add(new ArchiveModel { ArchiveId = archive.ArchiveId, Description = archive.ArchiveDescription, GlacierJobStatus = status, Size = archive.Size, ArchiveTopicFilePath = Path.Combine(GetTempDirectory(), topicFile) }); } // Delete topic files which are not related to the inventory var topicFiles = Directory.GetFiles( GetTempDirectory(), "glacier.archive*.topic", SearchOption.TopDirectoryOnly); var toDelete = from t in topicFiles where !model.Exists(m => m.ArchiveTopicFilePath == t) select t; toDelete.ToList().ForEach(t => { File.Delete(Path.Combine(GetTempDirectory(), t)); Debug.WriteLine($"Deleted {t}"); }); // Update inventory model file SaveInventoryModel(model); return(model); } } Debug.WriteLine("WARNING: no inventory model"); return(null); }