From 73b8839c5d1b481d1aa1330cd8c206a9f2213333 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Fri, 22 Jul 2022 12:48:19 +0200 Subject: [bugfix] Make `/api/v2/media` more compatible with masto API (#724) * update docs * make api version into a path param * update tests * workaround to unset URL if using v2 of api * make some fields into pointers --- internal/api/client/media/mediacreate.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'internal/api/client/media/mediacreate.go') diff --git a/internal/api/client/media/mediacreate.go b/internal/api/client/media/mediacreate.go index f51d272b6..5a040b26c 100644 --- a/internal/api/client/media/mediacreate.go +++ b/internal/api/client/media/mediacreate.go @@ -31,7 +31,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/oauth" ) -// MediaCreatePOSTHandler swagger:operation POST /api/v1/media mediaCreate +// MediaCreatePOSTHandler swagger:operation POST /api/{api_version}/media mediaCreate // // Upload a new media attachment. // @@ -46,6 +46,11 @@ import ( // - application/json // // parameters: +// - name: api version +// type: string +// in: path +// description: Version of the API to use. Must be one of v1 or v2. +// required: true // - name: description // in: formData // description: |- @@ -95,6 +100,13 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) { return } + apiVersion := c.Param(APIVersionKey) + if apiVersion != "v1" && apiVersion != "v2" { + err := errors.New("api version must be one of v1 or v2") + api.ErrorHandler(c, gtserror.NewErrorNotFound(err, err.Error()), m.processor.InstanceGet) + return + } + form := &model.AttachmentRequest{} if err := c.ShouldBind(&form); err != nil { api.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGet) @@ -112,6 +124,15 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) { return } + if apiVersion == "v2" { + // the mastodon v2 media API specifies that the URL should be null + // and that the client should call /api/v1/media/:id to get the URL + // + // so even though we have the URL already, remove it now to comply + // with the api + apiAttachment.URL = nil + } + c.JSON(http.StatusOK, apiAttachment) } -- cgit v1.2.3