Esempio n. 1
0
        /// <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
                    SaveInventoryModelAsync(model);
                    return(model);
                }
            }
            Debug.WriteLine("WARNING: no inventory model");
            return(null);
        }
Esempio n. 2
0
        private static async void SaveInventoryModelAsync(ArchiveModelList model)
        {
            var         localFolder            = ApplicationData.Current.LocalFolder;
            var         formatter              = new DataContractSerializer(typeof(ArchiveModelList));
            var         inventoryModelFileName = Path.Combine(localFolder.Path, InventoryModelFileName);
            StorageFile file = await localFolder.CreateFileAsync(SettingsName, CreationCollisionOption.ReplaceExisting);

            MemoryStream stream = new MemoryStream();

            formatter.WriteObject(stream, model);
            await FileIO.WriteBytesAsync(file, stream.ToArray());

            Debug.WriteLine("Saved inventory model");
        }