Exemplo n.º 1
0
        public async Task <IActionResult> GetFilesAsync(string repository, string digest, string targets)
        {
            if (string.IsNullOrEmpty(digest))
            {
                return(BadRequest("An image digest is required."));
            }
            if (!digest.IsDigest())
            {
                return(BadRequest("Digest appears invalid."));
            }

            try
            {
                if (string.IsNullOrEmpty(RegistryCredentials.Registry))
                {
                    return(BadRequest("Session is missing registry information. Try creating a new session."));
                }

                var targetList = string.IsNullOrEmpty(targets) ? Array.Empty <string>() : targets.Split(';');

                var client   = clientFactory.GetClient(AuthHandler);
                var imageSet = await client.GetImageSetAsync(repository, digest);

                if (imageSet.Images.Count() != 1)
                {
                    return(NotFound("No image was found with the given digest."));
                }

                // if we have a complete index, return it
                if (indexStore.IndexExists(digest))
                {
                    return(Ok(indexStore.GetIndex(digest)));
                }
                // otherwise, if they've requested a targeted index, look for that
                else if (indexStore.IndexExists(digest, targetList))
                {
                    return(Ok(indexStore.GetIndex(digest, targetList)));
                }
                // no dice, queue an indexing request
                else
                {
                    var request = new IndexRequest
                    {
                        Authorization = Request.Headers["Authorization"],
                        CreatedTime   = DateTime.UtcNow,
                        TargetRepo    = repository,
                        TargetDigest  = digest,
                        TargetPaths   = targetList
                    };
                    if (!indexQueue.Contains(request))
                    {
                        indexQueue.Push(request);
                    }

                    return(StatusCode(202, new { Status = RequestStatus.Pending }));
                }
            }
            catch (RedisConnectionException)
            {
                return(StatusCode(503, "Cannot access cache"));
            }
            catch (Client.NotFoundException)
            {
                return(NotFound());
            }
            catch (Client.AuthenticationException)
            {
                return(Unauthorized());
            }
        }