From 48679bc1cfc907fb2ad14a9378b2ec09c6914584 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 23 Mar 2022 21:48:28 +0100 Subject: [PATCH] Remove Shortpixel --- config.go | 2 -- docs/usage.md | 6 ++---- example-config.yml | 1 - feeds.go | 2 +- mediaCompression.go | 41 ---------------------------------------- mediaCompression_test.go | 30 ----------------------------- templateAssets.go | 5 +---- tor.go | 2 +- 8 files changed, 5 insertions(+), 84 deletions(-) diff --git a/config.go b/config.go index 64d6a37..98d2407 100644 --- a/config.go +++ b/config.go @@ -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"` } diff --git a/docs/usage.md b/docs/usage.md index e5bec8b..ccfac81 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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 diff --git a/example-config.yml b/example-config.yml index 24b3362..2fcbafa 100644 --- a/example-config.yml +++ b/example-config.yml @@ -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) diff --git a/feeds.go b/feeds.go index 5224975..9b793ec 100644 --- a/feeds.go +++ b/feeds.go @@ -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) diff --git a/mediaCompression.go b/mediaCompression.go index 84a2829..0e56e08 100644 --- a/mediaCompression.go +++ b/mediaCompression.go @@ -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 } diff --git a/mediaCompression_test.go b/mediaCompression_test.go index f8f6388..2412885 100644 --- a/mediaCompression_test.go +++ b/mediaCompression_test.go @@ -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) - }) - } diff --git a/templateAssets.go b/templateAssets.go index de4ddaa..e07930c 100644 --- a/templateAssets.go +++ b/templateAssets.go @@ -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 { diff --git a/tor.go b/tor.go index 49ed60e..a38fc61 100644 --- a/tor.go +++ b/tor.go @@ -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) {