From fc9fc24719d248e35c3344d0d784bec7f7f8c774 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Thu, 9 Feb 2023 16:34:44 -0700
Subject: [PATCH] Return default media attributes if none have been explicitly
 set.

---
 CHANGELOG.md                   |  4 ++++
 api/custom/media_attributes.go | 18 ++++++++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index afebc7f4..27b97397 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 6d4c6e0c..a4923330 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)
-- 
GitLab