2018-10-19 11:47:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
func getWatermarkData() (*imageData, error) {
|
2018-10-19 11:47:11 +02:00
|
|
|
if len(conf.WatermarkData) > 0 {
|
2019-09-20 13:01:00 +02:00
|
|
|
return base64WatermarkData(conf.WatermarkData)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(conf.WatermarkPath) > 0 {
|
2019-09-20 13:01:00 +02:00
|
|
|
return fileWatermarkData(conf.WatermarkPath)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(conf.WatermarkURL) > 0 {
|
2019-09-20 13:01:00 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +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 {
|
2019-09-20 13:01:00 +02:00
|
|
|
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 {
|
2019-09-20 13:01:00 +02:00
|
|
|
return nil, fmt.Errorf("Can't decode watermark: %s", err)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
return &imageData{Data: data, Type: imgtype}, nil
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
func fileWatermarkData(path string) (*imageData, error) {
|
|
|
|
f, err := os.Open(path)
|
2018-10-19 11:47:11 +02:00
|
|
|
if err != nil {
|
2019-09-20 13:01:00 +02:00
|
|
|
return nil, fmt.Errorf("Can't read watermark: %s", err)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
fi, err := f.Stat()
|
2018-10-19 11:47:11 +02:00
|
|
|
if err != nil {
|
2019-09-20 13:01:00 +02:00
|
|
|
return nil, fmt.Errorf("Can't read watermark: %s", err)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
imgdata, err := readAndCheckImage(f, int(fi.Size()))
|
2018-10-19 11:47:11 +02:00
|
|
|
if err != nil {
|
2019-09-20 13:01:00 +02:00
|
|
|
return nil, fmt.Errorf("Can't read watermark: %s", err)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
return imgdata, err
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +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
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
imgdata, err := readAndCheckImage(res.Body, int(res.ContentLength))
|
2018-10-19 11:47:11 +02:00
|
|
|
if err != nil {
|
2019-09-20 13:01:00 +02:00
|
|
|
return nil, fmt.Errorf("Can't download watermark: %s", err)
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:01:00 +02:00
|
|
|
return imgdata, err
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|