/// <summary> /// Package by ID /// </summary> /// <param name="id">Package ID</param> /// <returns>Package</returns> public Package FindById(string id) { if (!Security.IsAuthorizedTo(Rights.ManagePackages)) { throw new UnauthorizedAccessException(); } Package galPkg = CachedPackages.FirstOrDefault(p => p.Id == id); Package locPkg = Packaging.FileSystem.LoadThemes().FirstOrDefault(p => p.Id == id); if (locPkg == null) { locPkg = Packaging.FileSystem.LoadExtensions().FirstOrDefault(p => p.Id == id); } if (locPkg == null) { locPkg = Packaging.FileSystem.LoadWidgets().FirstOrDefault(p => p.Id == id); } if (locPkg != null && galPkg != null) { // package installed fro gallery galPkg.SettingsUrl = locPkg.SettingsUrl; } return(galPkg == null ? locPkg : galPkg); }
public async Task GetDependencies(HashSet <int> dependecies, HashSet <int> returnDependencies) { foreach (int depPackageId in dependecies) { if (!returnDependencies.Contains(depPackageId) && !InstalledPackages.Any(x => x.PackageId == depPackageId)) { Package dependencyPackage = CachedPackages.FirstOrDefault(x => x.PackageId == depPackageId); if (dependencyPackage == default) { dependencyPackage = await WebWrapper.GetPackage(depPackageId); lock (CachedPackages) { CachedPackages.Add(dependencyPackage); } } if (!dependencyPackage.IsPaid) { returnDependencies.Add(depPackageId); } await GetDependencies(dependencyPackage.Dependencies.ToHashSet(), returnDependencies); } } }
/// <summary> /// Find packages /// </summary> /// <param name="take">Items to take</param> /// <param name="skip">Items to skip</param> /// <param name="filter">Filter expression</param> /// <param name="order">Sort order</param> /// <returns>List of packages</returns> public IEnumerable <Package> Find(int take = 10, int skip = 0, string filter = "", string order = "") { if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages)) { throw new System.UnauthorizedAccessException(); } if (take == 0) { take = CachedPackages.Count(); } if (string.IsNullOrEmpty(filter)) { filter = "1==1"; } if (string.IsNullOrEmpty(order)) { order = "LastUpdated desc"; } var packages = new List <Package>(); var query = CachedPackages.AsQueryable().Where(filter); foreach (var item in query.OrderBy(order).Skip(skip).Take(take)) { packages.Add(item); } return(packages); }
/// <summary> /// Package by ID /// </summary> /// <param name="id">Package ID</param> /// <returns>Package</returns> public Package FindById(string id) { if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ManagePackages)) { throw new System.UnauthorizedAccessException(); } return(CachedPackages.FirstOrDefault(pkg => pkg.Id == id)); }
public PackageManager(Uri apiUrl, MainWindow mw, string RWPath) { ApiUrl = apiUrl; MainWindow = mw; SqLiteAdapter = new SqLiteAdapter(Path.Combine(RWPath, "main.dls")); InstalledPackages = SqLiteAdapter.LoadInstalledPackages(); CachedPackages = CachedPackages.Union(InstalledPackages).ToList(); WebWrapper = new WebWrapper(ApiUrl); }
public IQueryable <ServerPackage> GetPackages(ClientCompatibility compatibility) { var cache = CachedPackages.AsQueryable(); if (!compatibility.AllowSemVer2) { cache = cache.Where(p => !p.Version.IsSemVer2()); } return(cache); }
public void GetPackagesToDownload(IEnumerable <string> allMissing) { IEnumerable <string> depsToDownload = allMissing.Intersect(DownloadableDeps); while (depsToDownload.Count() > 0) { Package pkg = CachedPackages.First(x => x.PackageId == DownloadableDepsPackages[depsToDownload.First()]); PkgsToDownload.Add(pkg.PackageId); depsToDownload = depsToDownload.Except(pkg.FilesContained); } }
/// <summary> /// Returns list of packages from online gallery for a single page /// </summary> /// <param name="pkgType">Theme, Widget or Extension</param> /// <param name="page">Current page</param> /// <param name="sortOrder">Order - newest, most downloaded etc.</param> /// <param name="searchVal">Search term if it is search request</param> /// <returns>List of packages</returns> public static List <JsonPackage> FromGallery(string pkgType, int page = 0, Gallery.OrderType sortOrder = Gallery.OrderType.Newest, string searchVal = "") { var packages = new List <JsonPackage>(); var gallery = CachedPackages.Where(p => p.Location == "G" || p.Location == "I"); if (pkgType != "all") { gallery = gallery.Where(p => p.PackageType == pkgType).ToList(); } foreach (var pkg in gallery) { if (string.IsNullOrEmpty(searchVal)) { packages.Add(pkg); } else { if (pkg.Title.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) != -1 || pkg.Description.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) != -1 || (!string.IsNullOrWhiteSpace(pkg.Tags) && pkg.Tags.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) != -1)) { packages.Add(pkg); } } } Gallery.GalleryPager = new Pager(page, packages.Count, pkgType); if (packages.Count > 0) { switch (sortOrder) { case Gallery.OrderType.Downloads: packages = packages.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Title).ToList(); break; case Gallery.OrderType.Rating: packages = packages.OrderByDescending(p => p.Rating).ThenBy(p => p.Title).ToList(); break; case Gallery.OrderType.Newest: packages = packages.OrderByDescending(p => Convert.ToDateTime(p.LastUpdated, Utils.GetDefaultCulture())).ThenBy(p => p.Title).ToList(); break; case Gallery.OrderType.Alphanumeric: packages = packages.OrderBy(p => p.Title).ToList(); break; } } return(packages.Skip(page * Constants.PageSize).Take(Constants.PageSize).ToList()); }
public async Task <List <int> > FindFile(string file_name, bool withDeps = true) { Package package = InstalledPackages.FirstOrDefault(x => x.FilesContained.Contains(file_name)); if (package != default) { return new List <int>() { package.PackageId } } ; lock (CachedPackages) package = CachedPackages.FirstOrDefault(x => x.FilesContained.Contains(file_name)); if (package != default) { return new List <int>() { package.PackageId } } ; Package onlinePackage = await WebWrapper.SearchForFile(file_name); if (onlinePackage != null && onlinePackage.PackageId > 0) { lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == onlinePackage.PackageId)) { CachedPackages.Add(onlinePackage); } } HashSet <int> dependencyPkgIds = new HashSet <int>(); if (withDeps) { await GetDependencies(new HashSet <int>() { onlinePackage.PackageId }, dependencyPkgIds); } return(new List <int>() { onlinePackage.PackageId }.Union(dependencyPkgIds).ToList()); } return(new List <int>()); }
public async Task ReceiveMSMQ() { string queueFile = Path.Combine(Path.GetTempPath(), "DLS.queue"); HashSet <string> queuedPkgs = File.Exists(queueFile) ? File.ReadAllText(queueFile).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet() : new HashSet <string>(); if (queuedPkgs.Count > 0) { MainWindow.Dispatcher.Invoke(() => { MainWindow.Activate(); }); if (await CheckLogin(1) < 0 || App.IsDownloading) { File.WriteAllText(queueFile, string.Empty); return; } int idToDownload = Convert.ToInt32(queuedPkgs.PopOne()); if (!InstalledPackages.Exists(x => x.PackageId == idToDownload)) { Task.Run(async() => { Package packageToDownload = await WebWrapper.GetPackage(idToDownload); lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == packageToDownload.PackageId)) { CachedPackages.Add(packageToDownload); } } if (packageToDownload.IsPaid) { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download package", Content = "This is paid package. Paid packages cannot be downloaded through this app.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); return; } HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { packageToDownload.PackageId }, depsPkgs); HashSet <int> packageIds = new HashSet <int>() { packageToDownload.PackageId }.Union(depsPkgs).ToHashSet(); if (packageIds.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); await MainWindow.DownloadDialog.DownloadPackages(packageIds, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter); App.IsDownloading = false; } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download packages", Content = "An error ocured when trying to install package.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); }).Start(); } }).Wait(); } else { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = "Cannot download package", Content = "This package is already downloaded.", SecondaryButtonText = "OK", Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); } File.WriteAllText(queueFile, string.Join(",", queuedPkgs)); await ReceiveMSMQ(); } }
public async Task <HashSet <string> > GetDownloadableDependencies(HashSet <string> globalDependencies, HashSet <string> existing, MainWindow mw) { InstalledPackages = SqLiteAdapter.LoadInstalledPackages(); HashSet <string> allDownloadableDeps = await WebWrapper.QueryArray("listFiles"); HashSet <string> conflictDeps = existing.Intersect(allDownloadableDeps).Except(InstalledPackages.SelectMany(x => x.FilesContained)).ToHashSet(); HashSet <int> conflictPackages = new HashSet <int>(); int maxThreads = Math.Min(Environment.ProcessorCount, conflictDeps.Count); Parallel.For(0, maxThreads, workerId => { Task.Run(async() => { int max = conflictDeps.Count * (workerId + 1) / maxThreads; for (int i = conflictDeps.Count * workerId / maxThreads; i < max; i++) { List <int> packages = await FindFile(conflictDeps.ElementAt(i), false); Trace.Assert(packages.Count > 0, $"FindFile for {conflictDeps.ElementAt(i)} returned no packages!"); if (packages.Count > 0) { int id = packages.First(); lock (conflictPackages) { if (conflictPackages.Contains(id)) { continue; } conflictPackages.Add(id); } } } }).Wait(); }); bool rewriteAll = false; bool keepAll = false; for (int i = 0; i < conflictPackages.Count; i++) { int id = conflictPackages.ElementAt(i); if (Settings.Default.IgnoredPackages?.Contains(id) == true) { continue; } Package p = CachedPackages.FirstOrDefault(x => x.PackageId == id); bool rewrite = false; if (!rewriteAll && !keepAll) { Task <ContentDialogResult> t = null; mw.Dispatcher.Invoke(() => { MainWindow.ContentDialog = new ConflictPackageDialog(p.DisplayName); t = MainWindow.ContentDialog.ShowAsync(); }); ContentDialogResult result = await t; ConflictPackageDialog dlg = (ConflictPackageDialog)MainWindow.ContentDialog; rewrite = dlg.RewriteLocal; rewriteAll = dlg.RewriteAll; keepAll = dlg.KeepAll; } if (rewrite || rewriteAll) { PkgsToDownload.Add(id); HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { id }, depsPkgs); PkgsToDownload.UnionWith(depsPkgs); } else { if (Settings.Default.IgnoredPackages == null) { Settings.Default.IgnoredPackages = new List <int>(); } Settings.Default.IgnoredPackages.Add(id); Settings.Default.Save(); } } CheckUpdates(); DownloadableDeps = allDownloadableDeps.Intersect(globalDependencies).ToHashSet(); return(DownloadableDeps); }
public override IQueryable <IPackage> GetPackages() { return(CachedPackages.AsQueryable()); }
public async Task ReceiveMSMQ() { string queueFile = Path.Combine(Path.GetTempPath(), "DLS.queue"); HashSet <string> queuedPkgs = File.Exists(queueFile) ? File.ReadAllText(queueFile).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet() : new HashSet <string>(); if (queuedPkgs.Count > 0) { MainWindow.Dispatcher.Invoke(() => { MainWindow.Activate(); }); if (!await Utils.CheckLogin(async delegate { await ReceiveMSMQ(); MSMQRunning = false; }, MainWindow, ApiUrl) || App.IsDownloading) { //File.WriteAllText(queueFile, string.Empty); return; } int idToDownload = Convert.ToInt32(queuedPkgs.PopOne()); if (!InstalledPackages.Exists(x => x.PackageId == idToDownload)) { Task.Run(async() => { Package packageToDownload = await WebWrapper.GetPackage(idToDownload); lock (CachedPackages) { if (!CachedPackages.Any(x => x.PackageId == packageToDownload.PackageId)) { CachedPackages.Add(packageToDownload); } } if (packageToDownload.IsPaid) { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.PaidPackageFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); return; } HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { packageToDownload.PackageId }, depsPkgs); HashSet <int> packageIds = new HashSet <int>() { packageToDownload.PackageId }.Union(depsPkgs).ToHashSet(); if (packageIds.Count > 0) { App.IsDownloading = true; MainWindow.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); }); await MainWindow.DownloadDialog.DownloadPackages(packageIds, CachedPackages, InstalledPackages, WebWrapper, SqLiteAdapter); App.IsDownloading = false; } else { new Task(() => { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.InstalFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); }).Start(); } }).Wait(); } else { App.Window.Dispatcher.Invoke(() => { MainWindow.ErrorDialog = new ContentDialog() { Title = Localization.Strings.CantDownload, Content = Localization.Strings.AlreadyInstalFail, SecondaryButtonText = Localization.Strings.Ok, Owner = App.Window }; MainWindow.ErrorDialog.ShowAsync(); }); } File.WriteAllText(queueFile, string.Join(",", queuedPkgs)); await ReceiveMSMQ(); } }
public async Task ResolveConflicts() { HashSet <string> conflictDeps = MainWindow.RW.AllInstalledDeps.Intersect(DownloadableDeps).Except(InstalledPackages.SelectMany(x => x.FilesContained)).ToHashSet(); HashSet <int> conflictPackages = new HashSet <int>(); while (conflictDeps.Count > 0) { Package pkg = CachedPackages.First(x => x.FilesContained.Contains(conflictDeps.First())); conflictPackages.Add(pkg.PackageId); conflictDeps.ExceptWith(pkg.FilesContained); } //HashSet<int> conflictPackages = conflictPkgs.Select(x => x.PackageId).ToHashSet(); bool rewriteAll = false; bool keepAll = false; for (int i = 0; i < conflictPackages.Count; i++) { int id = conflictPackages.ElementAt(i); if (Settings.Default.IgnoredPackages?.Contains(id) == true) { continue; } Package p = CachedPackages.FirstOrDefault(x => x.PackageId == id); bool rewrite = false; if (!rewriteAll && !keepAll) { Task <ContentDialogResult> t = null; MainWindow.Dispatcher.Invoke(() => { MainWindow.ContentDialog = new ConflictPackageDialog(p.DisplayName); t = MainWindow.ContentDialog.ShowAsync(); }); ContentDialogResult result = await t; ConflictPackageDialog dlg = (ConflictPackageDialog)MainWindow.ContentDialog; rewrite = dlg.RewriteLocal; rewriteAll = dlg.RewriteAll; keepAll = dlg.KeepAll; } if (rewrite || rewriteAll) { PkgsToDownload.Add(id); HashSet <int> depsPkgs = new HashSet <int>(); await GetDependencies(new HashSet <int>() { id }, depsPkgs); PkgsToDownload.UnionWith(depsPkgs); } else { if (Settings.Default.IgnoredPackages == null) { Settings.Default.IgnoredPackages = new List <int>(); } Settings.Default.IgnoredPackages.Add(id); Settings.Default.Save(); } } }
public async Task VerifyCache() { List <Package> localCache = SqLiteAdapter.LoadPackages(true); ServerVersions = new Dictionary <int, int>(); localCache.ForEach(x => ServerVersions[x.PackageId] = x.Version); Tuple <IEnumerable <Package>, HashSet <int> > tRemoteCache = await WebWrapper.ValidateCache(ServerVersions); IEnumerable <Package> remoteCache = tRemoteCache.Item1; HashSet <int> remoteVersions = tRemoteCache.Item2; lock (CachedPackages) { CachedPackages = remoteCache.ToList(); localCache.ForEach(x => { if (!remoteVersions.Contains(x.PackageId)) { CachedPackages.Add(x); } else { ServerVersions[x.PackageId] = CachedPackages.First(y => y.PackageId == x.PackageId).Version; } }); foreach (Package pkg in remoteCache) { ServerVersions[pkg.PackageId] = pkg.Version; } } if (remoteVersions.Count > 0) { new Task(() => { foreach (Package pkg in CachedPackages) { SqLiteAdapter.SavePackage(pkg, true); } SqLiteAdapter.FlushToFile(true); }).Start(); } CachedPackages.ForEach(x => { if (x.IsPaid) { x.FilesContained.ForEach(y => { DownloadablePaidDepsPackages[y] = x.PackageId; DownloadablePaidDeps.Add(y); }); } else { x.FilesContained.ForEach(y => { DownloadableDepsPackages[y] = x.PackageId; DownloadableDeps.Add(y); }); } }); }
/// <summary> /// Package by ID /// </summary> /// <param name="pkgId">Package ID</param> /// <returns>Package</returns> public static JsonPackage GetPackage(string pkgId) { return(CachedPackages.FirstOrDefault(pkg => pkg.Id == pkgId)); }