Remove Shortpixel

This commit is contained in:
Jan-Lukas Else 2022-03-23 21:48:28 +01:00
parent b460f5dd0a
commit 48679bc1cf
8 changed files with 5 additions and 84 deletions

View File

@ -242,8 +242,6 @@ type configMicropubMedia struct {
FTPPassword string `mapstructure:"ftpPassword"`
// Tinify
TinifyKey string `mapstructure:"tinifyKey"`
// Shortpixel
ShortPixelKey string `mapstructure:"shortPixelKey"`
// Cloudflare
CloudflareCompressionEnabled bool `mapstructure:"cloudflareCompressionEnabled"`
}

View File

@ -19,16 +19,14 @@ To reduce the data transfer for blog visitors, GoBlog can compress the media fil
GoBlog currently supports the following media compression providers:
- [Cloudflare](https://cloudflare.com/) (no API key required)
- [ShortPixel](https://shortpixel.com/) (API key required)
- [Tinify](https://tinify.com/) (API key required)
Take a look at the `example-config.yml` on how to configure the compression providers.
It is possible to configure multiple compression providers. If one fails, the next one is tried. The current priority is as follows:
1. ShortPixel
2. Tinify
3. Cloudflare
1. Tinify
2. Cloudflare
## Text-to-Speech

View File

@ -111,7 +111,6 @@ micropub:
ftpUser: ftpuser # Username of FTP user
ftpPassword: ftppassword # Password of FTP user
# Image compression (optional, you can define no, one or multiple services, disabled when private mode enabled)
shortPixelKey: SHORT-PIXEL-KEY # Secret key for the ShortPixel API
tinifyKey: TINIFY-KEY # Secret key for the Tinify.com API (first fallback)
cloudflareCompressionEnabled: true # Use Cloudflare's compression as a second fallback
# MicroPub parameters (defaults already set, set to overwrite)

View File

@ -21,7 +21,7 @@ const (
jsonFeed feedType = "json"
)
func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Request, posts []*post, title string, description string) {
func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Request, posts []*post, title, description string) {
now := time.Now()
title = a.renderMdTitle(defaultIfEmpty(title, a.cfg.Blogs[blog].Title))
description = defaultIfEmpty(description, a.cfg.Blogs[blog].Description)

View File

@ -39,9 +39,6 @@ func (a *goBlog) initMediaCompressors() {
return
}
config := a.cfg.Micropub.MediaStorage
if key := config.ShortPixelKey; key != "" {
a.compressors = append(a.compressors, &shortpixel{key})
}
if key := config.TinifyKey; key != "" {
a.compressors = append(a.compressors, &tinify{key})
}
@ -50,44 +47,6 @@ func (a *goBlog) initMediaCompressors() {
}
}
type shortpixel struct {
key string
}
func (sp *shortpixel) compress(url string, upload mediaStorageSaveFunc, hc *http.Client) (string, error) {
// Check url
fileExtension, allowed := urlHasExt(url, "jpg", "jpeg", "png")
if !allowed {
return "", nil
}
// Compress
imgBuffer := bufferpool.Get()
defer bufferpool.Put(imgBuffer)
err := requests.
URL("https://api.shortpixel.com/v2/reducer-sync.php").
Client(hc).
Method(http.MethodPost).
BodyJSON(map[string]any{
"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())
if err != nil {
log.Println("Shortpixel error:", err.Error())
return "", errors.New("failed to compress image using shortpixel")
}
// Upload compressed file
return uploadCompressedFile(fileExtension, imgBuffer, upload)
}
type tinify struct {
key string
}

View File

@ -2,14 +2,12 @@ package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_compress(t *testing.T) {
@ -37,32 +35,4 @@ func Test_compress(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "https://example.com/"+fakeSha256+".jpeg", res)
})
t.Run("Shortpixel", func(t *testing.T) {
fakeClient := newFakeHttpClient()
fakeClient.setHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
assert.Equal(t, "https://api.shortpixel.com/v2/reducer-sync.php", r.URL.String())
requestBody, _ := io.ReadAll(r.Body)
defer r.Body.Close()
var requestJson map[string]any
err := json.Unmarshal(requestBody, &requestJson)
require.Nil(t, err)
require.NotNil(t, requestJson)
assert.Equal(t, "testkey", requestJson["key"])
assert.Equal(t, "https://example.com/original.jpg", requestJson["url"])
rw.WriteHeader(http.StatusOK)
_, _ = io.WriteString(rw, fakeFileContent)
}))
cf := &shortpixel{"testkey"}
res, err := cf.compress("https://example.com/original.jpg", uf, fakeClient.Client)
assert.Nil(t, err)
assert.Equal(t, "https://example.com/"+fakeSha256+".jpg", res)
})
}

View File

@ -46,10 +46,7 @@ func (a *goBlog) initTemplateAssets() error {
return err
}
// Add syntax highlighting CSS
if err := a.initChromaCSS(); err != nil {
return err
}
return nil
return a.initChromaCSS()
}
func (a *goBlog) compileAsset(name string, read io.Reader) error {

2
tor.go
View File

@ -77,7 +77,7 @@ func (a *goBlog) startOnionService(h http.Handler) error {
return nil
}
func (a *goBlog) createTorPrivateKey(torDataPath string) (crypto.PrivateKey, error) {
func (*goBlog) createTorPrivateKey(torDataPath string) (crypto.PrivateKey, error) {
torKeyPath := filepath.Join(torDataPath, "onion.pk")
var torKey crypto.PrivateKey
if _, err := os.Stat(torKeyPath); os.IsNotExist(err) {