distribution/registry/storage/layerreader.go
David Lawrence 3853e66f4b don't panic during a request when configuring repository middleware. Return a 500 with an appropriate error
Signed-off-by: David Lawrence <david.lawrence@docker.com> (github: endophage)
2015-03-11 08:59:02 -07:00

45 lines
1 KiB
Go

package storage
import (
"net/http"
"time"
"github.com/docker/distribution"
"github.com/docker/distribution/digest"
)
// layerReader implements Layer and provides facilities for reading and
// seeking.
type layerReader struct {
fileReader
digest digest.Digest
}
var _ distribution.Layer = &layerReader{}
func (lr *layerReader) Digest() digest.Digest {
return lr.digest
}
func (lr *layerReader) Length() int64 {
return lr.size
}
func (lr *layerReader) CreatedAt() time.Time {
return lr.modtime
}
// Close the layer. Should be called when the resource is no longer needed.
func (lr *layerReader) Close() error {
return lr.closeWithErr(distribution.ErrLayerClosed)
}
func (lr *layerReader) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Docker-Content-Digest", lr.digest.String())
if url, err := lr.fileReader.driver.URLFor(lr.path, map[string]interface{}{}); err == nil {
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
http.ServeContent(w, r, lr.digest.String(), lr.CreatedAt(), lr)
}