mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
feat: return better error if file size is too big to upload (#7775)
* feat: return better error if file size is too big to upload * Use a limit writer to capture actual tar size
This commit is contained in:
43
coderd/util/xio/limitwriter.go
Normal file
43
coderd/util/xio/limitwriter.go
Normal file
@ -0,0 +1,43 @@
|
||||
package xio
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
var ErrLimitReached = xerrors.Errorf("i/o limit reached")
|
||||
|
||||
// LimitWriter will only write bytes to the underlying writer until the limit is reached.
|
||||
type LimitWriter struct {
|
||||
Limit int64
|
||||
N int64
|
||||
W io.Writer
|
||||
}
|
||||
|
||||
func NewLimitWriter(w io.Writer, n int64) *LimitWriter {
|
||||
// If anyone tries this, just make a 0 writer.
|
||||
if n < 0 {
|
||||
n = 0
|
||||
}
|
||||
return &LimitWriter{
|
||||
Limit: n,
|
||||
N: 0,
|
||||
W: w,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LimitWriter) Write(p []byte) (int, error) {
|
||||
if l.N >= l.Limit {
|
||||
return 0, ErrLimitReached
|
||||
}
|
||||
|
||||
// Write 0 bytes if the limit is to be exceeded.
|
||||
if int64(len(p)) > l.Limit-l.N {
|
||||
return 0, ErrLimitReached
|
||||
}
|
||||
|
||||
n, err := l.W.Write(p)
|
||||
l.N += int64(n)
|
||||
return n, err
|
||||
}
|
Reference in New Issue
Block a user