summaryrefslogtreecommitdiff
path: root/internal/message/mediaprocess.go
blob: 77b387df3bb122819cf6621f656f25e2c06eeade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package message

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"strconv"
	"strings"

	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/media"
	"github.com/superseriousbusiness/gotosocial/internal/oauth"
)

func (p *processor) MediaCreate(authed *oauth.Auth, form *apimodel.AttachmentRequest) (*apimodel.Attachment, error) {
	// First check this user/account is permitted to create media
	// There's no point continuing otherwise.
	if authed.User.Disabled || !authed.User.Approved || !authed.Account.SuspendedAt.IsZero() {
		return nil, errors.New("not authorized to post new media")
	}

	// 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")
	}

	// 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.ProcessLocalAttachment(buf.Bytes(), authed.Account.ID)
	if err != nil {
		return nil, fmt.Errorf("error reading attachment: %s", err)
	}

	// now we need to add extra fields that the attachment processor doesn't know (from the form)
	// TODO: handle this inside mediaHandler.ProcessAttachment (just pass more params to it)

	// first description
	attachment.Description = form.Description

	// now parse the focus parameter
	// TODO: tidy this up into a separate function and just return an error so all the c.JSON and return calls are obviated
	var focusx, focusy float32
	if form.Focus != "" {
		spl := strings.Split(form.Focus, ",")
		if len(spl) != 2 {
			return nil, fmt.Errorf("improperly formatted focus %s", form.Focus)
		}
		xStr := spl[0]
		yStr := spl[1]
		if xStr == "" || yStr == "" {
			return nil, fmt.Errorf("improperly formatted focus %s", form.Focus)
		}
		fx, err := strconv.ParseFloat(xStr, 32)
		if err != nil {
			return nil, fmt.Errorf("improperly formatted focus %s: %s", form.Focus, err)
		}
		if fx > 1 || fx < -1 {
			return nil, fmt.Errorf("improperly formatted focus %s", form.Focus)
		}
		focusx = float32(fx)
		fy, err := strconv.ParseFloat(yStr, 32)
		if err != nil {
			return nil, fmt.Errorf("improperly formatted focus %s: %s", form.Focus, err)
		}
		if fy > 1 || fy < -1 {
			return nil, fmt.Errorf("improperly formatted focus %s", form.Focus)
		}
		focusy = float32(fy)
	}
	attachment.FileMeta.Focus.X = focusx
	attachment.FileMeta.Focus.Y = focusy

	// 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)
	mastoAttachment, err := p.tc.AttachmentToMasto(attachment)
	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
	if err := p.db.Put(attachment); err != nil {
		return nil, fmt.Errorf("error storing media attachment in db: %s", err)
	}

	return &mastoAttachment, nil
}

func (p *processor) MediaGet(authed *oauth.Auth, form *apimodel.GetContentRequestForm) (*apimodel.Content, error) {
	// parse the form fields
	mediaSize, err := media.ParseMediaSize(form.MediaSize)
	if err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("media size %s not valid", form.MediaSize))
	}

	mediaType, err := media.ParseMediaType(form.MediaType)
	if err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("media type %s not valid", form.MediaType))
	}

	spl := strings.Split(form.FileName, ".")
	if len(spl) != 2 || spl[0] == "" || spl[1] == "" {
		return nil, NewErrorNotFound(fmt.Errorf("file name %s not parseable", form.FileName))
	}
	wantedMediaID := spl[0]

	// get the account that owns the media and make sure it's not suspended
	acct := &gtsmodel.Account{}
	if err := p.db.GetByID(form.AccountID, acct); err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("account with id %s could not be selected from the db: %s", form.AccountID, err))
	}
	if !acct.SuspendedAt.IsZero() {
		return nil, NewErrorNotFound(fmt.Errorf("account with id %s is suspended", form.AccountID))
	}

	// make sure the requesting account and the media account don't block each other
	if authed.Account != nil {
		blocked, err := p.db.Blocked(authed.Account.ID, form.AccountID)
		if err != nil {
			return nil, NewErrorNotFound(fmt.Errorf("block status could not be established between accounts %s and %s: %s", form.AccountID, authed.Account.ID, err))
		}
		if blocked {
			return nil, NewErrorNotFound(fmt.Errorf("block exists between accounts %s and %s", form.AccountID, authed.Account.ID))
		}
	}

	// the way we store emojis is a little different from the way we store other attachments,
	// so we need to take different steps depending on the media type being requested
	content := &apimodel.Content{}
	var storagePath string
	switch mediaType {
	case media.Emoji:
		e := &gtsmodel.Emoji{}
		if err := p.db.GetByID(wantedMediaID, e); err != nil {
			return nil, NewErrorNotFound(fmt.Errorf("emoji %s could not be taken from the db: %s", wantedMediaID, err))
		}
		if e.Disabled {
			return nil, NewErrorNotFound(fmt.Errorf("emoji %s has been disabled", wantedMediaID))
		}
		switch mediaSize {
		case media.Original:
			content.ContentType = e.ImageContentType
			storagePath = e.ImagePath
		case media.Static:
			content.ContentType = e.ImageStaticContentType
			storagePath = e.ImageStaticPath
		default:
			return nil, NewErrorNotFound(fmt.Errorf("media size %s not recognized for emoji", mediaSize))
		}
	case media.Attachment, media.Header, media.Avatar:
		a := &gtsmodel.MediaAttachment{}
		if err := p.db.GetByID(wantedMediaID, a); err != nil {
			return nil, NewErrorNotFound(fmt.Errorf("attachment %s could not be taken from the db: %s", wantedMediaID, err))
		}
		if a.AccountID != form.AccountID {
			return nil, NewErrorNotFound(fmt.Errorf("attachment %s is not owned by %s", wantedMediaID, form.AccountID))
		}
		switch mediaSize {
		case media.Original:
			content.ContentType = a.File.ContentType
			storagePath = a.File.Path
		case media.Small:
			content.ContentType = a.Thumbnail.ContentType
			storagePath = a.Thumbnail.Path
		default:
			return nil, NewErrorNotFound(fmt.Errorf("media size %s not recognized for attachment", mediaSize))
		}
	}

	bytes, err := p.storage.RetrieveFileFrom(storagePath)
	if err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("error retrieving from storage: %s", err))
	}

	content.ContentLength = int64(len(bytes))
	content.Content = bytes
	return content, nil
}