1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00
imgproxy/watermark_data.go

101 lines
2.1 KiB
Go
Raw Normal View History

2018-10-19 15:47:11 +06:00
package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
)
2019-08-28 17:08:53 +06:00
type watermarkData struct {
data []byte
imgtype imageType
}
func getWatermarkData() (*watermarkData, error) {
2018-10-19 15:47:11 +06:00
if len(conf.WatermarkData) > 0 {
data, imgtype, err := base64WatermarkData()
2019-08-28 17:08:53 +06:00
if err != nil {
return nil, err
}
return &watermarkData{data, imgtype}, err
2018-10-19 15:47:11 +06:00
}
if len(conf.WatermarkPath) > 0 {
data, imgtype, err := fileWatermarkData()
2019-08-28 17:08:53 +06:00
if err != nil {
return nil, err
}
return &watermarkData{data, imgtype}, err
2018-10-19 15:47:11 +06:00
}
if len(conf.WatermarkURL) > 0 {
2019-08-28 17:08:53 +06:00
b, imgtype, cancel, err := remoteWatermarkData()
defer cancel()
if err != nil {
return nil, err
}
data := make([]byte, len(b))
copy(data, b)
return &watermarkData{data, imgtype}, err
2018-10-19 15:47:11 +06:00
}
2019-08-28 17:08:53 +06:00
return nil, nil
2018-10-19 15:47:11 +06:00
}
func base64WatermarkData() ([]byte, imageType, error) {
data, err := base64.StdEncoding.DecodeString(conf.WatermarkData)
if err != nil {
return nil, imageTypeUnknown, fmt.Errorf("Can't decode watermark data: %s", err)
}
imgtype, err := checkTypeAndDimensions(bytes.NewReader(data))
if err != nil {
return nil, imageTypeUnknown, fmt.Errorf("Can't decode watermark: %s", err)
}
return data, imgtype, nil
}
func fileWatermarkData() ([]byte, imageType, error) {
f, err := os.Open(conf.WatermarkPath)
if err != nil {
return nil, imageTypeUnknown, fmt.Errorf("Can't read watermark: %s", err)
}
imgtype, err := checkTypeAndDimensions(f)
if err != nil {
return nil, imageTypeUnknown, fmt.Errorf("Can't decode watermark: %s", err)
}
// Return to the beginning of the file
f.Seek(0, 0)
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, imageTypeUnknown, fmt.Errorf("Can't read watermark: %s", err)
}
return data, imgtype, nil
}
func remoteWatermarkData() ([]byte, imageType, context.CancelFunc, error) {
ctx := context.WithValue(context.Background(), imageURLCtxKey, conf.WatermarkURL)
ctx, cancel, err := downloadImage(ctx)
if err != nil {
return nil, imageTypeUnknown, cancel, fmt.Errorf("Can't download watermark: %s", err)
}
return getImageData(ctx).Bytes(), getImageType(ctx), cancel, err
}