2015-02-11 02:41:09 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-03-03 17:57:52 +01:00
|
|
|
"net/http"
|
2015-02-11 02:41:09 +01:00
|
|
|
"time"
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
"github.com/docker/distribution"
|
2015-02-11 02:41:09 +01:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
)
|
|
|
|
|
2015-03-06 18:48:25 +01:00
|
|
|
// layerReader implements Layer and provides facilities for reading and
|
2015-02-11 02:41:09 +01:00
|
|
|
// seeking.
|
|
|
|
type layerReader struct {
|
|
|
|
fileReader
|
|
|
|
|
|
|
|
digest digest.Digest
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
var _ distribution.Layer = &layerReader{}
|
2015-02-11 02:41:09 +01:00
|
|
|
|
2015-03-06 18:48:25 +01:00
|
|
|
func (lr *layerReader) Digest() digest.Digest {
|
|
|
|
return lr.digest
|
2015-02-11 02:41:09 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 18:48:25 +01:00
|
|
|
func (lr *layerReader) Length() int64 {
|
|
|
|
return lr.size
|
2015-03-05 05:57:14 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 18:48:25 +01:00
|
|
|
func (lr *layerReader) CreatedAt() time.Time {
|
|
|
|
return lr.modtime
|
2015-02-11 02:41:09 +01:00
|
|
|
}
|
2015-02-12 01:49:49 +01:00
|
|
|
|
|
|
|
// Close the layer. Should be called when the resource is no longer needed.
|
2015-03-06 18:48:25 +01:00
|
|
|
func (lr *layerReader) Close() error {
|
|
|
|
return lr.closeWithErr(distribution.ErrLayerClosed)
|
2015-02-12 01:49:49 +01:00
|
|
|
}
|
2015-03-03 17:57:52 +01:00
|
|
|
|
2015-03-06 18:48:25 +01:00
|
|
|
func (lr *layerReader) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Docker-Content-Digest", lr.digest.String())
|
2015-03-03 17:57:52 +01:00
|
|
|
|
2015-03-13 01:06:40 +01:00
|
|
|
if url, err := lr.fileReader.driver.URLFor(lr.path, map[string]interface{}{"method": r.Method}); err == nil {
|
2015-03-03 17:57:52 +01:00
|
|
|
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
|
|
|
}
|
2015-03-06 18:48:25 +01:00
|
|
|
http.ServeContent(w, r, lr.digest.String(), lr.CreatedAt(), lr)
|
2015-03-03 17:57:52 +01:00
|
|
|
}
|