Simplify image compression
parent
05e6f383c2
commit
0f2ac430ce
1
go.mod
1
go.mod
|
@ -3,7 +3,6 @@ module git.jlel.se/jlelse/hugo-micropub
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
codeberg.org/jlelse/tinify v0.0.0-20200123222407-7fc9c21822b0
|
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
|
||||||
github.com/gliderlabs/ssh v0.3.0 // indirect
|
github.com/gliderlabs/ssh v0.3.0 // indirect
|
||||||
github.com/go-git/go-git/v5 v5.1.0
|
github.com/go-git/go-git/v5 v5.1.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,3 @@
|
||||||
codeberg.org/jlelse/tinify v0.0.0-20200123222407-7fc9c21822b0 h1:pJX79kTd01NtxEnzhfd4OU2SY9fquKVoO47DUeNKe+8=
|
|
||||||
codeberg.org/jlelse/tinify v0.0.0-20200123222407-7fc9c21822b0/go.mod h1:X6cM4Sn0aL/4VQ/ku11yxmiV0WIk5XAaYEPHQLQjFFM=
|
|
||||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
|
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
|
||||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
|
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
|
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
tfgo "codeberg.org/jlelse/tinify"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImageCompression interface {
|
type ImageCompression interface {
|
||||||
|
@ -32,32 +34,27 @@ func (t *Tinify) Compress(url string) (location string, err error) {
|
||||||
err = errors.New("file not supported")
|
err = errors.New("file not supported")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tfgo.SetKey(t.key)
|
// Shrink
|
||||||
s, e := tfgo.FromUrl(url)
|
shrinkedUrl, e := t.shrink(url)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
err = errors.New("failed to compress file")
|
err = errors.New("failed to compress file")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
e = s.Resize(&tfgo.ResizeOption{
|
// Create temp file
|
||||||
Method: tfgo.ResizeMethodScale,
|
|
||||||
Width: 2000,
|
|
||||||
})
|
|
||||||
if e != nil {
|
|
||||||
err = errors.New("failed to resize file")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
file, e := ioutil.TempFile("", "tiny-*."+fileExtension)
|
file, e := ioutil.TempFile("", "tiny-*."+fileExtension)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
err = errors.New("failed to create temporary file")
|
err = errors.New("failed to create temporary file")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
// Cleanup
|
||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
_ = os.Remove(file.Name())
|
_ = os.Remove(file.Name())
|
||||||
}()
|
}()
|
||||||
e = s.ToFile(file.Name())
|
// Resize and download image
|
||||||
|
e = t.resize(shrinkedUrl, file)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
err = errors.New("failed to save compressed file")
|
err = errors.New("failed to resize file or save compressed file")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hashFile, e := os.Open(file.Name())
|
hashFile, e := os.Open(file.Name())
|
||||||
|
@ -73,3 +70,71 @@ func (t *Tinify) Compress(url string) (location string, err error) {
|
||||||
location, err = SelectedMediaStorage.Upload(fileName+"."+fileExtension, file)
|
location, err = SelectedMediaStorage.Upload(fileName+"."+fileExtension, file)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tinify) shrink(source string) (string, error) {
|
||||||
|
type Source struct {
|
||||||
|
Url string `json:"url"`
|
||||||
|
}
|
||||||
|
data := struct {
|
||||||
|
Source Source `json:"source"`
|
||||||
|
}{
|
||||||
|
Source: Source{
|
||||||
|
Url: source,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
body := new(bytes.Buffer)
|
||||||
|
err := json.NewEncoder(body).Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", "https://api.tinify.com/shrink", body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req.SetBasicAuth("api", t.key)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 201 {
|
||||||
|
return "", errors.New("not 201 response code")
|
||||||
|
}
|
||||||
|
return resp.Header.Get("Location"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tinify) resize(output string, file *os.File) error {
|
||||||
|
type Resize struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
}
|
||||||
|
data := struct {
|
||||||
|
Resize Resize `json:"resize"`
|
||||||
|
}{
|
||||||
|
Resize: Resize{
|
||||||
|
Method: "scale",
|
||||||
|
Width: 2000,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
body := new(bytes.Buffer)
|
||||||
|
err := json.NewEncoder(body).Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", output, body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.SetBasicAuth("api", t.key)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return errors.New("not 201 response code")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
_, err = io.Copy(file, resp.Body)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue