mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[feature] media: add webp support (#1155)
* media: add webp support Signed-off-by: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se> * bump exif-terminator to v0.5.0 Signed-off-by: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se> Signed-off-by: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
This commit is contained in:
committed by
GitHub
parent
f9e5ec99bd
commit
1a3f26fb5c
2
vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go
generated
vendored
2
vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go
generated
vendored
@ -91,7 +91,7 @@ func (v *jpegVisitor) HandleSegment(segmentMarker byte, _ string, _ int, _ bool)
|
||||
if segmentMarker == jpegstructure.MARKER_EOI {
|
||||
// take account of the last 2 bytes taken up by the EOI
|
||||
eoiLength := 2
|
||||
|
||||
|
||||
// this is the total file size we will
|
||||
// have written including the EOI
|
||||
willHaveWritten := v.writtenTotalBytes + eoiLength
|
||||
|
14
vendor/github.com/superseriousbusiness/exif-terminator/terminator.go
generated
vendored
14
vendor/github.com/superseriousbusiness/exif-terminator/terminator.go
generated
vendored
@ -43,6 +43,8 @@ func Terminate(in io.Reader, fileSize int, mediaType string) (io.Reader, error)
|
||||
switch mediaType {
|
||||
case "image/jpeg", "jpeg", "jpg":
|
||||
err = terminateJpeg(scanner, pipeWriter, fileSize)
|
||||
case "image/webp", "webp":
|
||||
err = terminateWebp(scanner, pipeWriter)
|
||||
case "image/png", "png":
|
||||
// for pngs we need to skip the header bytes, so read them in
|
||||
// and check we're really dealing with a png here
|
||||
@ -86,6 +88,18 @@ func terminateJpeg(scanner *bufio.Scanner, writer io.WriteCloser, expectedFileSi
|
||||
return nil
|
||||
}
|
||||
|
||||
func terminateWebp(scanner *bufio.Scanner, writer io.WriteCloser) error {
|
||||
v := &webpVisitor{
|
||||
writer: writer,
|
||||
}
|
||||
|
||||
// use the webp visitor's 'split' function, which satisfies the bufio.SplitFunc interface
|
||||
scanner.Split(v.split)
|
||||
|
||||
scanAndClose(scanner, writer)
|
||||
return nil
|
||||
}
|
||||
|
||||
func terminatePng(scanner *bufio.Scanner, writer io.WriteCloser) error {
|
||||
ps := pngstructure.NewPngSplitter()
|
||||
|
||||
|
101
vendor/github.com/superseriousbusiness/exif-terminator/webp.go
generated
vendored
Normal file
101
vendor/github.com/superseriousbusiness/exif-terminator/webp.go
generated
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
exif-terminator
|
||||
Copyright (C) 2022 SuperSeriousBusiness admin@gotosocial.org
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package terminator
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
riffHeaderSize = 4 * 3
|
||||
)
|
||||
|
||||
var (
|
||||
riffHeader = [4]byte{'R', 'I', 'F', 'F'}
|
||||
webpHeader = [4]byte{'W', 'E', 'B', 'P'}
|
||||
exifFourcc = [4]byte{'E', 'X', 'I', 'F'}
|
||||
xmpFourcc = [4]byte{'X', 'M', 'P', ' '}
|
||||
|
||||
errNoRiffHeader = errors.New("no RIFF header")
|
||||
errNoWebpHeader = errors.New("not a WEBP file")
|
||||
)
|
||||
|
||||
type webpVisitor struct {
|
||||
writer io.Writer
|
||||
doneHeader bool
|
||||
}
|
||||
|
||||
func fourCC(b []byte) [4]byte {
|
||||
return [4]byte{b[0], b[1], b[2], b[3]}
|
||||
}
|
||||
|
||||
func (v *webpVisitor) split(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
// parse/write the header first
|
||||
if !v.doneHeader {
|
||||
if len(data) < riffHeaderSize {
|
||||
// need the full header
|
||||
return
|
||||
}
|
||||
if fourCC(data) != riffHeader {
|
||||
err = errNoRiffHeader
|
||||
return
|
||||
}
|
||||
if fourCC(data[8:]) != webpHeader {
|
||||
err = errNoWebpHeader
|
||||
return
|
||||
}
|
||||
if _, err = v.writer.Write(data[:riffHeaderSize]); err != nil {
|
||||
return
|
||||
}
|
||||
advance += riffHeaderSize
|
||||
data = data[riffHeaderSize:]
|
||||
v.doneHeader = true
|
||||
}
|
||||
|
||||
// need enough for fourcc and size
|
||||
if len(data) < 8 {
|
||||
return
|
||||
}
|
||||
size := int64(binary.LittleEndian.Uint32(data[4:]))
|
||||
if (size & 1) != 0 {
|
||||
// odd chunk size - extra padding byte
|
||||
size++
|
||||
}
|
||||
// wait until there is enough
|
||||
if int64(len(data)-8) < size {
|
||||
return
|
||||
}
|
||||
|
||||
fourcc := fourCC(data)
|
||||
rawChunkData := data[8 : 8+size]
|
||||
if fourcc == exifFourcc || fourcc == xmpFourcc {
|
||||
// replace exif/xmp with blank
|
||||
rawChunkData = make([]byte, size)
|
||||
}
|
||||
|
||||
if _, err = v.writer.Write(data[:8]); err == nil {
|
||||
if _, err = v.writer.Write(rawChunkData); err == nil {
|
||||
advance += 8 + int(size)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user