cleanup code

This commit is contained in:
stefanodvx 2025-04-15 10:46:54 +02:00
parent 26452d0f53
commit c06c7958e2
10 changed files with 64 additions and 14 deletions

View file

@ -29,6 +29,7 @@ func DefaultConfig() *models.DownloadConfig {
RetryAttempts: 3,
RetryDelay: 2 * time.Second,
Remux: true,
MaxInMemory: 50 * 1024 * 1024, // 50MB
}
}
@ -127,7 +128,10 @@ func DownloadFileInMemory(
case <-ctx.Done():
return nil, ctx.Err()
default:
data, err := downloadInMemory(ctx, fileURL, config.Timeout)
data, err := downloadInMemory(
ctx, fileURL,
config,
)
if err != nil {
errs = append(errs, err)
continue
@ -142,9 +146,12 @@ func DownloadFileInMemory(
func downloadInMemory(
ctx context.Context,
fileURL string,
timeout time.Duration,
config *models.DownloadConfig,
) ([]byte, error) {
reqCtx, cancel := context.WithTimeout(ctx, timeout)
reqCtx, cancel := context.WithTimeout(
ctx,
config.Timeout,
)
defer cancel()
select {
@ -169,6 +176,10 @@ func downloadInMemory(
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
if resp.ContentLength > int64(config.MaxInMemory) {
return nil, fmt.Errorf("file too large for in-memory download: %d bytes", resp.ContentLength)
}
var buf bytes.Buffer
if resp.ContentLength > 0 {
buf.Grow(int(resp.ContentLength))

View file

@ -25,13 +25,15 @@ func GetHTTPSession() *http.Client {
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 10,
MaxIdleConnsPerHost: 20,
MaxConnsPerHost: 20,
ResponseHeaderTimeout: 30 * time.Second,
DisableCompression: false,
}
httpSession = &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
Timeout: 60 * time.Second,
}
})
return httpSession

View file

@ -105,9 +105,6 @@ func DetectImageFormat(file io.ReadSeeker) (string, error) {
}
func isHEIF(header []byte) bool {
if len(header) < 12 {
return false
}
isHeifHeader := header[0] == 0x00 && header[1] == 0x00 &&
header[2] == 0x00 && (header[3] == 0x18 || header[3] == 0x1C) &&
bytes.Equal(header[4:8], []byte("ftyp"))