mirror of
https://github.com/sbondCo/Watcharr.git
synced 2026-08-07 07:14:44 +00:00
4cc9bd3edb
- Create a better (more) re-usable flow for downloading images to disk and inserting images into the db - Validate images properly by reading them fully and using DecodeConfig - Fix some security issues by re-encoding images - Always outputs one format `jpeg`, which keeps everything "normalized" and adds compression to images (mainly user uploads which might be big).
19 lines
368 B
Go
19 lines
368 B
Go
package util
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
func LimitedReadAll(r io.Reader, maxSize int64) ([]byte, error) {
|
|
// LimitReader: Allow reading one extra byte so our maxSize check later works.
|
|
b, err := io.ReadAll(io.LimitReader(r, maxSize+1))
|
|
if err != nil {
|
|
return b, err
|
|
}
|
|
if int64(len(b)) > maxSize {
|
|
return b, errors.New("file is too big")
|
|
}
|
|
return b, nil
|
|
}
|