From 9813a044c0d6cbf8bf08b902c15a2437ec8f88ae Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Sun, 24 Apr 2022 13:11:52 +0200 Subject: [PATCH] [feature] Implement media v2 endpoint to accommodate Tusky 17 (#480) * serve v2 media api * go fmt --- internal/api/client/media/media.go | 27 ++++++++++++++----- internal/api/client/media/mediacreate_test.go | 6 ++--- internal/api/client/media/mediaupdate_test.go | 4 +-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/internal/api/client/media/media.go b/internal/api/client/media/media.go index db5f52877..c9aee64ca 100644 --- a/internal/api/client/media/media.go +++ b/internal/api/client/media/media.go @@ -26,14 +26,20 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/router" ) -// BasePath is the base API path for making media requests -const BasePath = "/api/v1/media" +// BasePathV1 is the base API path for making media requests through v1 of the api (for mastodon API compatibility) +const BasePathV1 = "/api/v1/media" + +// BasePathV2 is the base API path for making media requests through v2 of the api (for mastodon API compatibility) +const BasePathV2 = "/api/v2/media" // IDKey is the key for media attachment IDs const IDKey = "id" -// BasePathWithID corresponds to a media attachment with the given ID -const BasePathWithID = BasePath + "/:" + IDKey +// BasePathWithIDV1 corresponds to a media attachment with the given ID +const BasePathWithIDV1 = BasePathV1 + "/:" + IDKey + +// BasePathWithIDV2 corresponds to a media attachment with the given ID +const BasePathWithIDV2 = BasePathV2 + "/:" + IDKey // Module implements the ClientAPIModule interface for media type Module struct { @@ -49,8 +55,15 @@ func New(processor processing.Processor) api.ClientModule { // Route satisfies the RESTAPIModule interface func (m *Module) Route(s router.Router) error { - s.AttachHandler(http.MethodPost, BasePath, m.MediaCreatePOSTHandler) - s.AttachHandler(http.MethodGet, BasePathWithID, m.MediaGETHandler) - s.AttachHandler(http.MethodPut, BasePathWithID, m.MediaPUTHandler) + // v1 handlers + s.AttachHandler(http.MethodPost, BasePathV1, m.MediaCreatePOSTHandler) + s.AttachHandler(http.MethodGet, BasePathWithIDV1, m.MediaGETHandler) + s.AttachHandler(http.MethodPut, BasePathWithIDV1, m.MediaPUTHandler) + + // v2 handlers + s.AttachHandler(http.MethodPost, BasePathV2, m.MediaCreatePOSTHandler) + s.AttachHandler(http.MethodGet, BasePathWithIDV2, m.MediaGETHandler) + s.AttachHandler(http.MethodPut, BasePathWithIDV2, m.MediaPUTHandler) + return nil } diff --git a/internal/api/client/media/mediacreate_test.go b/internal/api/client/media/mediacreate_test.go index ca0b6483f..a283900a7 100644 --- a/internal/api/client/media/mediacreate_test.go +++ b/internal/api/client/media/mediacreate_test.go @@ -150,7 +150,7 @@ func (suite *MediaCreateTestSuite) TestMediaCreateSuccessful() { if err != nil { panic(err) } - ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePath), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting + ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePathV1), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting ctx.Request.Header.Set("Content-Type", w.FormDataContentType()) ctx.Request.Header.Set("accept", "application/json") @@ -234,7 +234,7 @@ func (suite *MediaCreateTestSuite) TestMediaCreateLongDescription() { if err != nil { panic(err) } - ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePath), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting + ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePathV1), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting ctx.Request.Header.Set("Content-Type", w.FormDataContentType()) ctx.Request.Header.Set("accept", "application/json") @@ -275,7 +275,7 @@ func (suite *MediaCreateTestSuite) TestMediaCreateTooShortDescription() { if err != nil { panic(err) } - ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePath), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting + ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePathV1), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting ctx.Request.Header.Set("Content-Type", w.FormDataContentType()) ctx.Request.Header.Set("accept", "application/json") diff --git a/internal/api/client/media/mediaupdate_test.go b/internal/api/client/media/mediaupdate_test.go index b99c89c06..9cc5a9ac5 100644 --- a/internal/api/client/media/mediaupdate_test.go +++ b/internal/api/client/media/mediaupdate_test.go @@ -140,7 +140,7 @@ func (suite *MediaUpdateTestSuite) TestUpdateImage() { if err != nil { panic(err) } - ctx.Request = httptest.NewRequest(http.MethodPut, fmt.Sprintf("http://localhost:8080/%s/%s", mediamodule.BasePath, toUpdate.ID), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting + ctx.Request = httptest.NewRequest(http.MethodPut, fmt.Sprintf("http://localhost:8080/%s/%s", mediamodule.BasePathV1, toUpdate.ID), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting ctx.Request.Header.Set("Content-Type", w.FormDataContentType()) ctx.Request.Header.Set("accept", "application/json") ctx.Params = gin.Params{ @@ -205,7 +205,7 @@ func (suite *MediaUpdateTestSuite) TestUpdateImageShortDescription() { if err != nil { panic(err) } - ctx.Request = httptest.NewRequest(http.MethodPut, fmt.Sprintf("http://localhost:8080/%s/%s", mediamodule.BasePath, toUpdate.ID), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting + ctx.Request = httptest.NewRequest(http.MethodPut, fmt.Sprintf("http://localhost:8080/%s/%s", mediamodule.BasePathV1, toUpdate.ID), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting ctx.Request.Header.Set("Content-Type", w.FormDataContentType()) ctx.Request.Header.Set("accept", "application/json") ctx.Params = gin.Params{