Ejemplo n.º 1
0
        public virtual ActionResult PostCuratedPackages(
            string curatedFeedName,
            CreateCuratedPackageRequest request)
        {
            var curatedFeed = CuratedFeedService.GetFeedByName(curatedFeedName, includePackages: true);

            if (curatedFeed == null)
            {
                return(HttpNotFound());
            }

            if (curatedFeed.Managers.All(manager => manager.Username != User.Identity.Name))
            {
                return(new HttpStatusCodeResult(403));
            }

            if (!ModelState.IsValid)
            {
                ViewBag.CuratedFeedName = curatedFeed.Name;
                return(View("CreateCuratedPackageForm"));
            }

            var packageRegistration = EntitiesContext.PackageRegistrations
                                      .Where(pr => pr.Id == request.PackageId)
                                      .Include(pr => pr.Packages)
                                      .Include(pr => pr.Owners)
                                      .FirstOrDefault();

            if (packageRegistration == null)
            {
                ModelState.AddModelError("PackageId", Strings.PackageWithIdDoesNotExist);
                ViewBag.CuratedFeedName = curatedFeed.Name;
                return(View("CreateCuratedPackageForm"));
            }

            if (curatedFeed.Packages.Any(cp => cp.PackageRegistration.Key == packageRegistration.Key))
            {
                ModelState.AddModelError("PackageId", Strings.PackageIsAlreadyCurated);
                ViewBag.CuratedFeedName = curatedFeed.Name;
                return(View("CreateCuratedPackageForm"));
            }

            var packages = packageRegistration.Packages.ToList();

            foreach (var package in packages)
            {
                CuratedFeedService.CreatedCuratedPackage(
                    curatedFeed,
                    package,
                    included: true,
                    automaticallyCurated: false,
                    notes: request.Notes,
                    commitChanges: false);
            }
            CuratedFeedService.UpdateIsLatest(packageRegistration, false);
            EntitiesContext.SaveChanges();

            return(RedirectToRoute(RouteName.CuratedFeed, new { name = curatedFeed.Name }));
        }
Ejemplo n.º 2
0
        private async Task <ActionResult> CreatePackageInternal()
        {
            // Get the user
            var user = GetCurrentUser();

            using (var packageToPush = ReadPackageFromRequest())
            {
                if (packageToPush.Metadata.MinClientVersion > new Version("3.0.0.0"))
                {
                    return(new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, String.Format(
                                                                CultureInfo.CurrentCulture,
                                                                Strings.UploadPackage_MinClientVersionOutOfRange,
                                                                packageToPush.Metadata.MinClientVersion)));
                }

                // Ensure that the user can push packages for this partialId.
                var packageRegistration = PackageService.FindPackageRegistrationById(packageToPush.Metadata.Id);
                if (packageRegistration != null)
                {
                    if (!packageRegistration.IsOwner(user))
                    {
                        return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, Strings.ApiKeyNotAuthorized));
                    }

                    // Check if a particular Id-Version combination already exists. We eventually need to remove this check.
                    string normalizedVersion = packageToPush.Metadata.Version.ToNormalizedString();
                    bool   packageExists     =
                        packageRegistration.Packages.Any(
                            p => String.Equals(
                                p.NormalizedVersion,
                                normalizedVersion,
                                StringComparison.OrdinalIgnoreCase));

                    if (packageExists)
                    {
                        return(new HttpStatusCodeWithBodyResult(
                                   HttpStatusCode.Conflict,
                                   String.Format(CultureInfo.CurrentCulture, Strings.PackageExistsAndCannotBeModified,
                                                 packageToPush.Metadata.Id, packageToPush.Metadata.Version.ToNormalizedStringSafe())));
                    }
                }

                var package = PackageService.CreatePackage(packageToPush, user, commitChanges: false);
                AutoCuratePackage.Execute(package, packageToPush, commitChanges: false);
                EntitiesContext.SaveChanges();

                using (Stream uploadStream = packageToPush.GetStream())
                {
                    await PackageFileService.SavePackageFileAsync(package, uploadStream);

                    IndexingService.UpdatePackage(package);
                }
            }

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }
Ejemplo n.º 3
0
        public virtual ActionResult ReIndex(string curatedFeedName)
        {
            foreach (var package in EntitiesContext.PackageRegistrations
                     .SelectMany(registration => registration.Packages)
                     .Include(package => package.PackageRegistration)
                     .ToList())
            {
                AutoCuratedPackageCmd.Execute(package, null, false);
            }
            EntitiesContext.SaveChanges();

            return(RedirectToRoute(RouteName.CuratedFeed, new { name = curatedFeedName }));
        }
Ejemplo n.º 4
0
        private async Task <ActionResult> CreatePackageInternal()
        {
            // Get the user
            var user = GetCurrentUser();

            using (var packageStream = ReadPackageFromRequest())
                using (var packageToPush = new PackageReader(packageStream, leaveStreamOpen: false))
                {
                    NuspecReader nuspec = null;
                    try
                    {
                        nuspec = packageToPush.GetNuspecReader();
                    }
                    catch (Exception ex)
                    {
                        return(new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, string.Format(
                                                                    CultureInfo.CurrentCulture,
                                                                    Strings.UploadPackage_InvalidNuspec,
                                                                    ex.Message)));
                    }

                    if (nuspec.GetMinClientVersion() > Constants.MaxSupportedMinClientVersion)
                    {
                        return(new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, string.Format(
                                                                    CultureInfo.CurrentCulture,
                                                                    Strings.UploadPackage_MinClientVersionOutOfRange,
                                                                    nuspec.GetMinClientVersion())));
                    }

                    // Ensure that the user can push packages for this partialId.
                    var packageRegistration = PackageService.FindPackageRegistrationById(nuspec.GetId());
                    if (packageRegistration != null)
                    {
                        if (!packageRegistration.IsOwner(user))
                        {
                            return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, Strings.ApiKeyNotAuthorized));
                        }

                        // Check if a particular Id-Version combination already exists. We eventually need to remove this check.
                        string normalizedVersion = nuspec.GetVersion().ToNormalizedString();
                        bool   packageExists     =
                            packageRegistration.Packages.Any(
                                p => String.Equals(
                                    p.NormalizedVersion,
                                    normalizedVersion,
                                    StringComparison.OrdinalIgnoreCase));

                        if (packageExists)
                        {
                            return(new HttpStatusCodeWithBodyResult(
                                       HttpStatusCode.Conflict,
                                       String.Format(CultureInfo.CurrentCulture, Strings.PackageExistsAndCannotBeModified,
                                                     nuspec.GetId(), nuspec.GetVersion().ToNormalizedStringSafe())));
                        }
                    }

                    var packageStreamMetadata = new PackageStreamMetadata
                    {
                        HashAlgorithm = Constants.Sha512HashAlgorithmId,
                        Hash          = CryptographyService.GenerateHash(packageStream.AsSeekableStream()),
                        Size          = packageStream.Length,
                    };

                    var package = PackageService.CreatePackage(packageToPush, packageStreamMetadata, user, commitChanges: false);
                    AutoCuratePackage.Execute(package, packageToPush, commitChanges: false);
                    EntitiesContext.SaveChanges();

                    using (Stream uploadStream = packageStream)
                    {
                        uploadStream.Position = 0;
                        await PackageFileService.SavePackageFileAsync(package, uploadStream.AsSeekableStream());

                        IndexingService.UpdatePackage(package);
                    }
                }

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }
 public void CommitChanges()
 {
     entities.SaveChanges();
 }