Exemplo n.º 1
0
        public async Task <PackageApiModel> PostAsync(string packageName, string packageType, string configType, string version, List <string> tags, IFormFile package)
        {
            if (string.IsNullOrEmpty(packageType))
            {
                throw new InvalidInputException("Package Type must be provided");
            }

            bool isValidPackageType = Enum.TryParse(packageType, true, out PackageType uploadedPackageType);

            if (!isValidPackageType)
            {
                throw new InvalidInputException($"Provided packageType {packageType} is not valid.");
            }

            if (uploadedPackageType.Equals(PackageType.EdgeManifest) && !string.IsNullOrEmpty(configType))
            {
                throw new InvalidInputException($"Package of type EdgeManifest cannot have parameter " +
                                                $"configType.");
            }

            if (configType == null)
            {
                configType = string.Empty;
            }

            if (package == null || package.Length == 0 || string.IsNullOrEmpty(package.FileName))
            {
                throw new InvalidInputException("Package uploaded is missing or invalid.");
            }

            string packageContent;

            using (var streamReader = new StreamReader(package.OpenReadStream()))
            {
                packageContent = await streamReader.ReadToEndAsync();
            }

            if (!PackagesHelper.VerifyPackageType(packageContent, uploadedPackageType))
            {
                throw new InvalidInputException($@"Package uploaded is invalid. Package contents
                            do not match with the given package type {packageType}.");
            }

            var packageToAdd = new PackageApiModel(
                packageContent,
                !string.IsNullOrWhiteSpace(packageName) ? packageName : package.FileName,
                uploadedPackageType,
                version,
                configType,
                tags ?? new List <string>());

            return(new PackageApiModel(await this.storage.AddPackageAsync(packageToAdd.ToServiceModel(), this.GetClaimsUserDetails(), this.GetTenantId())));
        }
        public async Task <PackageApiModel> PostAsync(string packageType, string configType, IFormFile package)
        {
            if (string.IsNullOrEmpty(packageType))
            {
                throw new InvalidInputException("Package Type must be provided");
            }

            bool isValidPackageType = Enum.TryParse(packageType, true, out PackageType uploadedPackageType);

            if (!isValidPackageType)
            {
                throw new InvalidInputException($"Provided packageType {packageType} is not valid.");
            }

            if (uploadedPackageType.Equals(PackageType.EdgeManifest) &&
                !(string.IsNullOrEmpty(configType)))
            {
                throw new InvalidInputException($"Package of type EdgeManifest cannot have parameter " +
                                                $"configType.");
            }

            if (configType == null)
            {
                configType = string.Empty;
            }

            if (package == null || package.Length == 0 || string.IsNullOrEmpty(package.FileName))
            {
                throw new InvalidInputException("Package uploaded is missing or invalid.");
            }

            string packageContent;

            using (var streamReader = new StreamReader(package.OpenReadStream()))
            {
                packageContent = await streamReader.ReadToEndAsync();
            }

            var packageToAdd = new PackageApiModel(
                packageContent,
                package.FileName,
                uploadedPackageType,
                configType);

            return(new PackageApiModel(await this.storage.AddPackageAsync(packageToAdd.ToServiceModel())));
        }