public BoolWithMessage AddAsset() { string errorMessage = ValidateAssetInfo(null); if (!string.IsNullOrWhiteSpace(errorMessage)) { return(BoolWithMessage.False($"The following errors were found:\n\n{errorMessage}")); } Asset newAsset = new Asset() { ID = $"{SelectedAssetID}{SelectedIDExtension}", Author = SelectedAssetAuthor, Category = SelectedAssetCategory, Description = SelectedAssetDescription, Name = SelectedAssetName, PreviewImage = SelectedAssetImageUrl, UpdatedDate = DateTime.UtcNow, Version = 1, }; if (!string.IsNullOrWhiteSpace(SelectedAssetUpdatedDate)) { DateTime.TryParse(SelectedAssetUpdatedDate, out DateTime updateDate); if (updateDate != null && updateDate != DateTime.MinValue) { newAsset.UpdatedDate = updateDate; } } double.TryParse(SelectedAssetVersion, out double version); if (version <= 0) { newAsset.Version = version; } if (SelectedAssetDownloadType == "Url") { newAsset.DownloadLink = AssetCatalog.FormatUrl(SelectedAssetDownloadUrl); } else if (SelectedAssetDownloadType == "Google Drive") { newAsset.DownloadLink = $"rsmm://GDrive/{SelectedAssetDownloadUrl}"; } AssetViewModel viewModel = new AssetViewModel(newAsset); AssetList.Add(viewModel); AssetToEdit = null; SelectedAsset = null; SelectedAsset = AssetList[AssetList.Count - 1]; return(BoolWithMessage.True()); }
private BoolWithMessage UpdateAsset(AssetViewModel assetToUpdate) { if (assetToUpdate == null) { return(BoolWithMessage.False("assetToUpdate is null")); } string validationMsg = ValidateAssetInfo(assetToUpdate); if (!string.IsNullOrEmpty(validationMsg)) { UpdatedAssetInvalid?.Invoke(validationMsg); return(BoolWithMessage.False(validationMsg)); } if (SelectedAssetDownloadType == "Url") { assetToUpdate.Asset.DownloadLink = AssetCatalog.FormatUrl(SelectedAssetDownloadUrl); } else if (SelectedAssetDownloadType == "Google Drive") { assetToUpdate.Asset.DownloadLink = $"rsmm://GDrive/{SelectedAssetDownloadUrl}"; } else if (SelectedAssetDownloadType == "Mega") { assetToUpdate.Asset.DownloadLink = $"rsmm://MegaFile/{SelectedAssetDownloadUrl}"; } assetToUpdate.Asset.ID = $"{SelectedAssetID}{SelectedIDExtension}"; assetToUpdate.Asset.Name = SelectedAssetName; assetToUpdate.Asset.Author = SelectedAssetAuthor; assetToUpdate.Asset.Category = SelectedAssetCategory; assetToUpdate.Asset.Description = SelectedAssetDescription; assetToUpdate.Asset.PreviewImage = SelectedAssetImageUrl; double.TryParse(SelectedAssetVersion, out double version); if (version <= 0) { assetToUpdate.Asset.Version = 1; } else { assetToUpdate.Asset.Version = version; } DateTime.TryParse(SelectedAssetUpdatedDate, out DateTime updateDate); if (updateDate != DateTime.MinValue) { assetToUpdate.Asset.UpdatedDate = updateDate.ToUniversalTime(); } else if (string.IsNullOrWhiteSpace(SelectedAssetUpdatedDate)) { assetToUpdate.Asset.UpdatedDate = DateTime.UtcNow; } assetToUpdate.Name = assetToUpdate.Asset.Name; assetToUpdate.Author = assetToUpdate.Asset.Author; assetToUpdate.AssetCategory = assetToUpdate.Asset.Category; assetToUpdate.Version = assetToUpdate.Asset.Version.ToString(); assetToUpdate.UpdatedDate = assetToUpdate.Asset.UpdatedDate.ToLocalTime().ToString(AssetViewModel.dateTimeFormat); assetToUpdate.Description = assetToUpdate.Asset.Description; return(BoolWithMessage.True()); }
public Task CheckForCatalogUpdatesAsync(bool clearCache = true) { object countLock = new object(); Task t = Task.Factory.StartNew(() => { string catFile = AbsolutePathToCatalogSettingsJson; Directory.CreateDirectory(AbsolutePathToTempDownloads); CatalogSettings currentSettings = new CatalogSettings(); if (File.Exists(catFile)) { currentSettings = JsonConvert.DeserializeObject <CatalogSettings>(File.ReadAllText(catFile)); currentSettings.CatalogUrls.RemoveAll(s => string.IsNullOrWhiteSpace(s.Url)); } else { CatalogSettings.AddDefaults(currentSettings); } if (currentSettings.CatalogUrls.Count == 0) { if (File.Exists(catFile)) { File.Delete(catFile); } _catalogCache = new AssetCatalog(); ReloadAllAssets(); RefreshFilteredAssetList(); return; } if (clearCache) { lock (catalogCacheLock) { _catalogCache = new AssetCatalog(); } } foreach (CatalogSubscription sub in currentSettings.CatalogUrls.Where(c => c.IsActive).ToArray()) { Logger.Info($"Checking catalog {sub.Url}"); string uniqueFileName = $"cattemp{Path.GetRandomFileName()}.xml"; // save temp catalog update to unique filename so multiple catalog updates can download async string path = Path.Combine(AbsolutePathToTempDownloads, uniqueFileName); Guid downloadId = Guid.NewGuid(); Action onCancel = () => { RemoveFromDownloads(downloadId); }; Action onError = () => { RemoveFromDownloads(downloadId); }; Action onComplete = () => { RemoveFromDownloads(downloadId); try { AssetCatalog c = JsonConvert.DeserializeObject <AssetCatalog>(File.ReadAllText(path)); lock (catalogCacheLock) // put a lock on the Catalog so multiple threads can only merge one at a time { _catalogCache = AssetCatalog.Merge(_catalogCache, c); File.WriteAllText(AbsolutePathToCatalogJson, JsonConvert.SerializeObject(_catalogCache, Formatting.Indented)); } } catch (Exception ex) { Logger.Error(ex); } finally { // delete temp catalog if (File.Exists(path)) { File.Delete(path); } ReloadAllAssets(); RefreshFilteredAssetList(); } }; DownloadItemViewModel catalogDownload = new DownloadItemViewModel() { UniqueId = downloadId, DownloadType = DownloadType.Catalog, ItemName = $"Checking catalog {sub.Url}", DownloadUrl = AssetCatalog.FormatUrl(sub.Url), SaveFilePath = path, OnComplete = onComplete, OnError = onError, OnCancel = onCancel }; AddToDownloads(catalogDownload); } }); return(t); }