create.go (2385B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package media 19 20 import ( 21 "context" 22 "fmt" 23 "io" 24 25 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 26 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 27 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 28 "github.com/superseriousbusiness/gotosocial/internal/media" 29 ) 30 31 // Create creates a new media attachment belonging to the given account, using the request form. 32 func (p *Processor) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, gtserror.WithCode) { 33 data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { 34 f, err := form.File.Open() 35 return f, form.File.Size, err 36 } 37 38 focusX, focusY, err := parseFocus(form.Focus) 39 if err != nil { 40 err := fmt.Errorf("could not parse focus value %s: %s", form.Focus, err) 41 return nil, gtserror.NewErrorBadRequest(err, err.Error()) 42 } 43 44 // process the media attachment and load it immediately 45 media, err := p.mediaManager.PreProcessMedia(ctx, data, account.ID, &media.AdditionalMediaInfo{ 46 Description: &form.Description, 47 FocusX: &focusX, 48 FocusY: &focusY, 49 }) 50 if err != nil { 51 return nil, gtserror.NewErrorUnprocessableEntity(err) 52 } 53 54 attachment, err := media.LoadAttachment(ctx) 55 if err != nil { 56 return nil, gtserror.NewErrorUnprocessableEntity(err) 57 } 58 59 apiAttachment, err := p.tc.AttachmentToAPIAttachment(ctx, attachment) 60 if err != nil { 61 err := fmt.Errorf("error parsing media attachment to frontend type: %s", err) 62 return nil, gtserror.NewErrorInternalError(err) 63 } 64 65 return &apiAttachment, nil 66 }