2014-11-18 00:29:42 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-03-03 16:57:52 +00:00
|
|
|
"net/http"
|
2014-11-18 00:29:42 +00:00
|
|
|
"time"
|
2014-11-19 22:39:32 +00:00
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
2014-11-18 00:29:42 +00:00
|
|
|
)
|
|
|
|
|
2015-03-03 16:57:52 +00:00
|
|
|
// LayerRead implements Layer and provides facilities for reading and
|
2014-11-18 00:29:42 +00:00
|
|
|
// seeking.
|
|
|
|
type layerReader struct {
|
2014-11-21 01:49:35 +00:00
|
|
|
fileReader
|
2014-11-18 00:29:42 +00:00
|
|
|
|
2014-12-05 04:55:59 +00:00
|
|
|
digest digest.Digest
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
var _ distribution.Layer = &layerReader{}
|
2014-11-18 00:29:42 +00:00
|
|
|
|
2015-03-03 16:57:52 +00:00
|
|
|
func (lrs *layerReader) Path() string {
|
|
|
|
return lrs.path
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:39:32 +00:00
|
|
|
func (lrs *layerReader) Digest() digest.Digest {
|
|
|
|
return lrs.digest
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 04:57:14 +00:00
|
|
|
func (lrs *layerReader) Length() int64 {
|
|
|
|
return lrs.size
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:29:42 +00:00
|
|
|
func (lrs *layerReader) CreatedAt() time.Time {
|
2014-12-05 04:55:59 +00:00
|
|
|
return lrs.modtime
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
2015-02-12 00:49:49 +00:00
|
|
|
|
|
|
|
// Close the layer. Should be called when the resource is no longer needed.
|
|
|
|
func (lrs *layerReader) Close() error {
|
|
|
|
return lrs.closeWithErr(distribution.ErrLayerClosed)
|
|
|
|
}
|
2015-03-03 16:57:52 +00:00
|
|
|
|
|
|
|
func (lrs *layerReader) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Docker-Content-Digest", lrs.digest.String())
|
|
|
|
|
|
|
|
if url, err := lrs.fileReader.driver.URLFor(lrs.Path(), map[string]interface{}{}); err == nil {
|
|
|
|
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, lrs.Digest().String(), lrs.CreatedAt(), lrs)
|
|
|
|
}
|