2021-04-21 17:47:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-24 11:58:14 +00:00
|
|
|
"context"
|
2021-04-21 17:47:45 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-24 11:58:14 +00:00
|
|
|
"log"
|
2021-04-21 17:47:45 +00:00
|
|
|
"net/http"
|
2021-06-18 12:32:03 +00:00
|
|
|
|
2021-12-24 11:58:14 +00:00
|
|
|
"github.com/carlmjohnson/requests"
|
2021-04-21 17:47:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultCompressionWidth = 2000
|
|
|
|
const defaultCompressionHeight = 3000
|
|
|
|
|
2021-06-20 13:18:02 +00:00
|
|
|
type mediaCompression interface {
|
2021-12-07 17:23:57 +00:00
|
|
|
compress(url string, save mediaStorageSaveFunc, hc *http.Client) (location string, err error)
|
2021-06-20 13:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) compressMediaFile(url string) (location string, err error) {
|
|
|
|
// Init compressors
|
|
|
|
a.compressorsInit.Do(a.initMediaCompressors)
|
|
|
|
// Try all compressors until success
|
|
|
|
for _, c := range a.compressors {
|
2021-06-23 12:28:51 +00:00
|
|
|
location, err = c.compress(url, a.saveMediaFile, a.httpClient)
|
2021-06-20 13:18:02 +00:00
|
|
|
if location != "" && err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return result
|
|
|
|
return location, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) initMediaCompressors() {
|
2021-12-24 11:58:14 +00:00
|
|
|
if a.cfg.Micropub == nil || a.cfg.Micropub.MediaStorage == nil {
|
2021-06-20 13:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-24 11:58:14 +00:00
|
|
|
config := a.cfg.Micropub.MediaStorage
|
2021-06-20 13:18:02 +00:00
|
|
|
if key := config.ShortPixelKey; key != "" {
|
|
|
|
a.compressors = append(a.compressors, &shortpixel{key})
|
|
|
|
}
|
|
|
|
if key := config.TinifyKey; key != "" {
|
|
|
|
a.compressors = append(a.compressors, &tinify{key})
|
|
|
|
}
|
|
|
|
if config.CloudflareCompressionEnabled {
|
|
|
|
a.compressors = append(a.compressors, &cloudflare{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:28:51 +00:00
|
|
|
type shortpixel struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
2021-12-24 11:58:14 +00:00
|
|
|
func (sp *shortpixel) compress(url string, upload mediaStorageSaveFunc, hc *http.Client) (string, error) {
|
2021-06-20 13:18:02 +00:00
|
|
|
// Check url
|
|
|
|
fileExtension, allowed := urlHasExt(url, "jpg", "jpeg", "png")
|
|
|
|
if !allowed {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
// Compress
|
2021-12-24 11:58:14 +00:00
|
|
|
var imgBuffer bytes.Buffer
|
|
|
|
err := requests.
|
|
|
|
URL("https://api.shortpixel.com/v2/reducer-sync.php").
|
|
|
|
Client(hc).
|
2022-01-04 08:48:37 +00:00
|
|
|
Method(http.MethodPost).
|
2021-12-24 11:58:14 +00:00
|
|
|
BodyJSON(map[string]interface{}{
|
|
|
|
"key": sp.key,
|
|
|
|
"plugin_version": "GB001",
|
|
|
|
"lossy": 1,
|
|
|
|
"resize": 3,
|
|
|
|
"resize_width": defaultCompressionWidth,
|
|
|
|
"resize_height": defaultCompressionHeight,
|
|
|
|
"cmyk2rgb": 1,
|
|
|
|
"keep_exif": 0,
|
|
|
|
"url": url,
|
|
|
|
}).
|
|
|
|
ToBytesBuffer(&imgBuffer).
|
|
|
|
Fetch(context.Background())
|
2021-06-20 13:18:02 +00:00
|
|
|
if err != nil {
|
2021-12-24 11:58:14 +00:00
|
|
|
log.Println("Shortpixel error:", err.Error())
|
|
|
|
return "", errors.New("failed to compress image using shortpixel")
|
2021-04-21 17:47:45 +00:00
|
|
|
}
|
2021-06-20 13:18:02 +00:00
|
|
|
// Upload compressed file
|
2021-12-24 11:58:14 +00:00
|
|
|
return uploadCompressedFile(fileExtension, &imgBuffer, upload)
|
2021-06-20 13:18:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 12:28:51 +00:00
|
|
|
type tinify struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
2021-12-24 11:58:14 +00:00
|
|
|
func (tf *tinify) compress(url string, upload mediaStorageSaveFunc, hc *http.Client) (string, error) {
|
2022-01-04 09:37:48 +00:00
|
|
|
tinifyErr := errors.New("failed to compress image using tinify")
|
2021-04-21 17:47:45 +00:00
|
|
|
// Check url
|
2021-06-20 13:18:02 +00:00
|
|
|
fileExtension, allowed := urlHasExt(url, "jpg", "jpeg", "png")
|
2021-04-21 17:47:45 +00:00
|
|
|
if !allowed {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
// Compress
|
2022-01-04 09:37:48 +00:00
|
|
|
headers := http.Header{}
|
2021-12-24 11:58:14 +00:00
|
|
|
err := requests.
|
|
|
|
URL("https://api.tinify.com/shrink").
|
|
|
|
Client(hc).
|
2022-01-04 08:48:37 +00:00
|
|
|
Method(http.MethodPost).
|
2021-12-24 11:58:14 +00:00
|
|
|
BasicAuth("api", tf.key).
|
|
|
|
BodyJSON(map[string]interface{}{
|
|
|
|
"source": map[string]interface{}{
|
|
|
|
"url": url,
|
|
|
|
},
|
|
|
|
}).
|
2022-01-04 09:37:48 +00:00
|
|
|
ToHeaders(headers).
|
2021-12-24 11:58:14 +00:00
|
|
|
Fetch(context.Background())
|
2021-04-21 17:47:45 +00:00
|
|
|
if err != nil {
|
2021-12-24 11:58:14 +00:00
|
|
|
log.Println("Tinify error:", err.Error())
|
2022-01-04 09:37:48 +00:00
|
|
|
return "", tinifyErr
|
|
|
|
}
|
|
|
|
compressedLocation := headers.Get("Location")
|
|
|
|
if compressedLocation == "" {
|
|
|
|
log.Println("Tinify error: location header missing")
|
|
|
|
return "", tinifyErr
|
2021-04-21 17:47:45 +00:00
|
|
|
}
|
|
|
|
// Resize and download image
|
2021-12-24 11:58:14 +00:00
|
|
|
var imgBuffer bytes.Buffer
|
|
|
|
err = requests.
|
|
|
|
URL(compressedLocation).
|
|
|
|
Client(hc).
|
2022-01-04 08:48:37 +00:00
|
|
|
Method(http.MethodPost).
|
2021-12-24 11:58:14 +00:00
|
|
|
BasicAuth("api", tf.key).
|
|
|
|
BodyJSON(map[string]interface{}{
|
|
|
|
"resize": map[string]interface{}{
|
|
|
|
"method": "fit",
|
|
|
|
"width": defaultCompressionWidth,
|
|
|
|
"height": defaultCompressionHeight,
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
ToBytesBuffer(&imgBuffer).
|
|
|
|
Fetch(context.Background())
|
2021-04-21 17:47:45 +00:00
|
|
|
if err != nil {
|
2021-12-24 11:58:14 +00:00
|
|
|
log.Println("Tinify error:", err.Error())
|
2022-01-04 09:37:48 +00:00
|
|
|
return "", tinifyErr
|
2021-04-21 17:47:45 +00:00
|
|
|
}
|
|
|
|
// Upload compressed file
|
2021-12-24 11:58:14 +00:00
|
|
|
return uploadCompressedFile(fileExtension, &imgBuffer, upload)
|
2021-04-21 17:47:45 +00:00
|
|
|
}
|
2021-04-21 18:10:32 +00:00
|
|
|
|
2021-12-24 11:58:14 +00:00
|
|
|
type cloudflare struct{}
|
2021-11-08 13:32:02 +00:00
|
|
|
|
2021-12-24 11:58:14 +00:00
|
|
|
func (cf *cloudflare) compress(url string, upload mediaStorageSaveFunc, hc *http.Client) (string, error) {
|
2021-04-21 18:10:32 +00:00
|
|
|
// Check url
|
2021-12-24 11:58:14 +00:00
|
|
|
if _, allowed := urlHasExt(url, "jpg", "jpeg", "png"); !allowed {
|
2021-04-21 18:10:32 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
// Force jpeg
|
|
|
|
fileExtension := "jpeg"
|
|
|
|
// Compress
|
2021-12-24 11:58:14 +00:00
|
|
|
var imgBuffer bytes.Buffer
|
|
|
|
err := requests.
|
|
|
|
URL(fmt.Sprintf("https://www.cloudflare.com/cdn-cgi/image/f=jpeg,q=75,metadata=none,fit=scale-down,w=%d,h=%d/%s", defaultCompressionWidth, defaultCompressionHeight, url)).
|
|
|
|
Client(hc).
|
|
|
|
ToBytesBuffer(&imgBuffer).
|
|
|
|
Fetch(context.Background())
|
2021-04-21 18:10:32 +00:00
|
|
|
if err != nil {
|
2021-12-24 11:58:14 +00:00
|
|
|
log.Println("Cloudflare error:", err.Error())
|
|
|
|
return "", errors.New("failed to compress image using cloudflare")
|
2021-04-21 18:10:32 +00:00
|
|
|
}
|
2021-12-24 11:58:14 +00:00
|
|
|
// Upload compressed file
|
|
|
|
return uploadCompressedFile(fileExtension, &imgBuffer, upload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadCompressedFile(fileExtension string, imgBuffer *bytes.Buffer, upload mediaStorageSaveFunc) (string, error) {
|
|
|
|
// Create reader from buffer
|
|
|
|
imgReader := bytes.NewReader(imgBuffer.Bytes())
|
|
|
|
// Get hash of compressed file
|
|
|
|
fileName, err := getSHA256(imgReader)
|
2021-04-21 18:10:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Upload compressed file
|
2021-12-24 11:58:14 +00:00
|
|
|
return upload(fileName+"."+fileExtension, imgReader)
|
2021-04-21 18:10:32 +00:00
|
|
|
}
|