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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 (
"context"
"io"
"strings"
"time"
"codeberg.org/gruf/go-iotools"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/storage"
"github.com/superseriousbusiness/gotosocial/internal/uris"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
var SupportedMIMETypes = []string{
"image/jpeg", // .jpeg
"image/gif", // .gif
"image/webp", // .webp
"audio/mp2", // .mp2
"audio/mp3", // .mp3
"video/x-msvideo", // .avi
"audio/flac", // .flac
"audio/x-flac", // .flac
// png types
"image/png", // .png
"image/apng", // .apng
// ogg types
"audio/ogg", // .ogg
"video/ogg", // .ogv
// mpeg4 types
"audio/mp4", // .m4a
"video/mp4", // .mp4
"video/quicktime", // .mov
// asf types
"audio/x-ms-wma", // .wma
"video/x-ms-wmv", // .wmv
// matroska types
"video/webm", // .webm
"audio/x-matroska", // .mka
"video/x-matroska", // .mkv
}
var SupportedEmojiMIMETypes = []string{
"image/jpeg", // .jpeg
"image/gif", // .gif
"image/webp", // .webp
// png types
"image/png", // .png
"image/apng", // .apng
}
type Manager struct {
state *state.State
}
// NewManager returns a media manager with given state.
func NewManager(state *state.State) *Manager {
return &Manager{state: state}
}
// CreateMedia creates a new media attachment entry
// in the database for given owning account ID and
// extra information, and prepares a new processing
// media entry to dereference it using the given
// data function, decode the media and finish filling
// out remaining media fields (e.g. type, path, etc).
func (m *Manager) CreateMedia(
ctx context.Context,
accountID string,
data DataFunc,
info AdditionalMediaInfo,
) (
*ProcessingMedia,
error,
) {
now := time.Now()
// Populate initial fields on the new media,
// leaving out fields with values we don't know
// yet. These will be overwritten as we go.
attachment := >smodel.MediaAttachment{
ID: id.NewULID(),
AccountID: accountID,
Type: gtsmodel.FileTypeUnknown,
Processing: gtsmodel.ProcessingStatusReceived,
Avatar: util.Ptr(false),
Header: util.Ptr(false),
Cached: util.Ptr(false),
CreatedAt: now,
}
// Check if we were provided additional info
// to add to the attachment, and overwrite
// some of the attachment fields if so.
if info.StatusID != nil {
attachment.StatusID = *info.StatusID
}
if info.RemoteURL != nil {
attachment.RemoteURL = *info.RemoteURL
}
if info.Description != nil {
attachment.Description = *info.Description
}
if info.ScheduledStatusID != nil {
attachment.ScheduledStatusID = *info.ScheduledStatusID
}
if info.Blurhash != nil {
attachment.Blurhash = *info.Blurhash
}
if info.Avatar != nil {
attachment.Avatar = info.Avatar
}
if info.Header != nil {
attachment.Header = info.Header
}
if info.FocusX != nil {
attachment.FileMeta.Focus.X = *info.FocusX
}
if info.FocusY != nil {
attachment.FileMeta.Focus.Y = *info.FocusY
}
// Store attachment in database in initial form.
err := m.state.DB.PutAttachment(ctx, attachment)
if err != nil {
return nil, err
}
// Pass prepared media as ready to be cached.
return m.CacheMedia(attachment, data), nil
}
// CacheMedia wraps a media model (assumed already
// inserted in the database!) with given data function
// to perform a blocking dereference / decode operation
// from the data stream returned.
func (m *Manager) CacheMedia(
media *gtsmodel.MediaAttachment,
data DataFunc,
) *ProcessingMedia {
return &ProcessingMedia{
media: media,
dataFn: data,
mgr: m,
}
}
// CreateEmoji creates a new emoji entry in the
// database for given shortcode, domain and extra
// information, and prepares a new processing emoji
// entry to dereference it using the given data
// function, decode the media and finish filling
// out remaining fields (e.g. type, path, etc).
func (m *Manager) CreateEmoji(
ctx context.Context,
shortcode string,
domain string,
data DataFunc,
info AdditionalEmojiInfo,
) (
*ProcessingEmoji,
error,
) {
now := time.Now()
// Generate new ID.
id := id.NewULID()
if domain == "" && info.URI == nil {
// Generate URI for local emoji.
uri := uris.URIForEmoji(id)
info.URI = &uri
}
// Populate initial fields on the new emoji,
// leaving out fields with values we don't know
// yet. These will be overwritten as we go.
emoji := >smodel.Emoji{
ID: id,
Shortcode: shortcode,
Domain: domain,
Disabled: util.Ptr(false),
VisibleInPicker: util.Ptr(true),
CreatedAt: now,
UpdatedAt: now,
}
// Finally, create new emoji.
return m.createOrUpdateEmoji(ctx,
m.state.DB.PutEmoji,
data,
emoji,
info,
)
}
// UpdateEmoji prepares an update operation for the given emoji,
// which is assumed to already exist in the database.
//
// Calling load on the returned *ProcessingEmoji will update the
// db entry with provided extra information, ensure emoji images
// are cached, and use new storage paths for the dereferenced media
// files to skirt around browser caching of the old files.
func (m *Manager) UpdateEmoji(
ctx context.Context,
emoji *gtsmodel.Emoji,
data DataFunc,
info AdditionalEmojiInfo,
) (
*ProcessingEmoji,
error,
) {
// Create references to old emoji image
// paths before they get updated with new
// path ID. These are required for later
// deleting the old image files on refresh.
shortcodeDomain := emoji.ShortcodeDomain()
oldStaticPath := emoji.ImageStaticPath
oldPath := emoji.ImagePath
// Since this is a refresh we will end up storing new images at new
// paths, so we should wrap closer to delete old paths at completion.
wrapped := func(ctx context.Context) (io.ReadCloser, error) {
// Call original func.
rc, err := data(ctx)
if err != nil {
return nil, err
}
// Cast as separated reader / closer types.
rct, ok := rc.(*iotools.ReadCloserType)
if !ok {
// Allocate new read closer type.
rct = new(iotools.ReadCloserType)
rct.Reader = rc
rct.Closer = rc
}
// Wrap underlying io.Closer type to cleanup old data.
rct.Closer = iotools.CloserCallback(rct.Closer, func() {
// Remove any *old* emoji image file path now stream is closed.
if err := m.state.Storage.Delete(ctx, oldPath); err != nil &&
!storage.IsNotFound(err) {
log.Errorf(ctx, "error deleting old emoji %s from storage: %v", shortcodeDomain, err)
}
// Remove any *old* emoji static image file path now stream is closed.
if err := m.state.Storage.Delete(ctx, oldStaticPath); err != nil &&
!storage.IsNotFound(err) {
log.Errorf(ctx, "error deleting old static emoji %s from storage: %v", shortcodeDomain, err)
}
})
return rct, nil
}
// Update existing emoji in database.
processingEmoji, err := m.createOrUpdateEmoji(ctx,
func(ctx context.Context, emoji *gtsmodel.Emoji) error {
return m.state.DB.UpdateEmoji(ctx, emoji)
},
wrapped,
emoji,
info,
)
if err != nil {
return nil, err
}
// Generate a new path ID to use instead.
processingEmoji.newPathID = id.NewULID()
return processingEmoji, nil
}
// CacheEmoji wraps an emoji model (assumed already
// inserted in the database!) with given data function
// to perform a blocking dereference / decode operation
// from the data stream returned.
func (m *Manager) CacheEmoji(
ctx context.Context,
emoji *gtsmodel.Emoji,
data DataFunc,
) (
*ProcessingEmoji,
error,
) {
// Fetch the local instance account for emoji path generation.
instanceAcc, err := m.state.DB.GetInstanceAccount(ctx, "")
if err != nil {
return nil, gtserror.Newf("error fetching instance account: %w", err)
}
var pathID string
// Look for an emoji path ID that differs from its actual ID, this indicates
// a previous 'refresh'. We need to be sure to set this on the ProcessingEmoji{}
// so it knows to store the emoji under this path, and not default to emoji.ID.
if id := extractEmojiPathID(emoji.ImagePath); id != emoji.ID {
pathID = id
}
return &ProcessingEmoji{
newPathID: pathID,
instAccID: instanceAcc.ID,
emoji: emoji,
dataFn: data,
mgr: m,
}, nil
}
// createOrUpdateEmoji updates the emoji according to
// provided additional data, and performs the actual
// database write, finally returning an emoji ready
// for processing (i.e. caching to local storage).
func (m *Manager) createOrUpdateEmoji(
ctx context.Context,
storeDB func(context.Context, *gtsmodel.Emoji) error,
data DataFunc,
emoji *gtsmodel.Emoji,
info AdditionalEmojiInfo,
) (
*ProcessingEmoji,
error,
) {
// Fetch the local instance account for emoji path generation.
instanceAcc, err := m.state.DB.GetInstanceAccount(ctx, "")
if err != nil {
return nil, gtserror.Newf("error fetching instance account: %w", err)
}
// Check if we have additional info to add to the emoji,
// and overwrite some of the emoji fields if so.
if info.URI != nil {
emoji.URI = *info.URI
}
if info.Domain != nil {
emoji.Domain = *info.Domain
}
if info.ImageRemoteURL != nil {
emoji.ImageRemoteURL = *info.ImageRemoteURL
}
if info.ImageStaticRemoteURL != nil {
emoji.ImageStaticRemoteURL = *info.ImageStaticRemoteURL
}
if info.Disabled != nil {
emoji.Disabled = info.Disabled
}
if info.VisibleInPicker != nil {
emoji.VisibleInPicker = info.VisibleInPicker
}
if info.CategoryID != nil {
emoji.CategoryID = *info.CategoryID
}
// Put or update emoji in database.
if err := storeDB(ctx, emoji); err != nil {
return nil, err
}
// Return wrapped emoji for later processing.
processingEmoji := &ProcessingEmoji{
instAccID: instanceAcc.ID,
emoji: emoji,
dataFn: data,
mgr: m,
}
return processingEmoji, nil
}
// extractEmojiPathID pulls the ID used in the final path segment of an emoji path (can be URL).
func extractEmojiPathID(path string) string {
// Look for '.' indicating file ext.
i := strings.LastIndexByte(path, '.')
if i == -1 {
return ""
}
// Strip ext.
path = path[:i]
// Look for '/' of final path sep.
i = strings.LastIndexByte(path, '/')
if i == -1 {
return ""
}
// Strip up to
// final segment.
path = path[i+1:]
return path
}
|