protected void GetNextLatestVersionPackages(int feedId, string packageId, IInternalPackageRepository packageRepository, out IInternalPackage latestAbsoluteVersionPackage, out IInternalPackage latestVersionPackage) { List <IInternalPackage> versionsOfPackage; using (ITransaction transaction = Store.BeginTransaction()) { versionsOfPackage = packageRepository.GetVersions(transaction, packageId, true).ToList(); } if (versionsOfPackage.Any()) { latestVersionPackage = versionsOfPackage.Where(vp => !vp.IsPrerelease).Aggregate(versionsOfPackage[0], (highest, candiate) => candiate.GetSemanticVersion().CompareTo(highest.GetSemanticVersion()) > 0 ? candiate : highest); latestAbsoluteVersionPackage = versionsOfPackage.Aggregate(versionsOfPackage[0], (highest, candiate) => candiate.GetSemanticVersion().CompareTo(highest.GetSemanticVersion()) > 0 ? candiate : highest); } else { latestVersionPackage = null; latestAbsoluteVersionPackage = null; } }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); int page = int.Parse(module.Request.Query["page"]); int pageSize = int.Parse(module.Request.Query["pageSize"]); int totalResults; List <IFeed> feeds; using (ITransaction transaction = _store.BeginTransaction()) { feeds = transaction.Query <IFeed>().ToList(pageSize * page, pageSize, out totalResults); } feeds.Where(fd => !string.IsNullOrWhiteSpace(fd.ApiKeyHashed)).ToList().ForEach(fd => fd.HasApiKey = true); feeds.ForEach(fd => fd.ApiKeyHashed = null); //Temporary until API Key table is used feeds.ForEach(fd => fd.ApiKeySalt = null); //Temporary until API Key table is used var totalPages = (int)Math.Ceiling((double)totalResults / pageSize); return(new { TotalCount = totalResults, TotalPages = totalPages, Results = feeds }); }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); FeedSearchResponse response = new FeedSearchResponse(); using (ITransaction transaction = _store.BeginTransaction()) { string name = module.Request.Query.name; int totalResults; var feeds = transaction.Query <IFeed>().Where("Name like @feedName").Parameter("feedName", "%" + name + "%").ToList(0, 10, out totalResults); var category = new FeedSearchResponse.Category("Default"); response.Results.Add(category); string rootUrl = module.Request.Url.SiteBase + "/#feeds/view/{0}"; foreach (var feed in feeds) { category.Feeds.Add(new FeedSearchResponse.Category.FeedResult(feed.Name, string.Format(rootUrl, feed.Id))); } } return(response); }
public dynamic Execute(dynamic parameters, global::Nancy.INancyModule module) { string id = parameters.id; string version = parameters.version; string feedName = parameters.feed; int feedId; using (ITransaction transaction = _store.BeginTransaction()) { var feed = transaction.Query <IFeed>().Where("Name = @feedName").Parameter("feedName", feedName).First(); if (feed == null) { var errorResponse = module.Response.AsText("Feed does not exist."); errorResponse.StatusCode = HttpStatusCode.BadRequest; return(errorResponse); } feedId = feed.Id; } var packageRepository = _packageRepositoryFactory.Create(feedId); IInternalPackage package = packageRepository.GetPackage(id, new SemanticVersion(version)); if (package == null) { var errorResponse = module.Response.AsText(string.Format("Package {0} version {1} not found.", id, version)); errorResponse.StatusCode = HttpStatusCode.NotFound; return(errorResponse); } var response = new Response(); bool endsWithSlash = _portalConfig.ListenPrefixes.EndsWith("/"); var location = string.Format("{0}{1}feeds/{2}/packages/{3}/{4}", _portalConfig.ListenPrefixes, endsWithSlash ? "" : "/", feedName, package.PackageId, package.Version); response.Headers.Add("Location", location); response.Contents = delegate(Stream stream) { var writer = new StreamWriter(stream) { AutoFlush = true }; writer.Write("<html><head><title>Object moved</title></head><body><h2>Object moved to <a href=\"{0}\">here</a>.</h2></body></html>", location); }; response.ContentType = "text/html"; response.StatusCode = HttpStatusCode.Found; return(response); }
public dynamic Execute(dynamic parameters, global::Nancy.INancyModule module) { module.RequiresAuthentication(); using (ITransaction transaction = _store.BeginTransaction()) { var model = new FeedPackageCountStatistic(transaction).GetModel(); return(model); } }
public dynamic Execute(dynamic parameters, global::Nancy.INancyModule module) { module.RequiresAuthentication(); using (ITransaction transaction = _store.BeginTransaction()) { int feedId = int.Parse(parameters.id); return(transaction.Query <IFeedConfiguration>().Where("FeedId = @feedId").Parameter("feedId", feedId).First()); } }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); using (ITransaction transaction = _store.BeginTransaction()) { var model = new SystemInformationStatistic(transaction).GetModel(); return(model); } }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); IFeed feed; try { int feedId = int.Parse(parameters.id); feed = module.Bind <Feed>(); if (feedId != feed.Id) { return(HttpStatusCode.BadRequest); } ITransaction transaction = _store.BeginTransaction(); var existingFeedExists = transaction.Query <IFeed>().Where("Id = @feedId").Parameter("feedId", feedId).First(); if (existingFeedExists == null) { return(HttpStatusCode.NotFound); } if (!string.IsNullOrWhiteSpace(feed.ApiKey)) { ICryptoService cryptoService = new PBKDF2(); feed.ApiKeySalt = cryptoService.GenerateSalt(); feed.ApiKeyHashed = cryptoService.Compute(feed.ApiKey); } else if (feed.HasApiKey) { feed.ApiKeyHashed = existingFeedExists.ApiKeyHashed; //Temporary until API Key table is used feed.ApiKeySalt = existingFeedExists.ApiKeySalt; //Temporary until API Key table is used } transaction.Update(feed); transaction.Commit(); transaction.Dispose(); } catch (Exception ex) { return(HttpStatusCode.InternalServerError); } return(feed); }
private IFeed GetFeedModel(string feedName) { IFeed feed; using (ITransaction transaction = Store.BeginTransaction()) { feed = transaction.Query <IFeed>() .Where("Name = @feedName") .Parameter("feedName", feedName) .First(); } return(feed); }
public dynamic Execute(dynamic parameters, global::Nancy.INancyModule module) { module.RequiresAuthentication(); using (ITransaction transaction = _store.BeginTransaction()) { var feedsCount = transaction.Query <IFeed>().Count(); var usersCount = transaction.Query <User>().Count(); var packagesCount = transaction.Query <IInternalPackage>().Count(); return(new { feedCount = feedsCount, userCount = usersCount, packageCount = packagesCount }); } }
public void AddPackage(IPackage package, bool isAbsoluteLatestVersion, bool isLatestVersion) { IInternalPackage localPackage; using (var transaction = _store.BeginTransaction()) { localPackage = InternalPackage.Create(_feedId, package, isAbsoluteLatestVersion, isLatestVersion); transaction.Insert(localPackage); transaction.Commit(); } _factory.AddFrameworkNames(localPackage.SupportedFrameworks); }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); IFeedConfiguration feedConfig; try { int feedId = int.Parse(parameters.id); feedConfig = module.Bind <FeedConfiguration>(); if (feedId != feedConfig.FeedId) { return(HttpStatusCode.BadRequest); } ITransaction transaction = _store.BeginTransaction(); var existingFeedExists = transaction.Query <IFeedConfiguration>().Where("FeedId = @feedId").Parameter("feedId", feedId).Count() > 0; if (!existingFeedExists) { return(HttpStatusCode.NotFound); } transaction.Update(feedConfig); transaction.Commit(); transaction.Dispose(); } catch (Exception ex) { return(HttpStatusCode.InternalServerError); } return(feedConfig); }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); using (ITransaction transaction = _store.BeginTransaction()) { int feedId = int.Parse(parameters.id); var feed = transaction.Query <IFeed>().Where("Id = @feedId").Parameter("feedId", feedId).First(); if (feed != null) { //Temporary until the API Key table is used if (!string.IsNullOrWhiteSpace(feed.ApiKeyHashed)) { feed.HasApiKey = true; } feed.ApiKeyHashed = null; //We don't want to expose this to the front end feed.ApiKeySalt = null; //We don't want to expose this to the front end } return(feed); } }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); IFeed feed; try { feed = module.Bind <Feed>(); ITransaction transaction = _store.BeginTransaction(); var existingFeedExists = transaction.Query <IFeed>().Where("Name = @feedName").Parameter("feedName", feed.Name).Count() > 0; if (existingFeedExists) { return(HttpStatusCode.Conflict); } if (!string.IsNullOrWhiteSpace(feed.ApiKey)) { ICryptoService cryptoService = new PBKDF2(); feed.ApiKeySalt = cryptoService.GenerateSalt(); feed.ApiKeyHashed = cryptoService.Compute(feed.ApiKey); } transaction.Insert(feed); transaction.Commit(); transaction.Dispose(); transaction = _store.BeginTransaction(); feed = transaction.Query <IFeed>() .Where("Name = @feedName") .Parameter("feedName", feed.Name) .First(); var appFolder = _home.InstallDirectory; var feedFolder = Path.Combine(appFolder, "Feeds", feed.Id.ToString()); IFeedConfiguration config = new FeedConfiguration { FeedId = feed.Id, PackagesDirectory = feedFolder }; transaction.Insert(config); transaction.Commit(); transaction.Dispose(); } catch (Exception ex) { return(HttpStatusCode.InternalServerError); } feed.ApiKeyHashed = null; //Temporary until API Key table is used feed.ApiKeySalt = null; //Temporary until API Key table is used return(feed); }
public dynamic Execute(dynamic parameters, INancyModule module) { string id = parameters.id; string version = parameters.version; string feedName = parameters.feed; int feedId; using (ITransaction transaction = _store.BeginTransaction()) { var feed = transaction.Query <IFeed>().Where("Name = @feedName").Parameter("feedName", feedName).First(); if (feed == null) { var response = module.Response.AsText("Feed does not exist."); response.StatusCode = HttpStatusCode.BadRequest; return(response); } feedId = feed.Id; } var packageRepository = _packageRepositoryFactory.Create(feedId); IInternalPackage package = packageRepository.GetPackage(id, new SemanticVersion(version)); if (package == null) { var response = module.Response.AsText(string.Format("Package {0} version {1} not found.", id, version)); response.StatusCode = HttpStatusCode.NotFound; return(response); } packageRepository.IncrementDownloadCount(package); Response result; var stream = packageRepository.GetPackageRaw(id, new SemanticVersion(version)); if (module.Request.Method == HttpMethod.Get.Method) { result = module.Response.FromStream(stream, "application/zip"); result.Headers.Add("Content-Length", stream.Length.ToString()); result.Headers.Add("Content-Disposition", "attachment; filename=" + package.PackageId + "." + package.Version + ".nupkg"); using (var md5 = MD5.Create()) { result.Headers.Add("Content-MD5", BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower()); stream.Seek(0, SeekOrigin.Begin); } } else { result = Response.NoBody; } result.StatusCode = HttpStatusCode.OK; return(result); }
/// <summary> /// Starts a transaction which can be used to group more queries together to be executed as one. /// </summary> /// <param name="isolationLevel">Isolation level used to lock the database.</param> /// <returns>A handle to the transaction.</returns> public ITransaction BeginTransaction(System.Data.IsolationLevel isolationLevel) { return(_store.BeginTransaction(isolationLevel)); }
public dynamic Execute(dynamic parameters, INancyModule module) { string id = parameters.id; string version = parameters.version; string feedName = parameters.feed; int feedId; IFeed feed; using (ITransaction transaction = _store.BeginTransaction()) { feed = transaction.Query <IFeed>().Where("Name = @feedName").Parameter("feedName", feedName).First(); if (feed == null) { var response = module.Response.AsText("Feed does not exist."); response.StatusCode = HttpStatusCode.BadRequest; return(response); } feedId = feed.Id; } if (!IsValidNuGetApiKey(module, feed)) { var response = module.Response.AsText("Invalid API key."); response.StatusCode = HttpStatusCode.Forbidden; return(response); } var packageRepository = _packageRepositoryFactory.Create(feedId); IInternalPackage package = packageRepository.GetPackage(id, new SemanticVersion(version)); if (package == null) { var response = module.Response.AsText(string.Format("Package {0} version {1} not found.", id, version)); response.StatusCode = HttpStatusCode.NotFound; return(response); } string deletedPackageId = package.PackageId; bool isDeletedPackageLatestVersion = package.IsLatestVersion; bool isDeletedPackageAbsoluteLatestVersion = package.IsAbsoluteLatestVersion; packageRepository.RemovePackage(package); if (isDeletedPackageAbsoluteLatestVersion || isDeletedPackageLatestVersion) { IInternalPackage latestAbsoluteVersionPackage; IInternalPackage latestVersionPackage; GetNextLatestVersionPackages(feedId, deletedPackageId, packageRepository, out latestAbsoluteVersionPackage, out latestVersionPackage); if (latestAbsoluteVersionPackage != null && !latestAbsoluteVersionPackage.IsAbsoluteLatestVersion) { latestAbsoluteVersionPackage.IsAbsoluteLatestVersion = true; using (ITransaction transaction = _store.BeginTransaction()) { transaction.Update(latestAbsoluteVersionPackage); transaction.Commit(); } } if (latestVersionPackage != null && !latestVersionPackage.IsLatestVersion) { latestVersionPackage.IsLatestVersion = true; using (ITransaction transaction = _store.BeginTransaction()) { transaction.Update(latestVersionPackage); transaction.Commit(); } } } return(new Response { StatusCode = HttpStatusCode.Created }); }
public dynamic Execute(dynamic parameters, INancyModule module) { module.RequiresAuthentication(); int feedId = int.Parse(parameters.id); IFeed feed; IFeedConfiguration config; List <IInternalPackage> packages; using (ITransaction transaction = _store.BeginTransaction()) { feed = transaction.Query <IFeed>() .Where("Id = @feedId") .Parameter("feedId", feedId) .First(); } if (feed == null) { return(HttpStatusCode.NotFound); } using (ITransaction transaction = _store.BeginTransaction()) { config = transaction.Query <IFeedConfiguration>() .Where("FeedId = @feedId") .Parameter("feedId", feedId) .First(); } using (ITransaction transaction = _store.BeginTransaction()) { packages = transaction.Query <IInternalPackage>() .Where("FeedId = @feedId") .Parameter("feedId", feedId) .ToList(); } string packageDirectory = config.PackagesDirectory; using (ITransaction transaction = _store.BeginTransaction()) { foreach (var package in packages) { transaction.Delete(package); } transaction.Delete(feed); transaction.Delete(config); transaction.Commit(); } if (Directory.Exists(packageDirectory)) { try { Directory.Delete(packageDirectory); } catch (Exception ex) { } } return(module.Response.AsJson(new object())); }