2021-07-05 12:23:03 +01:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2021-07-05 12:23:03 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-09-04 13:02:01 +01:00
|
|
|
"time"
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-07-26 19:25:54 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-07-05 12:23:03 +01:00
|
|
|
)
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, error) {
|
2021-07-05 12:23:03 +01:00
|
|
|
// open the attachment and extract the bytes from it
|
|
|
|
f, err := form.File.Open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error opening attachment: %s", err)
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
size, err := io.Copy(buf, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading attachment: %s", err)
|
|
|
|
}
|
|
|
|
if size == 0 {
|
|
|
|
return nil, errors.New("could not read provided attachment: size 0 bytes")
|
|
|
|
}
|
|
|
|
|
2021-09-04 13:02:01 +01:00
|
|
|
// now parse the focus parameter
|
|
|
|
focusx, focusy, err := parseFocus(form.Focus)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
2021-09-04 13:02:01 +01:00
|
|
|
return nil, fmt.Errorf("couldn't parse attachment focus: %s", err)
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2021-09-04 13:02:01 +01:00
|
|
|
minAttachment := >smodel.MediaAttachment{
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
AccountID: account.ID,
|
2021-11-22 10:49:11 +00:00
|
|
|
Description: text.SanitizeCaption(form.Description),
|
2021-09-04 13:02:01 +01:00
|
|
|
FileMeta: gtsmodel.FileMeta{
|
|
|
|
Focus: gtsmodel.Focus{
|
|
|
|
X: focusx,
|
|
|
|
Y: focusy,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-05 12:23:03 +01:00
|
|
|
|
2021-09-04 13:02:01 +01:00
|
|
|
// allow the mediaHandler to work its magic of processing the attachment bytes, and putting them in whatever storage backend we're using
|
|
|
|
attachment, err := p.mediaHandler.ProcessAttachment(ctx, buf.Bytes(), minAttachment)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
2021-09-04 13:02:01 +01:00
|
|
|
return nil, fmt.Errorf("error reading attachment: %s", err)
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare the frontend representation now -- if there are any errors here at least we can bail without
|
|
|
|
// having already put something in the database and then having to clean it up again (eugh)
|
2021-10-04 14:24:19 +01:00
|
|
|
apiAttachment, err := p.tc.AttachmentToAPIAttachment(ctx, attachment)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing media attachment to frontend type: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we can confidently put the attachment in the database
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.Put(ctx, attachment); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
return nil, fmt.Errorf("error storing media attachment in db: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
return &apiAttachment, nil
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|