Update to use blob interfaces
Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
parent
6bf4c45e52
commit
67e2e83434
5 changed files with 204 additions and 199 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -55,8 +56,8 @@ func (r *repository) Name() string {
|
|||
return r.name
|
||||
}
|
||||
|
||||
func (r *repository) Layers() distribution.LayerService {
|
||||
return &layers{
|
||||
func (r *repository) Blobs(ctx context.Context) distribution.BlobService {
|
||||
return &blobs{
|
||||
repository: r,
|
||||
}
|
||||
}
|
||||
|
@ -229,7 +230,7 @@ func (ms *manifests) Delete(dgst digest.Digest) error {
|
|||
}
|
||||
}
|
||||
|
||||
type layers struct {
|
||||
type blobs struct {
|
||||
*repository
|
||||
}
|
||||
|
||||
|
@ -254,25 +255,55 @@ func sanitizeLocation(location, source string) (string, error) {
|
|||
return location, nil
|
||||
}
|
||||
|
||||
func (ls *layers) Exists(dgst digest.Digest) (bool, error) {
|
||||
_, err := ls.fetchLayer(dgst)
|
||||
func (ls *blobs) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) {
|
||||
desc, err := ls.Stat(ctx, dgst)
|
||||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case distribution.ErrUnknownLayer:
|
||||
return false, nil
|
||||
default:
|
||||
return false, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
reader, err := ls.Open(ctx, desc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
return ioutil.ReadAll(reader)
|
||||
}
|
||||
|
||||
func (ls *blobs) Open(ctx context.Context, desc distribution.Descriptor) (distribution.ReadSeekCloser, error) {
|
||||
return &httpBlob{
|
||||
repository: ls.repository,
|
||||
desc: desc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ls *blobs) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, desc distribution.Descriptor) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ls *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
|
||||
writer, err := ls.Writer(ctx)
|
||||
if err != nil {
|
||||
return distribution.Descriptor{}, err
|
||||
}
|
||||
dgstr := digest.NewCanonicalDigester()
|
||||
n, err := io.Copy(writer, io.TeeReader(bytes.NewReader(p), dgstr))
|
||||
if err != nil {
|
||||
return distribution.Descriptor{}, err
|
||||
}
|
||||
if n < int64(len(p)) {
|
||||
return distribution.Descriptor{}, fmt.Errorf("short copy: wrote %d of %d", n, len(p))
|
||||
}
|
||||
|
||||
return true, nil
|
||||
desc := distribution.Descriptor{
|
||||
MediaType: mediaType,
|
||||
Length: int64(len(p)),
|
||||
Digest: dgstr.Digest(),
|
||||
}
|
||||
|
||||
return writer.Commit(ctx, desc)
|
||||
}
|
||||
|
||||
func (ls *layers) Fetch(dgst digest.Digest) (distribution.Layer, error) {
|
||||
return ls.fetchLayer(dgst)
|
||||
}
|
||||
|
||||
func (ls *layers) Upload() (distribution.LayerUpload, error) {
|
||||
func (ls *blobs) Writer(ctx context.Context) (distribution.BlobWriter, error) {
|
||||
u, err := ls.ub.BuildBlobUploadURL(ls.name)
|
||||
|
||||
resp, err := ls.client.Post(u, "", nil)
|
||||
|
@ -290,7 +321,7 @@ func (ls *layers) Upload() (distribution.LayerUpload, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &httpLayerUpload{
|
||||
return &httpBlobUpload{
|
||||
repo: ls.repository,
|
||||
client: ls.client,
|
||||
uuid: uuid,
|
||||
|
@ -302,19 +333,19 @@ func (ls *layers) Upload() (distribution.LayerUpload, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ls *layers) Resume(uuid string) (distribution.LayerUpload, error) {
|
||||
func (ls *blobs) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (ls *layers) fetchLayer(dgst digest.Digest) (distribution.Layer, error) {
|
||||
func (ls *blobs) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
||||
u, err := ls.ub.BuildBlobURL(ls.name, dgst)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return distribution.Descriptor{}, err
|
||||
}
|
||||
|
||||
resp, err := ls.client.Head(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return distribution.Descriptor{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
@ -323,31 +354,17 @@ func (ls *layers) fetchLayer(dgst digest.Digest) (distribution.Layer, error) {
|
|||
lengthHeader := resp.Header.Get("Content-Length")
|
||||
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing content-length: %v", err)
|
||||
return distribution.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err)
|
||||
}
|
||||
|
||||
var t time.Time
|
||||
lastModified := resp.Header.Get("Last-Modified")
|
||||
if lastModified != "" {
|
||||
t, err = http.ParseTime(lastModified)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing last-modified: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &httpLayer{
|
||||
layers: ls,
|
||||
size: length,
|
||||
digest: dgst,
|
||||
createdAt: t,
|
||||
return distribution.Descriptor{
|
||||
MediaType: resp.Header.Get("Content-Type"),
|
||||
Length: length,
|
||||
Digest: dgst,
|
||||
}, nil
|
||||
case resp.StatusCode == http.StatusNotFound:
|
||||
return nil, distribution.ErrUnknownLayer{
|
||||
FSLayer: manifest.FSLayer{
|
||||
BlobSum: dgst,
|
||||
},
|
||||
}
|
||||
return distribution.Descriptor{}, distribution.ErrBlobUnknown
|
||||
default:
|
||||
return nil, handleErrorResponse(resp)
|
||||
return distribution.Descriptor{}, handleErrorResponse(resp)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue