mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-01 11:13:12 -06:00
Verify filetype before trying to create thumbnails
This commit is contained in:
parent
382a3da9ae
commit
de680bebca
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -311,6 +312,26 @@ func (r *uploadRequest) storeFileAndMetadata(
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
file, err := os.Open(string(finalPath))
|
||||||
|
if err != nil {
|
||||||
|
r.Logger.WithError(err).Error("unable to open file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close() // nolint: errcheck
|
||||||
|
// http.DetectContentType only needs 512 bytes
|
||||||
|
buf := make([]byte, 512)
|
||||||
|
_, err = file.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
r.Logger.WithError(err).Error("unable to read file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Check if we need to generate thumbnails
|
||||||
|
fileType := http.DetectContentType(buf)
|
||||||
|
if !strings.HasPrefix(fileType, "image") {
|
||||||
|
r.Logger.WithField("contentType", fileType).Debugf("uploaded file is not an image or can not be thumbnailed, not generating thumbnails")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
busy, err := thumbnailer.GenerateThumbnails(
|
busy, err := thumbnailer.GenerateThumbnails(
|
||||||
context.Background(), finalPath, thumbnailSizes, r.MediaMetadata,
|
context.Background(), finalPath, thumbnailSizes, r.MediaMetadata,
|
||||||
activeThumbnailGeneration, maxThumbnailGenerators, db, r.Logger,
|
activeThumbnailGeneration, maxThumbnailGenerators, db, r.Logger,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue