2022-01-23 16:41:31 +03:00
|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-06-23 16:38:19 +03:00
|
|
|
jpegstructure "github.com/superseriousbusiness/go-jpeg-image-structure/v2"
|
2023-11-30 12:50:28 +03:00
|
|
|
pngstructure "github.com/superseriousbusiness/go-png-image-structure/v2"
|
2022-01-23 16:41:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func Terminate(in io.Reader, fileSize int, mediaType string) (io.Reader, error) {
|
2023-11-30 12:50:28 +03:00
|
|
|
// To avoid keeping too much stuff
|
|
|
|
// in memory we want to pipe data
|
|
|
|
// directly to the reader.
|
2022-01-23 16:41:31 +03:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// We don't know ahead of time how long
|
|
|
|
// segments might be: they could be as
|
|
|
|
// large as the file itself, so we need
|
|
|
|
// a buffer with generous overhead.
|
2022-01-23 16:41:31 +03:00
|
|
|
scanner := bufio.NewScanner(in)
|
|
|
|
scanner.Buffer([]byte{}, fileSize)
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
var err error
|
2022-01-23 16:41:31 +03:00
|
|
|
switch mediaType {
|
|
|
|
case "image/jpeg", "jpeg", "jpg":
|
2022-08-10 15:10:46 +03:00
|
|
|
err = terminateJpeg(scanner, pipeWriter, fileSize)
|
2023-11-30 12:50:28 +03:00
|
|
|
|
2022-12-06 16:15:25 +03:00
|
|
|
case "image/webp", "webp":
|
|
|
|
err = terminateWebp(scanner, pipeWriter)
|
2023-11-30 12:50:28 +03:00
|
|
|
|
2022-01-23 16:41:31 +03:00
|
|
|
case "image/png", "png":
|
2023-11-30 12:50:28 +03:00
|
|
|
// For pngs we need to skip the header bytes, so read
|
|
|
|
// them in and check we're really dealing with a png.
|
2022-01-23 16:41:31 +03:00
|
|
|
header := make([]byte, len(pngstructure.PngSignature))
|
|
|
|
if _, headerError := in.Read(header); headerError != nil {
|
|
|
|
err = headerError
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(header, pngstructure.PngSignature[:]) {
|
|
|
|
err = errors.New("could not decode png: invalid header")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
err = terminatePng(scanner, pipeWriter)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("mediaType %s cannot be processed", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipeReader, err
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
func terminateJpeg(scanner *bufio.Scanner, writer *io.PipeWriter, expectedFileSize int) error {
|
2022-01-23 16:41:31 +03:00
|
|
|
v := &jpegVisitor{
|
2022-08-10 15:10:46 +03:00
|
|
|
writer: writer,
|
|
|
|
expectedFileSize: expectedFileSize,
|
2022-01-23 16:41:31 +03:00
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// Provide the visitor to the splitter so
|
|
|
|
// that it triggers on every section scan.
|
2022-01-23 16:41:31 +03:00
|
|
|
js := jpegstructure.NewJpegSplitter(v)
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// The visitor also needs to read back the
|
|
|
|
// list of segments: for this it needs to
|
|
|
|
// know what jpeg splitter it's attached to,
|
|
|
|
// so give it a pointer to the splitter.
|
2022-01-23 16:41:31 +03:00
|
|
|
v.js = js
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// Jpeg visitor's 'split' function
|
|
|
|
// satisfies bufio.SplitFunc{}.
|
2022-01-23 16:41:31 +03:00
|
|
|
scanner.Split(js.Split)
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
go scanAndClose(scanner, writer)
|
2022-01-23 16:41:31 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
func terminateWebp(scanner *bufio.Scanner, writer *io.PipeWriter) error {
|
2022-12-06 16:15:25 +03:00
|
|
|
v := &webpVisitor{
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// Webp visitor's 'split' function
|
|
|
|
// satisfies bufio.SplitFunc{}.
|
2022-12-06 16:15:25 +03:00
|
|
|
scanner.Split(v.split)
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
go scanAndClose(scanner, writer)
|
2022-12-06 16:15:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
func terminatePng(scanner *bufio.Scanner, writer *io.PipeWriter) error {
|
2022-01-23 16:41:31 +03:00
|
|
|
ps := pngstructure.NewPngSplitter()
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// Don't bother checking CRC;
|
|
|
|
// we're overwriting it anyway.
|
|
|
|
ps.DoCheckCrc(false)
|
|
|
|
|
2022-01-23 16:41:31 +03:00
|
|
|
v := &pngVisitor{
|
|
|
|
ps: ps,
|
|
|
|
writer: writer,
|
|
|
|
lastWrittenChunk: -1,
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// Png visitor's 'split' function
|
|
|
|
// satisfies bufio.SplitFunc{}.
|
2022-01-23 16:41:31 +03:00
|
|
|
scanner.Split(v.split)
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
go scanAndClose(scanner, writer)
|
2022-01-23 16:41:31 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-30 12:50:28 +03:00
|
|
|
// scanAndClose scans through the given scanner until there's
|
|
|
|
// nothing left to scan, and then closes the writer so that the
|
|
|
|
// reader on the other side of the pipe knows that we're done.
|
|
|
|
//
|
|
|
|
// Any error encountered when scanning will be logged by terminator.
|
|
|
|
//
|
|
|
|
// Due to the nature of io.Pipe, writing won't actually work
|
|
|
|
// until the pipeReader starts being read by the caller, which
|
|
|
|
// is why this function should always be called asynchronously.
|
|
|
|
func scanAndClose(scanner *bufio.Scanner, writer *io.PipeWriter) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Always close writer, using returned
|
|
|
|
// scanner error (if any). If err is nil
|
|
|
|
// then the standard io.EOF will be used.
|
|
|
|
// (this will not overwrite existing).
|
|
|
|
writer.CloseWithError(err)
|
2022-01-23 16:41:31 +03:00
|
|
|
}()
|
2023-11-30 12:50:28 +03:00
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set error on return.
|
|
|
|
err = scanner.Err()
|
2022-01-23 16:41:31 +03:00
|
|
|
}
|