This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
624 lines (568 loc) · 14.6 KB
/
file.go
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package main
import (
"context"
"fmt"
"io"
"mime/multipart"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"github.com/getsentry/sentry-go"
"github.com/minio/minio-go/v7"
"github.com/vmihailenco/msgpack/v5"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type File struct {
Id string `bson:"_id" json:"id"`
Hash string `bson:"hash" json:"-"`
Bucket string `bson:"bucket" json:"-"`
Mime string `bson:"mime" json:"mime"`
ThumbnailMime string `bson:"thumbnail_mime,omitempty" json:"thumbnail_mime,omitempty"`
Size int64 `bson:"size" json:"size"`
ThumbnailSize int64 `bson:"thumbnail_size,omitempty" json:"thumbnail_size,omitempty"`
Filename string `bson:"filename,omitempty" json:"filename,omitempty"`
Width int `bson:"width,omitempty" json:"width,omitempty"`
Height int `bson:"height,omitempty" json:"height,omitempty"`
UploadRegion string `bson:"upload_region" json:"-"`
UploadedBy string `bson:"uploaded_by" json:"-"`
UploadedAt int64 `bson:"uploaded_at" json:"-"`
Claimed bool `bson:"claimed" json:"-"`
}
func IngestMultipartFile(
bucket string,
file multipart.File,
fileHeader *multipart.FileHeader,
uploader *User,
) (*File, error) {
// Init vars
var f File
var wg sync.WaitGroup
var err error
var id, hashHex string
var info minio.UploadInfo
// Create file ID
id, err = generateId()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
// Create directory in ingest directory for temporary files
ingestDir := fmt.Sprint(os.Getenv("INGEST_DIR"), "/", id)
defer os.RemoveAll(ingestDir)
if err := os.Mkdir(ingestDir, 0700); err != nil {
sentry.CaptureException(err)
return nil, err
}
// Save file
dst, err := os.Create(fmt.Sprint(ingestDir, "/original"))
if err != nil {
sentry.CaptureException(err)
return nil, err
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
sentry.CaptureException(err)
return nil, err
}
// "Ultra HD"
if uploader.Flags&FlagUltraHDUploads != 0 {
out, _ := exec.Command(
"file",
"--mime-type",
fmt.Sprint(ingestDir, "/original"),
).Output()
if strings.HasPrefix(strings.Fields(string(out))[1], "image/") {
exec.Command(
"magick",
fmt.Sprint(ingestDir, "/original"),
"-quality",
"5",
fmt.Sprint(ingestDir, "/original.jpg"),
).Run()
os.Rename(fmt.Sprint(ingestDir, "/original.jpg"), fmt.Sprint(ingestDir, "/original"))
}
}
// Get file hash
var out []byte
out, err = exec.Command(
"sha256sum",
fmt.Sprint(ingestDir, "/original"),
).Output()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
hashHex = strings.Fields(string(out))[0]
// Make sure file isn't blocked
if blocked, err := getBlockStatus(hashHex); blocked || err != nil {
os.Remove(fmt.Sprint(ingestDir, "/original"))
if blocked {
return nil, ErrFileBlocked
}
return nil, err
}
// Attempt to get existing file details
err = db.Collection("files").FindOne(
context.TODO(),
bson.M{"hash": hashHex, "bucket": bucket},
).Decode(&f)
if err != nil && err != mongo.ErrNoDocuments {
return nil, err
}
// Process and save file
if f.Hash == hashHex {
f.Id = id
f.Filename = cleanFilename(fileHeader.Filename)
f.UploadedBy = uploader.Username
f.UploadedAt = time.Now().Unix()
f.Claimed = false
} else {
// Create file details
f = File{
Id: id,
Hash: hashHex,
Bucket: bucket,
Filename: cleanFilename(fileHeader.Filename),
UploadRegion: s3RegionOrder[0],
UploadedBy: uploader.Username,
UploadedAt: time.Now().Unix(),
}
// Get mime
out, err = exec.Command(
"file",
"--mime-type",
fmt.Sprint(ingestDir, "/original"),
).Output()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
f.Mime = strings.Fields(string(out))[1]
// Get dimensions and number of frames, if it is an image
if strings.HasPrefix(f.Mime, "image/") {
out, err = exec.Command(
"magick",
"identify",
"-format",
"%w,%h",
fmt.Sprint(ingestDir, "/original"),
).Output()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
outSlice := strings.Split(string(out), ",")
f.Width, _ = strconv.Atoi(outSlice[0])
f.Height, _ = strconv.Atoi(outSlice[1])
}
if bucket == "icons" || bucket == "emojis" || bucket == "stickers" {
// Make sure the file is an image
if !strings.HasPrefix(f.Mime, "image/") {
return nil, ErrUnsupportedFile
}
// Get frames
out, err = exec.Command(
"magick",
"identify",
"-format",
"%n",
fmt.Sprint(ingestDir, "/original"),
).Output()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
frames := string(out)
// Choose format to convert to and update mime
format := "webp"
f.Mime = "image/webp"
if frames != "1" {
format = "gif"
f.Mime = "image/gif"
}
// If one of the axis is less than n, use that size rather than n
var desiredSize int
switch bucket {
case "icons":
desiredSize = 256
case "emojis":
desiredSize = 128
case "stickers":
desiredSize = 384
}
if f.Width < desiredSize {
desiredSize = f.Width
} else if f.Height < desiredSize {
desiredSize = f.Height
}
// Remove Exif, optimize, and resize
err = exec.Command(
"magick",
fmt.Sprint(ingestDir, "/original"),
"-quality",
"90",
"-resize",
fmt.Sprint(desiredSize, "x", desiredSize),
"-auto-orient",
"-strip",
fmt.Sprint(ingestDir, "/.", format),
).Run()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
// Upload to bucket
wg.Add(1)
go func() {
defer wg.Done()
info, err = s3Clients[s3RegionOrder[0]].FPutObject(
ctx,
bucket,
hashHex,
fmt.Sprint(ingestDir, "/.", format),
minio.PutObjectOptions{
ContentType: fmt.Sprint("image/", format),
},
)
f.Size = info.Size
}()
// Get new width and height
wg.Add(1)
go func() {
defer wg.Done()
out, _ = exec.Command(
"magick",
"identify",
"-format",
"%w,%h",
fmt.Sprint(ingestDir, "/.", format),
).Output()
outSlice := strings.Split(string(out), ",")
f.Width, _ = strconv.Atoi(outSlice[0])
f.Height, _ = strconv.Atoi(outSlice[1])
}()
wg.Wait()
} else if bucket == "attachments" {
if strings.HasPrefix(f.Mime, "image") { // Images
// Remove Exif and optimize
err = exec.Command(
"magick",
fmt.Sprint(ingestDir, "/original"),
"-quality",
"90",
"-auto-orient",
"-strip",
fmt.Sprint(ingestDir, "/optimized"),
).Run()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
// Upload optimized to bucket
wg.Add(1)
go func() {
defer wg.Done()
info, err = s3Clients[s3RegionOrder[0]].FPutObject(
ctx,
bucket,
hashHex,
fmt.Sprint(ingestDir, "/optimized"),
minio.PutObjectOptions{
ContentType: f.Mime,
},
)
f.Size = info.Size
}()
// Generate thumbnail
wg.Add(1)
go func() {
defer wg.Done()
err = f.GenerateThumbnail()
}()
wg.Wait()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
} else if strings.HasPrefix(f.Mime, "video") { // Videos
// Start uploading video to bucket
wg.Add(1)
go func() {
defer wg.Done()
info, err = s3Clients[s3RegionOrder[0]].FPutObject(
ctx,
bucket,
hashHex,
fmt.Sprint(ingestDir, "/original"),
minio.PutObjectOptions{
ContentType: f.Mime,
},
)
f.Size = info.Size
}()
// Get first frame
err = exec.Command(
"ffmpeg",
"-i",
fmt.Sprint(ingestDir, "/original"),
"-vf",
"select=eq(n\\,0)",
"-vsync",
"vfr",
"-q:v",
"2",
fmt.Sprint(ingestDir, "/first_frame.jpg"),
).Run()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
// Get dimensions from first frame
out, err = exec.Command(
"magick",
"identify",
"-format",
"%w,%h",
fmt.Sprint(ingestDir, "/first_frame.jpg"),
).Output()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
outSlice := strings.Split(string(out), ",")
f.Width, _ = strconv.Atoi(outSlice[0])
f.Height, _ = strconv.Atoi(outSlice[1])
// Generate thumbnail
wg.Add(1)
go func() {
defer wg.Done()
err = f.GenerateThumbnail()
}()
wg.Wait()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
} else { // Everything else
info, err = s3Clients[s3RegionOrder[0]].FPutObject(
ctx,
bucket,
hashHex,
fmt.Sprint(ingestDir, "/original"),
minio.PutObjectOptions{
ContentType: f.Mime,
},
)
if err != nil {
sentry.CaptureException(err)
return nil, err
}
f.Size = info.Size
}
}
}
// Send to files automod
marshaledEvent, err := msgpack.Marshal(map[string]interface{}{
"type": 0,
"username": f.UploadedBy,
"file_bucket": f.Bucket,
"file_hashes": []string{f.Hash},
})
if err := rdb.Publish(context.TODO(), "automod:files", marshaledEvent).Err(); err != nil {
fmt.Println(err)
sentry.CaptureException(err)
}
// Create database item
if _, err := db.Collection("files").InsertOne(context.TODO(), &f); err != nil {
sentry.CaptureException(err)
return &f, err
}
sentry.CaptureMessage(fmt.Sprintf("Uploaded file %s with hash %s to %s region", f.Id, f.Hash, f.UploadRegion))
return &f, nil
}
func GetFile(id string) (File, error) {
var f File
err := db.Collection("files").FindOne(
context.TODO(),
bson.M{"_id": id, "uploaded_at": bson.M{"$ne": 0}},
).Decode(&f)
return f, err
}
func (f *File) GenerateThumbnail() error {
// Create directory in ingest directory for temporary files
// And download file for processing
ingestDir := fmt.Sprint(os.Getenv("INGEST_DIR"), "/", f.Id)
if _, err := os.Stat(ingestDir); os.IsNotExist(err) {
defer os.RemoveAll(ingestDir)
if err := os.Mkdir(ingestDir, 0700); err != nil {
sentry.CaptureException(err)
return err
}
obj, _, err := f.GetObject(false)
if err != nil {
sentry.CaptureException(err)
return err
}
dst, err := os.Create(fmt.Sprint(ingestDir, "/original"))
if err != nil {
sentry.CaptureException(err)
return err
}
defer dst.Close()
if _, err := io.Copy(dst, obj); err != nil {
sentry.CaptureException(err)
return err
}
}
// Choose format to use for the thumbnail
format := "webp"
if strings.HasPrefix(f.Mime, "image/") { // use GIF for animated images
out, err := exec.Command(
"magick",
"identify",
"-format",
"%n",
fmt.Sprint(ingestDir, "/original"),
).Output()
if err != nil {
sentry.CaptureException(err)
return err
}
frames := string(out)
if frames != "1" {
format = "gif"
}
}
// Use largest axis that is smaller than 480px
var desiredSize int
if f.Width > f.Height {
desiredSize = f.Width
} else {
desiredSize = f.Height
}
if desiredSize > 480 {
desiredSize = 480
}
// Get first frame if it's a video
if strings.HasPrefix(f.Mime, "video/") {
if _, err := os.Stat(fmt.Sprint(ingestDir, "/first_frame.jpg")); os.IsNotExist(err) {
if err := exec.Command(
"ffmpeg",
"-i",
fmt.Sprint(ingestDir, "/original"),
"-vf",
"select=eq(n\\,0)",
"-vsync",
"vfr",
"-q:v",
"2",
fmt.Sprint(ingestDir, "/first_frame.jpg"),
).Run(); err != nil {
sentry.CaptureException(err)
return err
}
}
}
// Create thumbnail
fp := fmt.Sprint(ingestDir, "/original")
if strings.HasPrefix(f.Mime, "video/") {
fp = fmt.Sprint(ingestDir, "/first_frame.jpg")
}
if err := exec.Command(
"magick",
fp,
"-resize",
fmt.Sprint(desiredSize, "x", desiredSize),
fmt.Sprint(ingestDir, "/thumbnail.", format),
).Run(); err != nil {
sentry.CaptureException(err)
return err
}
// Upload thumbnail
uploadInfo, err := s3Clients[s3RegionOrder[0]].FPutObject(
ctx,
f.Bucket,
fmt.Sprint(f.Hash, "_thumbnail"),
fmt.Sprint(ingestDir, "/thumbnail.", format),
minio.PutObjectOptions{
ContentType: fmt.Sprint("image/", format),
},
)
if err != nil {
sentry.CaptureException(err)
return err
}
// Update file details
f.ThumbnailMime = fmt.Sprint("image/", format)
f.ThumbnailSize = uploadInfo.Size
if _, err := db.Collection("files").UpdateMany(
context.TODO(),
bson.M{"hash": f.Hash, "bucket": f.Bucket},
bson.M{"$set": bson.M{
"thumbnail_mime": f.ThumbnailMime,
"thumbnail_size": f.ThumbnailSize,
}},
); err != nil {
sentry.CaptureException(err)
return err
}
return nil
}
func (f *File) GetObject(thumbnail bool) (*minio.Object, *minio.ObjectInfo, error) {
objName := f.Hash
if thumbnail && f.Bucket == "attachments" && (strings.HasPrefix(f.Mime, "image/") || strings.HasPrefix(f.Mime, "video/")) {
// Generate thumbnail if one doesn't exist yet
if f.ThumbnailMime == "" || f.ThumbnailSize == 0 {
if err := f.GenerateThumbnail(); err != nil {
return nil, nil, err
}
}
objName += "_thumbnail"
}
// Get object
obj, err := s3Clients[s3RegionOrder[0]].GetObject(
ctx,
f.Bucket,
objName,
minio.GetObjectOptions{},
)
if err != nil {
return nil, nil, err
}
// Get object info (used for size)
objInfo, err := s3Clients[s3RegionOrder[0]].StatObject(
ctx,
f.Bucket,
objName,
minio.StatObjectOptions{},
)
if err != nil {
return nil, nil, err
}
return obj, &objInfo, nil
}
func (f *File) Delete() error {
// Delete database row
if _, err := db.Collection("files").DeleteOne(
context.TODO(),
bson.M{"_id": f.Id},
); err != nil {
return err
}
// Clean-up objects if nothing else is referencing them
referenced, err := isFileReferenced(f.Bucket, f.Hash)
if err != nil {
return err
}
if !referenced {
for _, s3Client := range s3Clients {
go s3Client.RemoveObject(ctx, f.Bucket, f.Hash, minio.RemoveObjectOptions{})
go s3Client.RemoveObject(ctx, f.Bucket, f.Hash+"_thumbnail", minio.RemoveObjectOptions{})
}
}
return nil
}