示例#1
0
        private void AddPackage()
        {
            var customers = _userService.GetAllCustomers();

            if (customers.Count < 1)
            {
                Console.WriteLine();
                _ioHelper.WriteString("It is not possible to add package - minimum 1 customer needed");
                return;
            }

            var customerId = _ioHelperAddPackage.SelectCustomerId(customers);
            var package    = _ioHelperAddPackage.CreateNewPackage(customerId);

            try
            {
                _packageService.AddAsync(package).Wait();

                Console.WriteLine("The new package has been added!");
                Console.WriteLine();
            }
            catch (Exception)
            {
                Console.WriteLine("The given address does not exist. Try again...");
                return;
            }
        }
示例#2
0
        private async Task IndexPackageAsync(
            Package package,
            Stream readmeStreamOrNull,
            CancellationToken cancellationToken)
        {
            var packageId      = package.Id.ToLowerInvariant();
            var packageVersion = package.Version.ToNormalizedString().ToLowerInvariant();

            if (readmeStreamOrNull != null)
            {
                _logger.LogInformation(
                    "Uploading readme for package {PackageId} {PackageVersion}...",
                    packageId,
                    packageVersion);

                var blob = _blobContainer.GetBlockBlobReference($"v3/package/{packageId}/{packageVersion}/readme");

                blob.Properties.ContentType  = "text/markdown";
                blob.Properties.CacheControl = "max-age=120, must-revalidate";

                await blob.UploadFromStreamAsync(readmeStreamOrNull, cancellationToken);

                _logger.LogInformation(
                    "Uploaded readme for package {PackageId} {PackageVersion}",
                    packageId,
                    packageVersion);
            }

            // Try to add the package to the database. If the package already exists,
            // the add operation will fail and we will update the package's metadata instead.
            var addResult = await _packages.AddAsync(package);

            switch (addResult)
            {
            case PackageAddResult.Success:
                _logger.LogInformation(
                    "Added package {PackageId} {PackageVersion} to database",
                    packageId,
                    packageVersion);
                break;

            // The package already exists. Update its metadata instead.
            case PackageAddResult.PackageAlreadyExists:
                await UpdatePackageMetadata(package, cancellationToken);

                break;

            default:
                throw new NotSupportedException($"Unknown add result '{addResult}'");
            }

            // Lastly, use the database to update static resources and the search service.
            await _indexer.BuildAsync(package.Id);
        }
        public async Task <ActionResult> Create(PackageResource packageResource)
        {
            if (ModelState.IsValid)
            {
                var packages = Mapper.Map <PackageResource, Package>(packageResource);
                var userid   = System.Web.HttpContext.Current.User.Identity.GetUserId();
                packages.CreatedDate = DateTime.Now;
                packages.UpdatedDate = DateTime.Now;
                packages.UserId      = userid;
                await _packageService.AddAsync(packages);

                _packageService.UnitOfWorkSaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(packageResource));
        }
示例#4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            CreatePackageRequest.UserName = HttpContext.User?.Identity.Name ?? "anonymous";

            var response = await PackageService.AddAsync(CreatePackageRequest);

            Message = response.Message;

            if (!response.Success)
            {
                return(Page());
            }

            return(RedirectToPage("./Index"));
        }
示例#5
0
        public async Task <PackageIndexingResult> IndexAsync(Stream packageStream, CancellationToken cancellationToken)
        {
            // Try to extract all the necessary information from the package.
            Package package;
            Stream  nuspecStream;
            Stream  readmeStream;

            try
            {
                using (var packageReader = new PackageArchiveReader(packageStream, leaveStreamOpen: true))
                {
                    package      = GetPackageMetadata(packageReader);
                    nuspecStream = await packageReader.GetNuspecAsync(cancellationToken);

                    if (package.HasReadme)
                    {
                        readmeStream = await packageReader.GetReadmeAsync(cancellationToken);
                    }
                    else
                    {
                        readmeStream = null;
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Uploaded package is invalid");

                return(PackageIndexingResult.InvalidPackage);
            }

            // The package is well-formed. Ensure this is a new package.
            if (await _packages.ExistsAsync(package.Id, package.Version))
            {
                if (!_options.Value.AllowPackageOverwrites)
                {
                    return(PackageIndexingResult.PackageAlreadyExists);
                }

                await _packages.HardDeletePackageAsync(package.Id, package.Version);

                await _storage.DeleteAsync(package.Id, package.Version, cancellationToken);
            }

            // TODO: Add more package validations
            // TODO: Call PackageArchiveReader.ValidatePackageEntriesAsync
            _logger.LogInformation(
                "Validated package {PackageId} {PackageVersion}, persisting content to storage...",
                package.Id,
                package.VersionString);

            try
            {
                packageStream.Position = 0;

                await _storage.SavePackageContentAsync(
                    package,
                    packageStream,
                    nuspecStream,
                    readmeStream,
                    cancellationToken);
            }
            catch (Exception e)
            {
                // This may happen due to concurrent pushes.
                // TODO: Make IPackageStorageService.SavePackageContentAsync return a result enum so this
                // can be properly handled.
                _logger.LogError(
                    e,
                    "Failed to persist package {PackageId} {PackageVersion} content to storage",
                    package.Id,
                    package.VersionString);

                throw;
            }

            _logger.LogInformation(
                "Persisted package {Id} {Version} content to storage, saving metadata to database...",
                package.Id,
                package.VersionString);

            var result = await _packages.AddAsync(package);

            if (result == PackageAddResult.PackageAlreadyExists)
            {
                _logger.LogWarning(
                    "Package {Id} {Version} metadata already exists in database",
                    package.Id,
                    package.VersionString);

                return(PackageIndexingResult.PackageAlreadyExists);
            }

            if (result != PackageAddResult.Success)
            {
                _logger.LogError($"Unknown {nameof(PackageAddResult)} value: {{PackageAddResult}}", result);

                throw new InvalidOperationException($"Unknown {nameof(PackageAddResult)} value: {result}");
            }

            _logger.LogInformation(
                "Successfully persisted package {Id} {Version} metadata to database. Indexing in search...",
                package.Id,
                package.VersionString);

            await _search.IndexAsync(package);

            _logger.LogInformation(
                "Successfully indexed package {Id} {Version} in search",
                package.Id,
                package.VersionString);

            return(PackageIndexingResult.Success);
        }
示例#6
0
        public async Task <IndexingResult> IndexAsync(Stream stream)
        {
            // Try to save the package stream to storage.
            // TODO: On exception, roll back storage save.
            Package package;

            try
            {
                using (var packageReader = new PackageArchiveReader(stream))
                {
                    var packageId      = packageReader.NuspecReader.GetId();
                    var packageVersion = packageReader.NuspecReader.GetVersion();

                    if (await _packages.ExistsAsync(packageId, packageVersion))
                    {
                        return(IndexingResult.PackageAlreadyExists);
                    }

                    try
                    {
                        _logger.LogInformation(
                            "Persisting package {Id} {Version} content to storage...",
                            packageId,
                            packageVersion.ToNormalizedString());

                        await _storage.SavePackageStreamAsync(packageReader, stream);
                    }
                    catch (Exception e)
                    {
                        // This may happen due to concurrent pushes.
                        // TODO: Make IStorageService.SaveAsync return a result enum so this can be properly handled.
                        _logger.LogError(e, "Failed to save package {Id} {Version}", packageId, packageVersion.ToNormalizedString());

                        throw;
                    }

                    try
                    {
                        package = GetPackageMetadata(packageReader);
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(
                            e,
                            "Failed to extract metadata for package {Id} {Version}",
                            packageId,
                            packageVersion.ToNormalizedString());

                        throw;
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Uploaded package is invalid or the package already existed in storage");

                return(IndexingResult.InvalidPackage);
            }

            // The package stream has been stored. Persist the package's metadata to the database.
            _logger.LogInformation(
                "Persisting package {Id} {Version} metadata to database...",
                package.Id,
                package.VersionString);

            var result = await _packages.AddAsync(package);

            switch (result)
            {
            case PackageAddResult.Success:
                _logger.LogInformation(
                    "Successfully persisted package {Id} {Version} metadata to database. Indexing in search...",
                    package.Id,
                    package.VersionString);

                await _search.IndexAsync(package);

                _logger.LogInformation(
                    "Successfully indexed package {Id} {Version} in search",
                    package.Id,
                    package.VersionString);

                return(IndexingResult.Success);

            case PackageAddResult.PackageAlreadyExists:
                _logger.LogWarning(
                    "Package {Id} {Version} metadata already exists in database",
                    package.Id,
                    package.VersionString);

                return(IndexingResult.PackageAlreadyExists);

            default:
                _logger.LogError($"Unknown {nameof(PackageAddResult)} value: {{PackageAddResult}}", result);

                throw new InvalidOperationException($"Unknown {nameof(PackageAddResult)} value: {result}");
            }
        }
 public async Task PostPackageAsync([FromBody] Package package)
 {
     await _packageService.AddAsync(package);
 }
示例#8
0
 public Task <PackageAddResult> AddAsync(Package package) => _localPackages.AddAsync(package);