/
attachments.go
117 lines (109 loc) · 3.32 KB
/
attachments.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
package models
import (
"time"
"github.com/davecheney/pub/internal/snowflake"
)
type Attachment struct {
// snowflake.ID `gorm:"primarykey;autoIncrement:false"`
MediaType string `gorm:"size:64;not null"`
URL string `gorm:"size:255;not null"`
Name string `gorm:"not null"`
Blurhash string `gorm:"size:36;not null"`
Width int `gorm:"not null"`
Height int `gorm:"not null"`
FocalPoint FocalPoint `gorm:"embedded;embeddedPrefix:focal_point_"`
}
type FocalPoint struct {
X float64 `gorm:"not null;default:0"`
Y float64 `gorm:"not null;default:0"`
}
// Extension returns the file extension of the attachment.
// This is used to generate the filename of the attachment which most IOS clients expect.
func (att *Attachment) Extension() string {
switch att.MediaType {
case "image/jpeg":
return "jpg"
case "image/png":
return "png"
case "image/gif":
return "gif"
case "image/webp":
return "webp"
case "video/mp4":
return "mp4"
case "video/webm":
return "webm"
case "audio/mpeg":
return "mp3"
case "audio/ogg":
return "ogg"
default:
return "jpg" // todo YOLO
}
}
// ToType returns the Mastodon type of the attachment, image, video, audio or unknown.
func (att *Attachment) ToType() string {
switch att.MediaType {
case "image/jpeg":
return "image"
case "image/png":
return "image"
case "image/gif":
return "image"
case "image/webp":
return "image"
case "video/mp4":
return "video"
case "video/webm":
return "video"
case "audio/mpeg":
return "audio"
case "audio/ogg":
return "audio"
default:
return "unknown"
}
}
// func (s *StatusAttachment) AfterSave(tx *gorm.DB) error {
// tx = tx.Clauses(clause.OnConflict{
// UpdateAll: true,
// })
// if s.URL == "" {
// // no URL, so no need to fetch the attachment
// return nil
// }
// if s.Height > 0 && s.Width > 0 {
// // already have the dimensions, so no need to fetch the attachment
// return nil
// }
// switch s.MediaType {
// case "image/jpeg", "image/png", "image/gif", "image/webp":
// // supported media type, so fetch the attachment
// return tx.Create(&StatusAttachmentRequest{
// StatusAttachmentID: s.ID,
// }).Error
// default:
// // unsupported media type, so no need to fetch the attachment
// return nil
// }
// }
// A StatusAttachmentRequest records a request fetch a remote attachment.
// StatusAttachmentRequest are created by hooks on the StatusAttachment model, and are
// processed by the StatusAttachmentRequestProcessor in the background.
type StatusAttachmentRequest struct {
ID uint32 `gorm:"primarykey;"`
// CreatedAt is the time the request was created.
CreatedAt time.Time
// UpdatedAt is the time the request was last updated.
UpdatedAt time.Time
// StatusAttachmentID is the ID of the StatusAttachment that the request is for.
StatusAttachmentID snowflake.ID `gorm:"uniqueIndex;not null;"`
// StatusAttachment is the StatusAttachment that the request is for.
StatusAttachment *StatusAttachment `gorm:"constraint:OnDelete:CASCADE;<-:false;"`
// Attempts is the number of times the request has been attempted.
Attempts uint32 `gorm:"not null;default:0"`
// LastAttempt is the time the request was last attempted.
LastAttempt time.Time
// LastResult is the result of the last attempt if it failed.
LastResult string `gorm:"size:255;not null;default:''"`
}