start updating media handler for proper processing

This commit is contained in:
tsmethurst 2021-05-10 09:56:49 +02:00
commit d797ddf704
5 changed files with 89 additions and 5 deletions

View file

@ -78,7 +78,7 @@ func (m *FileServer) ServeFile(c *gin.Context) {
return
}
content, err := m.processor.MediaGet(authed, &model.GetContentRequestForm{
content, err := m.processor.FileGet(authed, &model.GetContentRequestForm{
AccountID: accountID,
MediaType: mediaType,
MediaSize: mediaSize,

View file

@ -33,6 +33,10 @@ import (
// BasePath is the base API path for making media requests
const BasePath = "/api/v1/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
// Module implements the ClientAPIModule interface for media
type Module struct {
@ -53,6 +57,7 @@ func New(config *config.Config, processor message.Processor, log *logrus.Logger)
// 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)
return nil
}

View file

@ -0,0 +1,51 @@
package media
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
)
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// MediaGETHandler allows the owner of an attachment to get information about that attachment before it's used in a status.
func (m *Module) MediaGETHandler(c *gin.Context) {
l := m.log.WithField("func", "statusCreatePOSTHandler")
authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
l.Debugf("couldn't auth: %s", err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
attachmentID := c.GetString(IDKey)
if attachmentID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "no attachment ID given in request"})
return
}
attachment, errWithCode := m.processor.MediaGet(authed, attachmentID)
if err != nil {
c.JSON(errWithCode.Code(),gin.H{"error": errWithCode.Safe()})
return
}
c.JSON(http.StatusOK, attachment)
}