summaryrefslogtreecommitdiff
path: root/internal/media/util.go
diff options
context:
space:
mode:
authorLibravatar tsmethurst <tobi.smethurst@protonmail.com>2022-01-16 18:52:55 +0100
committerLibravatar tsmethurst <tobi.smethurst@protonmail.com>2022-01-16 18:52:55 +0100
commit589bb9df0275457b5f9c3790e67517ec1be1745d (patch)
tree8b15a4a457bb7ac1d4b209e5cb930cb4ec315ca8 /internal/media/util.go
parentupdate dependencies (diff)
downloadgotosocial-589bb9df0275457b5f9c3790e67517ec1be1745d.tar.xz
pass reader around instead of []byte
Diffstat (limited to 'internal/media/util.go')
-rw-r--r--internal/media/util.go11
1 files changed, 5 insertions, 6 deletions
diff --git a/internal/media/util.go b/internal/media/util.go
index 7a3d81c0f..248d5fb19 100644
--- a/internal/media/util.go
+++ b/internal/media/util.go
@@ -19,7 +19,6 @@
package media
import (
- "bytes"
"errors"
"fmt"
@@ -28,11 +27,11 @@ import (
// parseContentType parses the MIME content type from a file, returning it as a string in the form (eg., "image/jpeg").
// Returns an error if the content type is not something we can process.
-func parseContentType(content []byte) (string, error) {
- // read in the first bytes of the file
- fileHeader := make([]byte, maxFileHeaderBytes)
- if _, err := bytes.NewReader(content).Read(fileHeader); err != nil {
- return "", fmt.Errorf("could not read first magic bytes of file: %s", err)
+//
+// Fileheader should be no longer than 262 bytes; anything more than this is inefficient.
+func parseContentType(fileHeader []byte) (string, error) {
+ if fhLength := len(fileHeader); fhLength > maxFileHeaderBytes {
+ return "", fmt.Errorf("parseContentType requires %d bytes max, we got %d", maxFileHeaderBytes, fhLength)
}
kind, err := filetype.Match(fileHeader)