-
Notifications
You must be signed in to change notification settings - Fork 72
/
movie.go
311 lines (271 loc) · 8.02 KB
/
movie.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
package model
import (
"database/sql/driver"
"fmt"
"strings"
"time"
"github.com/synctv-org/synctv/utils"
"gorm.io/gorm"
)
type Movie struct {
ID string `gorm:"primaryKey;type:char(32)" json:"id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Position uint `gorm:"not null" json:"-"`
RoomID string `gorm:"not null;index;type:char(32)" json:"-"`
CreatorID string `gorm:"index;type:char(32)" json:"creatorId"`
MovieBase `gorm:"embedded;embeddedPrefix:base_" json:"base"`
Children []*Movie `gorm:"foreignKey:ParentID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"-"`
}
func (m *Movie) Clone() *Movie {
return &Movie{
ID: m.ID,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
Position: m.Position,
RoomID: m.RoomID,
CreatorID: m.CreatorID,
MovieBase: *m.MovieBase.Clone(),
Children: m.Children,
}
}
func (m *Movie) BeforeCreate(tx *gorm.DB) error {
if m.ID == "" {
m.ID = utils.SortUUID()
}
return nil
}
func (m *Movie) BeforeSave(tx *gorm.DB) (err error) {
if m.ParentID != "" {
mv := &Movie{}
err = tx.Where("id = ?", m.ParentID).First(mv).Error
if err != nil {
return fmt.Errorf("load parent movie failed: %w", err)
}
if !mv.IsFolder {
return fmt.Errorf("parent is not a folder")
}
if mv.IsDynamicFolder() {
return fmt.Errorf("parent is a dynamic folder, cannot add child")
}
}
return
}
type MoreSource struct {
Name string `json:"name"`
Type string `json:"type"`
Url string `json:"url"`
}
type MovieBase struct {
Url string `gorm:"type:varchar(8192)" json:"url"`
MoreSources []*MoreSource `gorm:"serializer:fastjson;type:text" json:"moreSources,omitempty"`
Name string `gorm:"not null;type:varchar(256)" json:"name"`
Live bool `json:"live"`
Proxy bool `json:"proxy"`
RtmpSource bool `json:"rtmpSource"`
Type string `json:"type"`
Headers map[string]string `gorm:"serializer:fastjson;type:text" json:"headers,omitempty"`
Subtitles map[string]*Subtitle `gorm:"serializer:fastjson;type:text" json:"subtitles,omitempty"`
VendorInfo VendorInfo `gorm:"embedded;embeddedPrefix:vendor_info_" json:"vendorInfo,omitempty"`
IsFolder bool `json:"isFolder"`
ParentID EmptyNullString `gorm:"type:char(32)" json:"parentId"`
}
func (m *MovieBase) Clone() *MovieBase {
mss := make([]*MoreSource, len(m.MoreSources))
for i, ms := range m.MoreSources {
mss[i] = &MoreSource{
Name: ms.Name,
Type: ms.Type,
Url: ms.Url,
}
}
hds := make(map[string]string, len(m.Headers))
for k, v := range m.Headers {
hds[k] = v
}
sbs := make(map[string]*Subtitle, len(m.Subtitles))
for k, v := range m.Subtitles {
sbs[k] = &Subtitle{
URL: v.URL,
Type: v.Type,
}
}
return &MovieBase{
Url: m.Url,
MoreSources: mss,
Name: m.Name,
Live: m.Live,
Proxy: m.Proxy,
RtmpSource: m.RtmpSource,
Type: m.Type,
Headers: hds,
Subtitles: sbs,
VendorInfo: m.VendorInfo,
IsFolder: m.IsFolder,
ParentID: m.ParentID,
}
}
func (m *MovieBase) IsDynamicFolder() bool {
return m.IsFolder && m.VendorInfo.Vendor != ""
}
type EmptyNullString string
func (ns EmptyNullString) String() string {
return string(ns)
}
// Scan implements the [Scanner] interface.
func (ns *EmptyNullString) Scan(value any) error {
if value == nil {
*ns = ""
return nil
}
switch v := value.(type) {
case []byte:
*ns = EmptyNullString(v)
case string:
*ns = EmptyNullString(v)
default:
return fmt.Errorf("unsupported type: %T", v)
}
return nil
}
// Value implements the [driver.Valuer] interface.
func (ns EmptyNullString) Value() (driver.Value, error) {
if ns == "" {
return nil, nil
}
return string(ns), nil
}
type Subtitle struct {
URL string `json:"url"`
Type string `json:"type"`
}
type VendorName = string
const (
VendorBilibili VendorName = "bilibili"
VendorAlist VendorName = "alist"
VendorEmby VendorName = "emby"
)
type VendorInfo struct {
Vendor VendorName `gorm:"type:varchar(32)" json:"vendor"`
Backend string `gorm:"type:varchar(64)" json:"backend"`
Bilibili *BilibiliStreamingInfo `gorm:"embedded;embeddedPrefix:bilibili_" json:"bilibili,omitempty"`
Alist *AlistStreamingInfo `gorm:"embedded;embeddedPrefix:alist_" json:"alist,omitempty"`
Emby *EmbyStreamingInfo `gorm:"embedded;embeddedPrefix:emby_" json:"emby,omitempty"`
}
type BilibiliStreamingInfo struct {
Bvid string `json:"bvid,omitempty"`
Cid uint64 `json:"cid,omitempty"`
Epid uint64 `json:"epid,omitempty"`
Quality uint64 `json:"quality,omitempty"`
Shared bool `json:"shared,omitempty"`
}
func (b *BilibiliStreamingInfo) Validate() error {
switch {
// 先判断epid是否为0来确定是否是pgc
case b.Epid != 0:
if b.Bvid == "" || b.Cid == 0 {
return fmt.Errorf("bvid or cid is empty")
}
case b.Bvid != "":
if b.Cid == 0 {
return fmt.Errorf("cid is empty")
}
case b.Cid != 0: // live
return nil
default:
return fmt.Errorf("bvid or epid is empty")
}
return nil
}
type AlistStreamingInfo struct {
// {/}serverId/Path
Path string `gorm:"type:varchar(4096)" json:"path,omitempty"`
Password string `gorm:"type:varchar(256)" json:"password,omitempty"`
}
func GetAlistServerIdFromPath(path string) (serverID string, filePath string, err error) {
before, after, found := strings.Cut(strings.TrimLeft(path, "/"), "/")
if !found {
return "", path, fmt.Errorf("path is invalid")
}
return before, after, nil
}
func FormatAlistPath(serverID, filePath string) string {
return fmt.Sprintf("%s/%s", serverID, strings.Trim(filePath, "/"))
}
func (a *AlistStreamingInfo) SetServerIDAndFilePath(serverID, filePath string) {
a.Path = FormatAlistPath(serverID, filePath)
}
func (a *AlistStreamingInfo) ServerID() (string, error) {
serverID, _, err := GetAlistServerIdFromPath(a.Path)
return serverID, err
}
func (a *AlistStreamingInfo) FilePath() (string, error) {
_, filePath, err := GetAlistServerIdFromPath(a.Path)
return filePath, err
}
func (a *AlistStreamingInfo) ServerIDAndFilePath() (serverID, filePath string, err error) {
return GetAlistServerIdFromPath(a.Path)
}
func (a *AlistStreamingInfo) Validate() error {
if a.Path == "" {
return fmt.Errorf("path is empty")
}
return nil
}
func (a *AlistStreamingInfo) BeforeSave(tx *gorm.DB) error {
if a.Password != "" {
s, err := utils.CryptoToBase64([]byte(a.Password), utils.GenCryptoKey(a.Path))
if err != nil {
return err
}
a.Password = s
}
return nil
}
func (a *AlistStreamingInfo) AfterSave(tx *gorm.DB) error {
if a.Password != "" {
b, err := utils.DecryptoFromBase64(a.Password, utils.GenCryptoKey(a.Path))
if err != nil {
return err
}
a.Password = string(b)
}
return nil
}
func (a *AlistStreamingInfo) AfterFind(tx *gorm.DB) error {
return a.AfterSave(tx)
}
type EmbyStreamingInfo struct {
// {/}serverId/ItemId
Path string `gorm:"type:varchar(52)" json:"path,omitempty"`
Transcode bool `json:"transcode,omitempty"`
}
func GetEmbyServerIdFromPath(path string) (serverID string, filePath string, err error) {
if s := strings.Split(strings.TrimLeft(path, "/"), "/"); len(s) == 2 {
return s[0], s[1], nil
}
return "", path, fmt.Errorf("path is invalid")
}
func FormatEmbyPath(serverID, filePath string) string {
return fmt.Sprintf("%s/%s", serverID, filePath)
}
func (e *EmbyStreamingInfo) SetServerIDAndFilePath(serverID, filePath string) {
e.Path = FormatEmbyPath(serverID, filePath)
}
func (e *EmbyStreamingInfo) ServerID() (string, error) {
serverID, _, err := GetEmbyServerIdFromPath(e.Path)
return serverID, err
}
func (e *EmbyStreamingInfo) FilePath() (string, error) {
_, filePath, err := GetEmbyServerIdFromPath(e.Path)
return filePath, err
}
func (e *EmbyStreamingInfo) ServerIDAndFilePath() (serverID, filePath string, err error) {
return GetEmbyServerIdFromPath(e.Path)
}
func (e *EmbyStreamingInfo) Validate() error {
if e.Path == "" {
return fmt.Errorf("path is empty")
}
return nil
}