1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/watermark_data.go

75 lines
1.6 KiB
Go
Raw Normal View History

2018-10-19 11:47:11 +02:00
package main
import (
"bytes"
"encoding/base64"
"fmt"
"os"
)
func getWatermarkData() (*imageData, error) {
2018-10-19 11:47:11 +02:00
if len(conf.WatermarkData) > 0 {
return base64WatermarkData(conf.WatermarkData)
2018-10-19 11:47:11 +02:00
}
if len(conf.WatermarkPath) > 0 {
return fileWatermarkData(conf.WatermarkPath)
2018-10-19 11:47:11 +02:00
}
if len(conf.WatermarkURL) > 0 {
return remoteWatermarkData(conf.WatermarkURL)
2018-10-19 11:47:11 +02:00
}
2019-08-28 13:08:53 +02:00
return nil, nil
2018-10-19 11:47:11 +02:00
}
func base64WatermarkData(encoded string) (*imageData, error) {
data, err := base64.StdEncoding.DecodeString(encoded)
2018-10-19 11:47:11 +02:00
if err != nil {
return nil, fmt.Errorf("Can't decode watermark data: %s", err)
2018-10-19 11:47:11 +02:00
}
imgtype, err := checkTypeAndDimensions(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("Can't decode watermark: %s", err)
2018-10-19 11:47:11 +02:00
}
return &imageData{Data: data, Type: imgtype}, nil
2018-10-19 11:47:11 +02:00
}
func fileWatermarkData(path string) (*imageData, error) {
f, err := os.Open(path)
2018-10-19 11:47:11 +02:00
if err != nil {
return nil, fmt.Errorf("Can't read watermark: %s", err)
2018-10-19 11:47:11 +02:00
}
fi, err := f.Stat()
2018-10-19 11:47:11 +02:00
if err != nil {
return nil, fmt.Errorf("Can't read watermark: %s", err)
2018-10-19 11:47:11 +02:00
}
imgdata, err := readAndCheckImage(f, int(fi.Size()))
2018-10-19 11:47:11 +02:00
if err != nil {
return nil, fmt.Errorf("Can't read watermark: %s", err)
2018-10-19 11:47:11 +02:00
}
return imgdata, err
2018-10-19 11:47:11 +02:00
}
func remoteWatermarkData(imageURL string) (*imageData, error) {
res, err := requestImage(imageURL)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("Can't download watermark: %s", err)
}
2018-10-19 11:47:11 +02:00
imgdata, err := readAndCheckImage(res.Body, int(res.ContentLength))
2018-10-19 11:47:11 +02:00
if err != nil {
return nil, fmt.Errorf("Can't download watermark: %s", err)
2018-10-19 11:47:11 +02:00
}
return imgdata, err
2018-10-19 11:47:11 +02:00
}