mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-08 19:30:35 +02:00
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package imagedata
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
"github.com/imgproxy/imgproxy/v3/fetcher"
|
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
|
"github.com/imgproxy/imgproxy/v3/transport"
|
|
)
|
|
|
|
var (
|
|
Fetcher *fetcher.Fetcher
|
|
|
|
// For tests. This needs to move to fetcher once we will have a way to isolate
|
|
// the fetcher in tests.
|
|
redirectAllRequestsTo string
|
|
)
|
|
|
|
type DownloadOptions struct {
|
|
Header http.Header
|
|
CookieJar http.CookieJar
|
|
MaxSrcFileSize int
|
|
DownloadFinished context.CancelFunc
|
|
}
|
|
|
|
func DefaultDownloadOptions() DownloadOptions {
|
|
return DownloadOptions{
|
|
Header: nil,
|
|
CookieJar: nil,
|
|
MaxSrcFileSize: config.MaxSrcFileSize,
|
|
DownloadFinished: nil,
|
|
}
|
|
}
|
|
|
|
func initDownloading() error {
|
|
trc, err := transport.LoadFromEnv(transport.NewDefaultConfig())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ts, err := transport.New(trc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c, err := fetcher.LoadFromEnv(fetcher.NewDefaultConfig())
|
|
if err != nil {
|
|
return ierrors.Wrap(err, 0, ierrors.WithPrefix("configuration error"))
|
|
}
|
|
|
|
Fetcher, err = fetcher.NewFetcher(ts, c)
|
|
if err != nil {
|
|
return ierrors.Wrap(err, 0, ierrors.WithPrefix("can't create image fetcher"))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func RedirectAllRequestsTo(u string) {
|
|
redirectAllRequestsTo = u
|
|
}
|
|
|
|
func StopRedirectingRequests() {
|
|
redirectAllRequestsTo = ""
|
|
}
|