mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-29 12:42:46 +00:00
Refactor media storage layer, add tests (#2352)
* Refactor mediaapi storage layer * Verify filetype before trying to create thumbnails * Add media api storage tests * Fix returned values
This commit is contained in:
parent
3a5e9a0f28
commit
3ddbffd59e
20 changed files with 417 additions and 416 deletions
|
@ -22,6 +22,7 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
|
@ -311,6 +312,26 @@ func (r *uploadRequest) storeFileAndMetadata(
|
|||
}
|
||||
|
||||
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(
|
||||
context.Background(), finalPath, thumbnailSizes, r.MediaMetadata,
|
||||
activeThumbnailGeneration, maxThumbnailGenerators, db, r.Logger,
|
||||
|
|
|
@ -51,7 +51,7 @@ func Test_uploadRequest_doUpload(t *testing.T) {
|
|||
_ = os.Mkdir(testdataPath, os.ModePerm)
|
||||
defer fileutils.RemoveDir(types.Path(testdataPath), nil)
|
||||
|
||||
db, err := storage.Open(&config.DatabaseOptions{
|
||||
db, err := storage.NewMediaAPIDatasource(&config.DatabaseOptions{
|
||||
ConnectionString: "file::memory:?cache=shared",
|
||||
MaxOpenConnections: 100,
|
||||
MaxIdleConnections: 2,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue