diff options
author | 2022-12-06 14:15:25 +0100 | |
---|---|---|
committer | 2022-12-06 14:15:25 +0100 | |
commit | 1a3f26fb5c515f5672561449f91f6be6383b5a3a (patch) | |
tree | 801c8469ea8ae122cf583953940d3e28bdbfa90f /vendor/github.com | |
parent | [docs] Remove filesystem logging directives from example systemd unit config ... (diff) | |
download | gotosocial-1a3f26fb5c515f5672561449f91f6be6383b5a3a.tar.xz |
[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>
Diffstat (limited to 'vendor/github.com')
3 files changed, 116 insertions, 1 deletions
diff --git a/vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go b/vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go index aa6940a71..576c4e430 100644 --- a/vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go +++ b/vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go @@ -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 diff --git a/vendor/github.com/superseriousbusiness/exif-terminator/terminator.go b/vendor/github.com/superseriousbusiness/exif-terminator/terminator.go index 575bb1e42..9d9e6e743 100644 --- a/vendor/github.com/superseriousbusiness/exif-terminator/terminator.go +++ b/vendor/github.com/superseriousbusiness/exif-terminator/terminator.go @@ -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() diff --git a/vendor/github.com/superseriousbusiness/exif-terminator/webp.go b/vendor/github.com/superseriousbusiness/exif-terminator/webp.go new file mode 100644 index 000000000..392c4871d --- /dev/null +++ b/vendor/github.com/superseriousbusiness/exif-terminator/webp.go @@ -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 +} |