]> Sergey Matveev's repositories - tofuproxy.git/commitdiff
AVIF transcoding
authorSergey Matveev <stargrave@stargrave.org>
Tue, 7 Sep 2021 21:45:53 +0000 (00:45 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 7 Sep 2021 21:45:53 +0000 (00:45 +0300)
doc/index.texi
rounds/45transcodeAVIF.go [new file with mode: 0644]
rounds/45transcodeJXL.go
trip.go

index 5839a6b3f11e99759e98a25cab17d32b198f97e5..0d27a68707f7792bed0724ec1e5168d36107b72c 100644 (file)
@@ -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 (file)
index 0000000..06a0ffa
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+tofuproxy -- HTTP proxy with TLS certificates management
+Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
+
+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 <http://www.gnu.org/licenses/>.
+*/
+
+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)
+}
index 2d748fa4854ff5202f0276b43b6085fd10d12181..2043fb189154a4e851fa8318faf8fa82dd9a9f1d 100644 (file)
@@ -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 019c4081b93ce4ca6cae1e9b44fd7bebacc83fa8..27d4e9e30beacd8239b3827d74fe45a97526d8af 100644 (file)
--- 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)