Esempio n. 1
0
        private async Task <IActionResult> TryServePackageFileAsync(string package, PackageFile packageFile, string version = null)
        {
            if (string.IsNullOrWhiteSpace(package))
            {
                return(BadRequest(new { Message = "Package name required." }));
            }

            // FIXME: duplicate code
            if (Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag))
            {
                var resources = await _packages.GetAvailablePackagesAsync(CancellationToken);

                foreach (var hash in etag)
                {
                    if (resources.Contains(hash))
                    {
                        return(StatusCode((int)HttpStatusCode.NotModified));
                    }
                }
            }

            var(error, buildHash) = await TryGetBuildHash(version);

            if (error != null)
            {
                return(error);
            }

            var assembly = await _packages.FindAssemblyByNameAsync(package, CancellationToken);

            if (assembly == null)
            {
                return(NotFound(new { Message = $"No package assemblies found matching name '{package}." }));
            }

            var packageHash = assembly.ComputePackageHash(buildHash);

            var packages = await _packages.GetAvailablePackagesAsync(CancellationToken);

            if (packages.Contains(packageHash))
            {
                var file = await ServePackageFileAsync(packageHash, packageFile, CancellationToken);

                if (!(file is NotFoundObjectResult))
                {
                    return(file);                    // try to re-compile if we ran into an issue earlier
                }
            }

            var result = await _packages.CompilePackageAsync(assembly, buildHash, CancellationToken);

            if (!result.Successful)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { Message = "Package compile error." }));
            }

            return(await ServePackageFileAsync(packageHash, packageFile, CancellationToken));
        }