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
|
||||
// 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) {
|
||||
logger = logger.WithFields(log.Fields{
|
||||
"Width": config.Width,
|
||||
"Height": config.Height,
|
||||
"ResizeMethod": config.ResizeMethod,
|
||||
})
|
||||
|
||||
dst := GetThumbnailPath(src, config)
|
||||
|
||||
// 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.
|
||||
thumbnailMetadata, err := db.GetThumbnail(mediaMetadata.MediaID, mediaMetadata.Origin, config.Width, config.Height, config.ResizeMethod)
|
||||
if err != nil {
|
||||
logger.WithFields(log.Fields{
|
||||
"Width": config.Width,
|
||||
"Height": config.Height,
|
||||
"ResizeMethod": config.ResizeMethod,
|
||||
}).Error("Failed to query database for thumbnail.")
|
||||
logger.Error("Failed to query database for thumbnail.")
|
||||
return err
|
||||
}
|
||||
if thumbnailMetadata != nil {
|
||||
|
|
@ -174,11 +176,7 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
|||
|
||||
if isActive == false {
|
||||
// Note: This should not happen, but we check just in case.
|
||||
logger.WithFields(log.Fields{
|
||||
"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.")
|
||||
logger.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)
|
||||
}
|
||||
|
||||
|
|
@ -188,11 +186,8 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
|||
return err
|
||||
}
|
||||
logger.WithFields(log.Fields{
|
||||
"Width": config.Width,
|
||||
"Height": config.Height,
|
||||
"ActualWidth": width,
|
||||
"ActualHeight": height,
|
||||
"ResizeMethod": config.ResizeMethod,
|
||||
"processTime": time.Now().Sub(start),
|
||||
}).Info("Generated thumbnail")
|
||||
|
||||
|
|
@ -219,11 +214,8 @@ func createThumbnail(src types.Path, buffer []byte, config types.ThumbnailSize,
|
|||
err = db.StoreThumbnail(thumbnailMetadata)
|
||||
if err != nil {
|
||||
logger.WithError(err).WithFields(log.Fields{
|
||||
"Width": config.Width,
|
||||
"Height": config.Height,
|
||||
"ActualWidth": width,
|
||||
"ActualHeight": height,
|
||||
"ResizeMethod": config.ResizeMethod,
|
||||
}).Error("Failed to store thumbnail metadata in database.")
|
||||
return err
|
||||
}
|
||||
|
|
@ -237,11 +229,7 @@ func getActiveThumbnailGeneration(dst types.Path, config types.ThumbnailSize, ac
|
|||
activeThumbnailGeneration.Lock()
|
||||
defer activeThumbnailGeneration.Unlock()
|
||||
if activeThumbnailGenerationResult, ok := activeThumbnailGeneration.PathToResult[string(dst)]; ok {
|
||||
logger.WithFields(log.Fields{
|
||||
"Width": config.Width,
|
||||
"Height": config.Height,
|
||||
"ResizeMethod": config.ResizeMethod,
|
||||
}).Info("Waiting for another goroutine to generate the thumbnail.")
|
||||
logger.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.
|
||||
activeThumbnailGenerationResult.Cond.Wait()
|
||||
|
|
@ -263,11 +251,7 @@ func broadcastGeneration(dst types.Path, activeThumbnailGeneration *types.Active
|
|||
activeThumbnailGeneration.Lock()
|
||||
defer activeThumbnailGeneration.Unlock()
|
||||
if activeThumbnailGenerationResult, ok := activeThumbnailGeneration.PathToResult[string(dst)]; ok {
|
||||
logger.WithFields(log.Fields{
|
||||
"Width": config.Width,
|
||||
"Height": config.Height,
|
||||
"ResizeMethod": config.ResizeMethod,
|
||||
}).Info("Signalling other goroutines waiting for this goroutine to generate the thumbnail.")
|
||||
logger.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
|
||||
activeThumbnailGenerationResult.Err = errorReturn
|
||||
activeThumbnailGenerationResult.Cond.Broadcast()
|
||||
|
|
|
|||
|
|
@ -86,6 +86,11 @@ func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib
|
|||
Height: height,
|
||||
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
|
||||
|
|
@ -242,13 +247,7 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePat
|
|||
responseFile = file
|
||||
responseMetadata = r.MediaMetadata
|
||||
} else {
|
||||
r.Logger.WithFields(log.Fields{
|
||||
"Width": thumbMetadata.ThumbnailSize.Width,
|
||||
"Height": thumbMetadata.ThumbnailSize.Height,
|
||||
"ResizeMethod": thumbMetadata.ThumbnailSize.ResizeMethod,
|
||||
"FileSizeBytes": thumbMetadata.MediaMetadata.FileSizeBytes,
|
||||
"ContentType": thumbMetadata.MediaMetadata.ContentType,
|
||||
}).Info("Responding with thumbnail")
|
||||
r.Logger.Info("Responding with thumbnail")
|
||||
responseFile = thumbFile
|
||||
responseMetadata = thumbMetadata.MediaMetadata
|
||||
}
|
||||
|
|
@ -297,11 +296,7 @@ func (r *downloadRequest) getThumbnailFile(filePath types.Path, activeThumbnailG
|
|||
} else {
|
||||
thumbnails, err := db.GetThumbnails(r.MediaMetadata.MediaID, r.MediaMetadata.Origin)
|
||||
if err != nil {
|
||||
r.Logger.WithError(err).WithFields(log.Fields{
|
||||
"Width": r.ThumbnailSize.Width,
|
||||
"Height": r.ThumbnailSize.Height,
|
||||
"ResizeMethod": r.ThumbnailSize.ResizeMethod,
|
||||
}).Error("Error looking up thumbnails")
|
||||
r.Logger.WithError(err).Error("Error looking up thumbnails")
|
||||
resErr := jsonerror.InternalServerError()
|
||||
return nil, nil, &resErr
|
||||
}
|
||||
|
|
@ -325,6 +320,13 @@ func (r *downloadRequest) getThumbnailFile(filePath types.Path, activeThumbnailG
|
|||
if thumbnail == 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))
|
||||
thumbFile, err := os.Open(string(thumbPath))
|
||||
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) {
|
||||
logger := r.Logger.WithFields(log.Fields{
|
||||
"Width": thumbnailSize.Width,
|
||||
"Height": thumbnailSize.Height,
|
||||
"ResizeMethod": thumbnailSize.ResizeMethod,
|
||||
})
|
||||
var err error
|
||||
if err = thumbnailer.GenerateThumbnail(filePath, thumbnailSize, r.MediaMetadata, activeThumbnailGeneration, db, r.Logger); err != nil {
|
||||
r.Logger.WithError(err).WithFields(log.Fields{
|
||||
"Width": thumbnailSize.Width,
|
||||
"Height": thumbnailSize.Height,
|
||||
"ResizeMethod": thumbnailSize.ResizeMethod,
|
||||
}).Error("Error creating thumbnail")
|
||||
if err = thumbnailer.GenerateThumbnail(filePath, thumbnailSize, r.MediaMetadata, activeThumbnailGeneration, db, logger); err != nil {
|
||||
logger.WithError(err).Error("Error creating thumbnail")
|
||||
resErr := jsonerror.InternalServerError()
|
||||
return nil, &resErr
|
||||
}
|
||||
var thumbnail *types.ThumbnailMetadata
|
||||
thumbnail, err = db.GetThumbnail(r.MediaMetadata.MediaID, r.MediaMetadata.Origin, thumbnailSize.Width, thumbnailSize.Height, thumbnailSize.ResizeMethod)
|
||||
if err != nil {
|
||||
r.Logger.WithError(err).WithFields(log.Fields{
|
||||
"Width": thumbnailSize.Width,
|
||||
"Height": thumbnailSize.Height,
|
||||
"ResizeMethod": thumbnailSize.ResizeMethod,
|
||||
}).Error("Error looking up thumbnails")
|
||||
logger.WithError(err).Error("Error looking up thumbnails")
|
||||
resErr := jsonerror.InternalServerError()
|
||||
return nil, &resErr
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue