Adds capabilities to import a .zip file with importZip.
Detects the content type of source by checking bytes & DetectContentType.
Adds LimitedReader reader, a fork of io.LimitedReader,
was needed for better error messaging instead of just getting back EOF.
We are using limited reader to avoid very big files causing memory issues.
Adds a new file size limit for context imports,
this limit is used for the main file for .zip & .tar and individual compressed
files for .zip.
Added TestImportZip that will check the import content type
Then will assert no err on Importing .zip file
Signed-off-by: Goksu Toprak <goksu.toprak@docker.com>
(cherry picked from commit 291e86289b)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
30 lines
602 B
Go
30 lines
602 B
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
// LimitedReader is a fork of io.LimitedReader to override Read.
|
|
type LimitedReader struct {
|
|
R io.Reader
|
|
N int64 // max bytes remaining
|
|
}
|
|
|
|
// Read is a fork of io.LimitedReader.Read that returns an error when limit exceeded.
|
|
func (l *LimitedReader) Read(p []byte) (n int, err error) {
|
|
if l.N < 0 {
|
|
return 0, errors.New("read exceeds the defined limit")
|
|
}
|
|
if l.N == 0 {
|
|
return 0, io.EOF
|
|
}
|
|
// have to cap N + 1 otherwise we won't hit limit err
|
|
if int64(len(p)) > l.N+1 {
|
|
p = p[0 : l.N+1]
|
|
}
|
|
n, err = l.R.Read(p)
|
|
l.N -= int64(n)
|
|
return n, err
|
|
}
|