From 5de2203a3d1fb3fe2c6ac61cf07441634d3151ae Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Wed, 8 Sep 2021 00:45:53 +0300 Subject: [PATCH] AVIF transcoding --- doc/index.texi | 5 +++-- rounds/45transcodeAVIF.go | 31 +++++++++++++++++++++++++++++++ rounds/45transcodeJXL.go | 24 ++++++++++++++++-------- trip.go | 1 + 4 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 rounds/45transcodeAVIF.go diff --git a/doc/index.texi b/doc/index.texi index 5839a6b..0d27a68 100644 --- a/doc/index.texi +++ b/doc/index.texi @@ -44,7 +44,8 @@ Why the hell people just do not send PostScript documents instead!? @item And wonderful @url{http://jpegxl.info/, JPEG XL} image format is not supported by most browsers. Even pretty old @url{https://developers.google.com/speed/webp, WebP} is not supported -everywhere. +everywhere. @url{https://aomediacodec.github.io/av1-avif/, AVIF} would +be useful too. @end itemize @@ -78,7 +79,7 @@ creating some kind of complex configuration framework. @item WebP images, if it is not Xombrero, is transcoded to PNG. -@item JPEG XL images are transparently transcoded to PNG too. +@item JPEG XL and AVIF images are transparently transcoded to PNG too. @item Default Go's checks are applied to all certificates. If they pass, then certificate chain is saved on the disk. Future connections are diff --git a/rounds/45transcodeAVIF.go b/rounds/45transcodeAVIF.go new file mode 100644 index 0000000..06a0ffa --- /dev/null +++ b/rounds/45transcodeAVIF.go @@ -0,0 +1,31 @@ +/* +tofuproxy -- HTTP proxy with TLS certificates management +Copyright (C) 2021 Sergey Matveev + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package rounds + +import ( + "net/http" +) + +func RoundTranscodeAVIF( + host string, + resp *http.Response, + w http.ResponseWriter, + req *http.Request, +) (bool, error) { + return transcodeCmd2Png("image/avif", "avif", "avifdec", resp, w, req) +} diff --git a/rounds/45transcodeJXL.go b/rounds/45transcodeJXL.go index 2d748fa..2043fb1 100644 --- a/rounds/45transcodeJXL.go +++ b/rounds/45transcodeJXL.go @@ -29,18 +29,16 @@ import ( "go.stargrave.org/tofuproxy/fifos" ) -const CmdDJXL = "djxl" - -func RoundTranscodeJXL( - host string, +func transcodeCmd2Png( + contentType, ext, cmdName string, resp *http.Response, w http.ResponseWriter, req *http.Request, ) (bool, error) { - if resp.Header.Get("Content-Type") != "image/jxl" { + if resp.Header.Get("Content-Type") != contentType { return true, nil } - tmpFd, err := ioutil.TempFile("", "tofuproxy.*.jxl") + tmpFd, err := ioutil.TempFile("", "tofuproxy.*."+ext) if err != nil { log.Fatalln(err) } @@ -53,7 +51,7 @@ func RoundTranscodeJXL( } tmpFd.Close() dstFn := tmpFd.Name() + ".png" - cmd := exec.Command(CmdDJXL, tmpFd.Name(), dstFn) + cmd := exec.Command(cmdName, tmpFd.Name(), dstFn) err = cmd.Run() defer os.Remove(dstFn) if err != nil { @@ -67,10 +65,20 @@ func RoundTranscodeJXL( w.WriteHeader(http.StatusOK) w.Write(data) fifos.SinkOther <- fmt.Sprintf( - "%s %s\t%d\tJPEG XL transcoded to PNG", + "%s %s\t%d\t%s transcoded to PNG", req.Method, req.URL.String(), http.StatusOK, + contentType, ) return false, nil } + +func RoundTranscodeJXL( + host string, + resp *http.Response, + w http.ResponseWriter, + req *http.Request, +) (bool, error) { + return transcodeCmd2Png("image/jxl", "jxl", "djxl", resp, w, req) +} diff --git a/trip.go b/trip.go index 019c408..27d4e9e 100644 --- a/trip.go +++ b/trip.go @@ -91,6 +91,7 @@ func roundTrip(w http.ResponseWriter, req *http.Request) { rounds.RoundDenyFonts, rounds.RoundTranscodeWebP, rounds.RoundTranscodeJXL, + rounds.RoundTranscodeAVIF, rounds.RoundRedirectHTML, } { cont, err := round(host, resp, w, req) -- 2.44.0