mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 15:33:09 -06:00
mediaapi: Simplify logging of thumbnail properties
This commit is contained in:
parent
2825b4143b
commit
7af3bdf04a
|
|
@ -131,6 +131,12 @@ func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailM
|
||||||
// createThumbnail checks if the thumbnail exists, and if not, generates it
|
// createThumbnail checks if the thumbnail exists, and if not, generates it
|
||||||
// Thumbnail generation is only done once for each non-existing thumbnail.
|
// Thumbnail generation is only done once for each non-existing thumbnail.
|
||||||
func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize, mediaMetadata *types.MediaMetadata, activeThumbnailGeneration *types.ActiveThumbnailGeneration, db *storage.Database, logger *log.Entry) (errorReturn error) {
|
func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize, mediaMetadata *types.MediaMetadata, activeThumbnailGeneration *types.ActiveThumbnailGeneration, db *storage.Database, logger *log.Entry) (errorReturn error) {
|
||||||
|
logger = logger.WithFields(log.Fields{
|
||||||
|
"Width": config.Width,
|
||||||
|
"Height": config.Height,
|
||||||
|
"ResizeMethod": config.ResizeMethod,
|
||||||
|
})
|
||||||
|
|
||||||
dst := GetThumbnailPath(src, config)
|
dst := GetThumbnailPath(src, config)
|
||||||
|
|
||||||
// Note: getActiveThumbnailGeneration uses mutexes and conditions from activeThumbnailGeneration
|
// Note: getActiveThumbnailGeneration uses mutexes and conditions from activeThumbnailGeneration
|
||||||
|
|
@ -155,11 +161,7 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
||||||
// Check if the thumbnail exists.
|
// Check if the thumbnail exists.
|
||||||
thumbnailMetadata, err := db.GetThumbnail(mediaMetadata.MediaID, mediaMetadata.Origin, config.Width, config.Height, config.ResizeMethod)
|
thumbnailMetadata, err := db.GetThumbnail(mediaMetadata.MediaID, mediaMetadata.Origin, config.Width, config.Height, config.ResizeMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.WithFields(log.Fields{
|
logger.Error("Failed to query database for thumbnail.")
|
||||||
"Width": config.Width,
|
|
||||||
"Height": config.Height,
|
|
||||||
"ResizeMethod": config.ResizeMethod,
|
|
||||||
}).Error("Failed to query database for thumbnail.")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if thumbnailMetadata != nil {
|
if thumbnailMetadata != nil {
|
||||||
|
|
@ -174,11 +176,7 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
||||||
|
|
||||||
if isActive == false {
|
if isActive == false {
|
||||||
// Note: This should not happen, but we check just in case.
|
// Note: This should not happen, but we check just in case.
|
||||||
logger.WithFields(log.Fields{
|
logger.Error("Failed to stat file but this is not the active thumbnail generator. This should not happen.")
|
||||||
"Width": config.Width,
|
|
||||||
"Height": config.Height,
|
|
||||||
"ResizeMethod": config.ResizeMethod,
|
|
||||||
}).Error("Failed to stat file but this is not the active thumbnail generator. This should not happen.")
|
|
||||||
return fmt.Errorf("Not active thumbnail generator. Stat error: %q", err)
|
return fmt.Errorf("Not active thumbnail generator. Stat error: %q", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -188,11 +186,8 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.WithFields(log.Fields{
|
logger.WithFields(log.Fields{
|
||||||
"Width": config.Width,
|
|
||||||
"Height": config.Height,
|
|
||||||
"ActualWidth": width,
|
"ActualWidth": width,
|
||||||
"ActualHeight": height,
|
"ActualHeight": height,
|
||||||
"ResizeMethod": config.ResizeMethod,
|
|
||||||
"processTime": time.Now().Sub(start),
|
"processTime": time.Now().Sub(start),
|
||||||
}).Info("Generated thumbnail")
|
}).Info("Generated thumbnail")
|
||||||
|
|
||||||
|
|
@ -219,11 +214,8 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
||||||
err = db.StoreThumbnail(thumbnailMetadata)
|
err = db.StoreThumbnail(thumbnailMetadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.WithError(err).WithFields(log.Fields{
|
logger.WithError(err).WithFields(log.Fields{
|
||||||
"Width": config.Width,
|
|
||||||
"Height": config.Height,
|
|
||||||
"ActualWidth": width,
|
"ActualWidth": width,
|
||||||
"ActualHeight": height,
|
"ActualHeight": height,
|
||||||
"ResizeMethod": config.ResizeMethod,
|
|
||||||
}).Error("Failed to store thumbnail metadata in database.")
|
}).Error("Failed to store thumbnail metadata in database.")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -237,11 +229,7 @@ func getActiveThumbnailGeneration(dst types.Path, config types.ThumbnailSize, ac
|
||||||
activeThumbnailGeneration.Lock()
|
activeThumbnailGeneration.Lock()
|
||||||
defer activeThumbnailGeneration.Unlock()
|
defer activeThumbnailGeneration.Unlock()
|
||||||
if activeThumbnailGenerationResult, ok := activeThumbnailGeneration.PathToResult[string(dst)]; ok {
|
if activeThumbnailGenerationResult, ok := activeThumbnailGeneration.PathToResult[string(dst)]; ok {
|
||||||
logger.WithFields(log.Fields{
|
logger.Info("Waiting for another goroutine to generate the thumbnail.")
|
||||||
"Width": config.Width,
|
|
||||||
"Height": config.Height,
|
|
||||||
"ResizeMethod": config.ResizeMethod,
|
|
||||||
}).Info("Waiting for another goroutine to generate the thumbnail.")
|
|
||||||
|
|
||||||
// NOTE: Wait unlocks and locks again internally. There is still a deferred Unlock() that will unlock this.
|
// NOTE: Wait unlocks and locks again internally. There is still a deferred Unlock() that will unlock this.
|
||||||
activeThumbnailGenerationResult.Cond.Wait()
|
activeThumbnailGenerationResult.Cond.Wait()
|
||||||
|
|
@ -263,11 +251,7 @@ func broadcastGeneration(dst types.Path, activeThumbnailGeneration *types.Active
|
||||||
activeThumbnailGeneration.Lock()
|
activeThumbnailGeneration.Lock()
|
||||||
defer activeThumbnailGeneration.Unlock()
|
defer activeThumbnailGeneration.Unlock()
|
||||||
if activeThumbnailGenerationResult, ok := activeThumbnailGeneration.PathToResult[string(dst)]; ok {
|
if activeThumbnailGenerationResult, ok := activeThumbnailGeneration.PathToResult[string(dst)]; ok {
|
||||||
logger.WithFields(log.Fields{
|
logger.Info("Signalling other goroutines waiting for this goroutine to generate the thumbnail.")
|
||||||
"Width": config.Width,
|
|
||||||
"Height": config.Height,
|
|
||||||
"ResizeMethod": config.ResizeMethod,
|
|
||||||
}).Info("Signalling other goroutines waiting for this goroutine to generate the thumbnail.")
|
|
||||||
// Note: retErr is a named return value error that is signalled from here to waiting goroutines
|
// Note: retErr is a named return value error that is signalled from here to waiting goroutines
|
||||||
activeThumbnailGenerationResult.Err = errorReturn
|
activeThumbnailGenerationResult.Err = errorReturn
|
||||||
activeThumbnailGenerationResult.Cond.Broadcast()
|
activeThumbnailGenerationResult.Cond.Broadcast()
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,11 @@ func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib
|
||||||
Height: height,
|
Height: height,
|
||||||
ResizeMethod: strings.ToLower(req.FormValue("method")),
|
ResizeMethod: strings.ToLower(req.FormValue("method")),
|
||||||
}
|
}
|
||||||
|
r.Logger.WithFields(log.Fields{
|
||||||
|
"RequestedWidth": r.ThumbnailSize.Width,
|
||||||
|
"RequestedHeight": r.ThumbnailSize.Height,
|
||||||
|
"RequestedResizeMethod": r.ThumbnailSize.ResizeMethod,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// request validation
|
// request validation
|
||||||
|
|
@ -242,13 +247,7 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat
|
||||||
responseFile = file
|
responseFile = file
|
||||||
responseMetadata = r.MediaMetadata
|
responseMetadata = r.MediaMetadata
|
||||||
} else {
|
} else {
|
||||||
r.Logger.WithFields(log.Fields{
|
r.Logger.Info("Responding with thumbnail")
|
||||||
"Width": thumbMetadata.ThumbnailSize.Width,
|
|
||||||
"Height": thumbMetadata.ThumbnailSize.Height,
|
|
||||||
"ResizeMethod": thumbMetadata.ThumbnailSize.ResizeMethod,
|
|
||||||
"FileSizeBytes": thumbMetadata.MediaMetadata.FileSizeBytes,
|
|
||||||
"ContentType": thumbMetadata.MediaMetadata.ContentType,
|
|
||||||
}).Info("Responding with thumbnail")
|
|
||||||
responseFile = thumbFile
|
responseFile = thumbFile
|
||||||
responseMetadata = thumbMetadata.MediaMetadata
|
responseMetadata = thumbMetadata.MediaMetadata
|
||||||
}
|
}
|
||||||
|
|
@ -297,11 +296,7 @@ func (r *downloadRequest) getThumbnailFile(filePath types.Path, activeThumbnailG
|
||||||
} else {
|
} else {
|
||||||
thumbnails, err := db.GetThumbnails(r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
|
thumbnails, err := db.GetThumbnails(r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Logger.WithError(err).WithFields(log.Fields{
|
r.Logger.WithError(err).Error("Error looking up thumbnails")
|
||||||
"Width": r.ThumbnailSize.Width,
|
|
||||||
"Height": r.ThumbnailSize.Height,
|
|
||||||
"ResizeMethod": r.ThumbnailSize.ResizeMethod,
|
|
||||||
}).Error("Error looking up thumbnails")
|
|
||||||
resErr := jsonerror.InternalServerError()
|
resErr := jsonerror.InternalServerError()
|
||||||
return nil, nil, &resErr
|
return nil, nil, &resErr
|
||||||
}
|
}
|
||||||
|
|
@ -325,6 +320,13 @@ func (r *downloadRequest) getThumbnailFile(filePath types.Path, activeThumbnailG
|
||||||
if thumbnail == nil {
|
if thumbnail == nil {
|
||||||
return nil, nil, nil
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
|
r.Logger = r.Logger.WithFields(log.Fields{
|
||||||
|
"Width": thumbnail.ThumbnailSize.Width,
|
||||||
|
"Height": thumbnail.ThumbnailSize.Height,
|
||||||
|
"ResizeMethod": thumbnail.ThumbnailSize.ResizeMethod,
|
||||||
|
"FileSizeBytes": thumbnail.MediaMetadata.FileSizeBytes,
|
||||||
|
"ContentType": thumbnail.MediaMetadata.ContentType,
|
||||||
|
})
|
||||||
thumbPath := string(thumbnailer.GetThumbnailPath(types.Path(filePath), thumbnail.ThumbnailSize))
|
thumbPath := string(thumbnailer.GetThumbnailPath(types.Path(filePath), thumbnail.ThumbnailSize))
|
||||||
thumbFile, err := os.Open(string(thumbPath))
|
thumbFile, err := os.Open(string(thumbPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -347,24 +349,21 @@ func (r *downloadRequest) getThumbnailFile(filePath types.Path, activeThumbnailG
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *downloadRequest) generateThumbnail(filePath types.Path, thumbnailSize types.ThumbnailSize, activeThumbnailGeneration *types.ActiveThumbnailGeneration, db *storage.Database) (*types.ThumbnailMetadata, *util.JSONResponse) {
|
func (r *downloadRequest) generateThumbnail(filePath types.Path, thumbnailSize types.ThumbnailSize, activeThumbnailGeneration *types.ActiveThumbnailGeneration, db *storage.Database) (*types.ThumbnailMetadata, *util.JSONResponse) {
|
||||||
|
logger := r.Logger.WithFields(log.Fields{
|
||||||
|
"Width": thumbnailSize.Width,
|
||||||
|
"Height": thumbnailSize.Height,
|
||||||
|
"ResizeMethod": thumbnailSize.ResizeMethod,
|
||||||
|
})
|
||||||
var err error
|
var err error
|
||||||
if err = thumbnailer.GenerateThumbnail(filePath, thumbnailSize, r.MediaMetadata, activeThumbnailGeneration, db, r.Logger); err != nil {
|
if err = thumbnailer.GenerateThumbnail(filePath, thumbnailSize, r.MediaMetadata, activeThumbnailGeneration, db, logger); err != nil {
|
||||||
r.Logger.WithError(err).WithFields(log.Fields{
|
logger.WithError(err).Error("Error creating thumbnail")
|
||||||
"Width": thumbnailSize.Width,
|
|
||||||
"Height": thumbnailSize.Height,
|
|
||||||
"ResizeMethod": thumbnailSize.ResizeMethod,
|
|
||||||
}).Error("Error creating thumbnail")
|
|
||||||
resErr := jsonerror.InternalServerError()
|
resErr := jsonerror.InternalServerError()
|
||||||
return nil, &resErr
|
return nil, &resErr
|
||||||
}
|
}
|
||||||
var thumbnail *types.ThumbnailMetadata
|
var thumbnail *types.ThumbnailMetadata
|
||||||
thumbnail, err = db.GetThumbnail(r.MediaMetadata.MediaID, r.MediaMetadata.Origin, thumbnailSize.Width, thumbnailSize.Height, thumbnailSize.ResizeMethod)
|
thumbnail, err = db.GetThumbnail(r.MediaMetadata.MediaID, r.MediaMetadata.Origin, thumbnailSize.Width, thumbnailSize.Height, thumbnailSize.ResizeMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Logger.WithError(err).WithFields(log.Fields{
|
logger.WithError(err).Error("Error looking up thumbnails")
|
||||||
"Width": thumbnailSize.Width,
|
|
||||||
"Height": thumbnailSize.Height,
|
|
||||||
"ResizeMethod": thumbnailSize.ResizeMethod,
|
|
||||||
}).Error("Error looking up thumbnails")
|
|
||||||
resErr := jsonerror.InternalServerError()
|
resErr := jsonerror.InternalServerError()
|
||||||
return nil, &resErr
|
return nil, &resErr
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue