@ -35,9 +35,10 @@ func (m *Module) OauthTokenMiddleware(c *gin.Context) {
|
||||
|
||||
ti, err := m.server.ValidationBearerToken(c.Request)
|
||||
if err != nil {
|
||||
l.Trace("no valid token presented: continuing with unauthenticated request")
|
||||
l.Tracef("could not validate token: %s", err)
|
||||
return
|
||||
}
|
||||
l.Trace("continuing with unauthenticated request")
|
||||
c.Set(oauth.SessionAuthorizedToken, ti)
|
||||
l.Tracef("set gin context %s to %+v", oauth.SessionAuthorizedToken, ti)
|
||||
|
||||
|
@ -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,
|
||||
@ -90,5 +90,14 @@ func (m *FileServer) ServeFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: do proper content negotiation here -- if the requester only accepts text/html we should try to serve them *something*
|
||||
// This is mostly needed because when sharing a link to a gts-hosted file on something like mastodon, the masto servers will
|
||||
// attempt to look up the content to provide a preview of the link, and they ask for text/html.
|
||||
if c.NegotiateFormat(content.ContentType) == "" {
|
||||
l.Debugf("couldn't negotiate content for Accept headers %+v: we have content type %s", c.Request.Header.Get("Accepted"), content.ContentType)
|
||||
c.AbortWithStatus(http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
c.DataFromReader(http.StatusOK, content.ContentLength, content.ContentType, bytes.NewReader(content.Content), nil)
|
||||
}
|
||||
|
@ -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,8 @@ 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)
|
||||
s.AttachHandler(http.MethodPut, BasePathWithID, m.MediaPUTHandler)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -41,8 +41,8 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) {
|
||||
|
||||
// extract the media create form from the request context
|
||||
l.Tracef("parsing request form: %s", c.Request.Form)
|
||||
form := &model.AttachmentRequest{}
|
||||
if err := c.ShouldBind(form); err != nil || form == nil {
|
||||
var form model.AttachmentRequest
|
||||
if err := c.ShouldBind(&form); err != nil {
|
||||
l.Debugf("could not parse form from request: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing one or more required form values"})
|
||||
return
|
||||
@ -50,19 +50,19 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) {
|
||||
|
||||
// Give the fields on the request form a first pass to make sure the request is superficially valid.
|
||||
l.Tracef("validating form %+v", form)
|
||||
if err := validateCreateMedia(form, m.config.MediaConfig); err != nil {
|
||||
if err := validateCreateMedia(&form, m.config.MediaConfig); err != nil {
|
||||
l.Debugf("error validating form: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
mastoAttachment, err := m.processor.MediaCreate(authed, form)
|
||||
mastoAttachment, err := m.processor.MediaCreate(authed, &form)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusAccepted, mastoAttachment)
|
||||
c.JSON(http.StatusOK, mastoAttachment)
|
||||
}
|
||||
|
||||
func validateCreateMedia(form *model.AttachmentRequest, config *config.MediaConfig) error {
|
||||
|
51
internal/api/client/media/mediaget.go
Normal file
51
internal/api/client/media/mediaget.go
Normal 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", "MediaGETHandler")
|
||||
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.Param(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 errWithCode != nil {
|
||||
c.JSON(errWithCode.Code(),gin.H{"error": errWithCode.Safe()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, attachment)
|
||||
}
|
87
internal/api/client/media/mediaupdate.go
Normal file
87
internal/api/client/media/mediaupdate.go
Normal file
@ -0,0 +1,87 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"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/>.
|
||||
*/
|
||||
|
||||
// MediaPUTHandler allows the owner of an attachment to update information about that attachment before it's used in a status.
|
||||
func (m *Module) MediaPUTHandler(c *gin.Context) {
|
||||
l := m.log.WithField("func", "MediaGETHandler")
|
||||
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.Param(IDKey)
|
||||
if attachmentID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no attachment ID given in request"})
|
||||
return
|
||||
}
|
||||
|
||||
// extract the media update form from the request context
|
||||
l.Tracef("parsing request form: %s", c.Request.Form)
|
||||
var form model.AttachmentUpdateRequest
|
||||
if err := c.ShouldBind(&form); err != nil {
|
||||
l.Debugf("could not parse form from request: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing one or more required form values"})
|
||||
return
|
||||
}
|
||||
|
||||
// Give the fields on the request form a first pass to make sure the request is superficially valid.
|
||||
l.Tracef("validating form %+v", form)
|
||||
if err := validateUpdateMedia(&form, m.config.MediaConfig); err != nil {
|
||||
l.Debugf("error validating form: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
attachment, errWithCode := m.processor.MediaUpdate(authed, attachmentID, &form)
|
||||
if errWithCode != nil {
|
||||
c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, attachment)
|
||||
}
|
||||
|
||||
func validateUpdateMedia(form *model.AttachmentUpdateRequest, config *config.MediaConfig) error {
|
||||
|
||||
if form.Description != nil {
|
||||
if len(*form.Description) < config.MinDescriptionChars || len(*form.Description) > config.MaxDescriptionChars {
|
||||
return fmt.Errorf("image description length must be between %d and %d characters (inclusive), but provided image description was %d chars", config.MinDescriptionChars, config.MaxDescriptionChars, len(*form.Description))
|
||||
}
|
||||
}
|
||||
|
||||
if form.Focus == nil && form.Description == nil {
|
||||
return errors.New("focus and description were both nil, there's nothing to update")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -49,13 +49,14 @@ func (m *Module) StatusCreatePOSTHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
// extract the status create form from the request context
|
||||
l.Tracef("parsing request form: %s", c.Request.Form)
|
||||
l.Debugf("parsing request form: %s", c.Request.Form)
|
||||
form := &model.AdvancedStatusCreateForm{}
|
||||
if err := c.ShouldBind(form); err != nil || form == nil {
|
||||
l.Debugf("could not parse form from request: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing one or more required form values"})
|
||||
return
|
||||
}
|
||||
l.Debugf("handling status request form: %+v", form)
|
||||
|
||||
// Give the fields on the request form a first pass to make sure the request is superficially valid.
|
||||
l.Tracef("validating form %+v", form)
|
||||
|
@ -23,10 +23,16 @@ import "mime/multipart"
|
||||
// AttachmentRequest represents the form data parameters submitted by a client during a media upload request.
|
||||
// See: https://docs.joinmastodon.org/methods/statuses/media/
|
||||
type AttachmentRequest struct {
|
||||
File *multipart.FileHeader `form:"file"`
|
||||
Thumbnail *multipart.FileHeader `form:"thumbnail"`
|
||||
Description string `form:"description"`
|
||||
Focus string `form:"focus"`
|
||||
File *multipart.FileHeader `form:"file" binding:"required"`
|
||||
Description string `form:"description" json:"description" xml:"description"`
|
||||
Focus string `form:"focus" json:"focus" xml:"focus"`
|
||||
}
|
||||
|
||||
// AttachmentRequest represents the form data parameters submitted by a client during a media update/PUT request.
|
||||
// See: https://docs.joinmastodon.org/methods/statuses/media/
|
||||
type AttachmentUpdateRequest struct {
|
||||
Description *string `form:"description" json:"description" xml:"description"`
|
||||
Focus *string `form:"focus" json:"focus" xml:"focus"`
|
||||
}
|
||||
|
||||
// Attachment represents the object returned to a client after a successful media upload request.
|
||||
@ -57,7 +63,7 @@ type Attachment struct {
|
||||
// See https://docs.joinmastodon.org/methods/statuses/media/#focal-points points for more.
|
||||
Meta MediaMeta `json:"meta,omitempty"`
|
||||
// Alternate text that describes what is in the media attachment, to be used for the visually impaired or when media attachments do not load.
|
||||
Description string `json:"description,omitempty"`
|
||||
Description string `json:"description"`
|
||||
// A hash computed by the BlurHash algorithm, for generating colorful preview thumbnails when media has not been downloaded yet.
|
||||
// See https://github.com/woltapp/blurhash
|
||||
Blurhash string `json:"blurhash,omitempty"`
|
||||
|
@ -88,7 +88,7 @@ type StatusCreateRequest struct {
|
||||
// Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
|
||||
Status string `form:"status"`
|
||||
// Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used.
|
||||
MediaIDs []string `form:"media_ids"`
|
||||
MediaIDs []string `form:"media_ids" json:"media_ids" xml:"media_ids"`
|
||||
// Poll to include with this status.
|
||||
Poll *PollRequest `form:"poll"`
|
||||
// ID of the status being replied to, if status is a reply
|
||||
|
Reference in New Issue
Block a user