mirror of https://github.com/jlelse/GoBlog
Remove Shortpixel
This commit is contained in:
parent
b460f5dd0a
commit
48679bc1cf
|
@ -242,8 +242,6 @@ type configMicropubMedia struct {
|
||||||
FTPPassword string `mapstructure:"ftpPassword"`
|
FTPPassword string `mapstructure:"ftpPassword"`
|
||||||
// Tinify
|
// Tinify
|
||||||
TinifyKey string `mapstructure:"tinifyKey"`
|
TinifyKey string `mapstructure:"tinifyKey"`
|
||||||
// Shortpixel
|
|
||||||
ShortPixelKey string `mapstructure:"shortPixelKey"`
|
|
||||||
// Cloudflare
|
// Cloudflare
|
||||||
CloudflareCompressionEnabled bool `mapstructure:"cloudflareCompressionEnabled"`
|
CloudflareCompressionEnabled bool `mapstructure:"cloudflareCompressionEnabled"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -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:
|
GoBlog currently supports the following media compression providers:
|
||||||
|
|
||||||
- [Cloudflare](https://cloudflare.com/) (no API key required)
|
- [Cloudflare](https://cloudflare.com/) (no API key required)
|
||||||
- [ShortPixel](https://shortpixel.com/) (API key required)
|
|
||||||
- [Tinify](https://tinify.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.
|
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:
|
It is possible to configure multiple compression providers. If one fails, the next one is tried. The current priority is as follows:
|
||||||
|
|
||||||
1. ShortPixel
|
1. Tinify
|
||||||
2. Tinify
|
2. Cloudflare
|
||||||
3. Cloudflare
|
|
||||||
|
|
||||||
## Text-to-Speech
|
## Text-to-Speech
|
||||||
|
|
||||||
|
|
|
@ -111,7 +111,6 @@ micropub:
|
||||||
ftpUser: ftpuser # Username of FTP user
|
ftpUser: ftpuser # Username of FTP user
|
||||||
ftpPassword: ftppassword # Password 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)
|
# 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)
|
tinifyKey: TINIFY-KEY # Secret key for the Tinify.com API (first fallback)
|
||||||
cloudflareCompressionEnabled: true # Use Cloudflare's compression as a second fallback
|
cloudflareCompressionEnabled: true # Use Cloudflare's compression as a second fallback
|
||||||
# MicroPub parameters (defaults already set, set to overwrite)
|
# MicroPub parameters (defaults already set, set to overwrite)
|
||||||
|
|
2
feeds.go
2
feeds.go
|
@ -21,7 +21,7 @@ const (
|
||||||
jsonFeed feedType = "json"
|
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()
|
now := time.Now()
|
||||||
title = a.renderMdTitle(defaultIfEmpty(title, a.cfg.Blogs[blog].Title))
|
title = a.renderMdTitle(defaultIfEmpty(title, a.cfg.Blogs[blog].Title))
|
||||||
description = defaultIfEmpty(description, a.cfg.Blogs[blog].Description)
|
description = defaultIfEmpty(description, a.cfg.Blogs[blog].Description)
|
||||||
|
|
|
@ -39,9 +39,6 @@ func (a *goBlog) initMediaCompressors() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
config := a.cfg.Micropub.MediaStorage
|
config := a.cfg.Micropub.MediaStorage
|
||||||
if key := config.ShortPixelKey; key != "" {
|
|
||||||
a.compressors = append(a.compressors, &shortpixel{key})
|
|
||||||
}
|
|
||||||
if key := config.TinifyKey; key != "" {
|
if key := config.TinifyKey; key != "" {
|
||||||
a.compressors = append(a.compressors, &tinify{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 {
|
type tinify struct {
|
||||||
key string
|
key string
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_compress(t *testing.T) {
|
func Test_compress(t *testing.T) {
|
||||||
|
@ -37,32 +35,4 @@ func Test_compress(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "https://example.com/"+fakeSha256+".jpeg", res)
|
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)
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,10 +46,7 @@ func (a *goBlog) initTemplateAssets() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Add syntax highlighting CSS
|
// Add syntax highlighting CSS
|
||||||
if err := a.initChromaCSS(); err != nil {
|
return a.initChromaCSS()
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) compileAsset(name string, read io.Reader) error {
|
func (a *goBlog) compileAsset(name string, read io.Reader) error {
|
||||||
|
|
2
tor.go
2
tor.go
|
@ -77,7 +77,7 @@ func (a *goBlog) startOnionService(h http.Handler) error {
|
||||||
return nil
|
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")
|
torKeyPath := filepath.Join(torDataPath, "onion.pk")
|
||||||
var torKey crypto.PrivateKey
|
var torKey crypto.PrivateKey
|
||||||
if _, err := os.Stat(torKeyPath); os.IsNotExist(err) {
|
if _, err := os.Stat(torKeyPath); os.IsNotExist(err) {
|
||||||
|
|
Loading…
Reference in New Issue