/
model_file.go
297 lines (240 loc) · 5.86 KB
/
model_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
package models
import (
"bytes"
"fmt"
"io"
"io/fs"
"math"
"net/http"
"strconv"
"time"
)
type HashAlgorithm string
const (
HashAlgorithmMd5 HashAlgorithm = "MD5"
// oshash
HashAlgorithmOshash HashAlgorithm = "OSHASH"
)
var AllHashAlgorithm = []HashAlgorithm{
HashAlgorithmMd5,
HashAlgorithmOshash,
}
func (e HashAlgorithm) IsValid() bool {
switch e {
case HashAlgorithmMd5, HashAlgorithmOshash:
return true
}
return false
}
func (e HashAlgorithm) String() string {
return string(e)
}
func (e *HashAlgorithm) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
*e = HashAlgorithm(str)
if !e.IsValid() {
return fmt.Errorf("%s is not a valid HashAlgorithm", str)
}
return nil
}
func (e HashAlgorithm) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(e.String()))
}
// ID represents an ID of a file.
type FileID int32
func (i FileID) String() string {
return strconv.Itoa(int(i))
}
func (i *FileID) UnmarshalGQL(v interface{}) (err error) {
switch v := v.(type) {
case string:
var id int
id, err = strconv.Atoi(v)
*i = FileID(id)
return err
case int:
*i = FileID(v)
return nil
default:
return fmt.Errorf("%T is not an int", v)
}
}
func (i FileID) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(i.String()))
}
// DirEntry represents a file or directory in the file system.
type DirEntry struct {
ZipFileID *FileID `json:"zip_file_id"`
// transient - not persisted
// only guaranteed to have id, path and basename set
ZipFile File
ModTime time.Time `json:"mod_time"`
}
func (e *DirEntry) info(fs FS, path string) (fs.FileInfo, error) {
if e.ZipFile != nil {
zipPath := e.ZipFile.Base().Path
zfs, err := fs.OpenZip(zipPath)
if err != nil {
return nil, err
}
defer zfs.Close()
fs = zfs
}
// else assume os file
ret, err := fs.Lstat(path)
return ret, err
}
// File represents a file in the file system.
type File interface {
Base() *BaseFile
SetFingerprints(fp Fingerprints)
Open(fs FS) (io.ReadCloser, error)
}
// BaseFile represents a file in the file system.
type BaseFile struct {
ID FileID `json:"id"`
DirEntry
// resolved from parent folder and basename only - not stored in DB
Path string `json:"path"`
Basename string `json:"basename"`
ParentFolderID FolderID `json:"parent_folder_id"`
Fingerprints Fingerprints `json:"fingerprints"`
Size int64 `json:"size"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SetFingerprints sets the fingerprints of the file.
// If a fingerprint of the same type already exists, it is overwritten.
func (f *BaseFile) SetFingerprints(fp Fingerprints) {
for _, v := range fp {
f.SetFingerprint(v)
}
}
// SetFingerprint sets the fingerprint of the file.
// If a fingerprint of the same type already exists, it is overwritten.
func (f *BaseFile) SetFingerprint(fp Fingerprint) {
for i, existing := range f.Fingerprints {
if existing.Type == fp.Type {
f.Fingerprints[i] = fp
return
}
}
f.Fingerprints = append(f.Fingerprints, fp)
}
// Base is used to fulfil the File interface.
func (f *BaseFile) Base() *BaseFile {
return f
}
func (f *BaseFile) Open(fs FS) (io.ReadCloser, error) {
if f.ZipFile != nil {
zipPath := f.ZipFile.Base().Path
zfs, err := fs.OpenZip(zipPath)
if err != nil {
return nil, err
}
return zfs.OpenOnly(f.Path)
}
return fs.Open(f.Path)
}
func (f *BaseFile) Info(fs FS) (fs.FileInfo, error) {
return f.info(fs, f.Path)
}
func (f *BaseFile) Serve(fs FS, w http.ResponseWriter, r *http.Request) error {
reader, err := f.Open(fs)
if err != nil {
return err
}
defer reader.Close()
content, ok := reader.(io.ReadSeeker)
if !ok {
data, err := io.ReadAll(reader)
if err != nil {
return err
}
content = bytes.NewReader(data)
}
if r.URL.Query().Has("t") {
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
} else {
w.Header().Set("Cache-Control", "no-cache")
}
// Set filename if not previously set
if w.Header().Get("Content-Disposition") == "" {
w.Header().Set("Content-Disposition", fmt.Sprintf(`filename="%s"`, f.Basename))
}
http.ServeContent(w, r, f.Basename, f.ModTime, content)
return nil
}
// VisualFile is an interface for files that have a width and height.
type VisualFile interface {
File
GetWidth() int
GetHeight() int
GetFormat() string
}
func GetMinResolution(f VisualFile) int {
w := f.GetWidth()
h := f.GetHeight()
if w < h {
return w
}
return h
}
// ImageFile is an extension of BaseFile to represent image files.
type ImageFile struct {
*BaseFile
Format string `json:"format"`
Width int `json:"width"`
Height int `json:"height"`
}
func (f ImageFile) GetWidth() int {
return f.Width
}
func (f ImageFile) GetHeight() int {
return f.Height
}
func (f ImageFile) GetFormat() string {
return f.Format
}
// VideoFile is an extension of BaseFile to represent video files.
type VideoFile struct {
*BaseFile
Format string `json:"format"`
Width int `json:"width"`
Height int `json:"height"`
Duration float64 `json:"duration"`
VideoCodec string `json:"video_codec"`
AudioCodec string `json:"audio_codec"`
FrameRate float64 `json:"frame_rate"`
BitRate int64 `json:"bitrate"`
Interactive bool `json:"interactive"`
InteractiveSpeed *int `json:"interactive_speed"`
}
func (f VideoFile) GetWidth() int {
return f.Width
}
func (f VideoFile) GetHeight() int {
return f.Height
}
func (f VideoFile) GetFormat() string {
return f.Format
}
// #1572 - Inf and NaN values cause the JSON marshaller to fail
// Replace these values with 0 rather than erroring
func (f VideoFile) DurationFinite() float64 {
ret := f.Duration
if math.IsInf(ret, 0) || math.IsNaN(ret) {
return 0
}
return ret
}
func (f VideoFile) FrameRateFinite() float64 {
ret := f.FrameRate
if math.IsInf(ret, 0) || math.IsNaN(ret) {
return 0
}
return ret
}