diff --git a/CHANGELOG.md b/CHANGELOG.md index afebc7f40568144c43f68c8f0a8f3bc2629a9884..27b973977adb2bb64dbf718ce27a8beb72755fe5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), * The development environment now uses Synapse as a homeserver. Test accounts will need recreating. * Updated to Go 1.18 +### Fixed + +* Return default media attributes if none have been explicitly set. + ## [1.2.12] - March 31, 2022 ### Fixed diff --git a/api/custom/media_attributes.go b/api/custom/media_attributes.go index 6d4c6e0cfcf2471097f32b2af712ffd5ed20971b..a4923330a57bd4de1891730d471a637bff94e255 100644 --- a/api/custom/media_attributes.go +++ b/api/custom/media_attributes.go @@ -1,11 +1,13 @@ package custom import ( + "database/sql" "encoding/json" - "github.com/getsentry/sentry-go" "io/ioutil" "net/http" + "github.com/getsentry/sentry-go" + "github.com/gorilla/mux" "github.com/sirupsen/logrus" "github.com/turt2live/matrix-media-repo/api" @@ -49,9 +51,21 @@ func GetAttributes(r *http.Request, rctx rcontext.RequestContext, user api.UserI return api.AuthFailed() } + // Check to see if the media exists + mediaDb := storage.GetDatabase().GetMediaStore(rctx) + media, err := mediaDb.Get(origin, mediaId) + if err != nil && err != sql.ErrNoRows { + rctx.Log.Error(err) + sentry.CaptureException(err) + return api.InternalServerError("failed to get media record") + } + if media == nil || err == sql.ErrNoRows { + return api.NotFoundError() + } + db := storage.GetDatabase().GetMediaAttributesStore(rctx) - attrs, err := db.GetAttributes(origin, mediaId) + attrs, err := db.GetAttributesDefaulted(origin, mediaId) if err != nil { rctx.Log.Error(err) sentry.CaptureException(err)