forked from vavallee/bindery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
353 lines (327 loc) · 11.7 KB
/
Copy pathtypes.go
File metadata and controls
353 lines (327 loc) · 11.7 KB
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
package abs
import (
"strings"
"github.com/vavallee/bindery/internal/textutil"
)
type UserPermissions struct {
AccessAllLibraries bool `json:"accessAllLibraries"`
}
type User struct {
ID string `json:"id"`
Username string `json:"username"`
Type string `json:"type"`
LibrariesAccessible []string `json:"librariesAccessible"`
Permissions UserPermissions `json:"permissions"`
}
type ServerSettings struct {
Version string `json:"version"`
}
type AuthorizeResponse struct {
User User `json:"user"`
UserDefaultLibraryID string `json:"userDefaultLibraryId"`
ServerSettings ServerSettings `json:"serverSettings"`
Source string `json:"Source"`
}
type LibraryFolder struct {
ID string `json:"id"`
FullPath string `json:"fullPath"`
}
type Library struct {
ID string `json:"id"`
Name string `json:"name"`
MediaType string `json:"mediaType"`
Icon string `json:"icon"`
Provider string `json:"provider"`
Folders []LibraryFolder `json:"folders"`
}
type librariesResponse struct {
Libraries []Library `json:"libraries"`
}
type Author struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Series struct {
ID string `json:"id"`
Name string `json:"name"`
}
type SeriesSequence struct {
ID string `json:"id"`
Name string `json:"name"`
Sequence string `json:"sequence"`
}
type AudioFileMetadata struct {
Filename string `json:"filename"`
Ext string `json:"ext"`
}
type AudioFile struct {
INO string `json:"ino"`
Index int `json:"index"`
Path string `json:"path"`
Duration *float64 `json:"duration,omitempty"`
Size *int64 `json:"size,omitempty"`
Metadata *AudioFileMetadata `json:"metadata,omitempty"`
}
type EbookFile struct {
Path string `json:"path"`
INO string `json:"ino"`
}
type BookMetadata struct {
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Authors []Author `json:"authors"`
Narrators []string `json:"narrators"`
Series []SeriesSequence `json:"series"`
Genres []string `json:"genres"`
PublishedYear string `json:"publishedYear"`
PublishedDate string `json:"publishedDate"`
Publisher string `json:"publisher"`
Description string `json:"description"`
Language string `json:"language"`
ISBN string `json:"isbn"`
ASIN string `json:"asin"`
Explicit bool `json:"explicit"`
}
type BookMedia struct {
LibraryItemID string `json:"libraryItemId"`
Metadata BookMetadata `json:"metadata"`
Duration *float64 `json:"duration,omitempty"`
Size *int64 `json:"size,omitempty"`
AudioFiles []AudioFile `json:"audioFiles"`
EbookFile *EbookFile `json:"ebookFile,omitempty"`
}
type LibraryItem struct {
ID string `json:"id"`
LibraryID string `json:"libraryId"`
FolderID string `json:"folderId"`
Path string `json:"path"`
RelPath string `json:"relPath"`
IsFile bool `json:"isFile"`
MediaType string `json:"mediaType"`
Media BookMedia `json:"media"`
}
type LibraryItemsPage struct {
Results []LibraryItem `json:"results"`
Total int `json:"total"`
Limit int `json:"limit"`
Page int `json:"page"`
MediaType string `json:"mediaType"`
Minified bool `json:"minified"`
CollapseSeries bool `json:"collapseseries"`
}
type NormalizedAuthor struct {
ID string `json:"id"`
Name string `json:"name"`
}
type NormalizedSeries struct {
ID string `json:"id"`
Name string `json:"name"`
Sequence string `json:"sequence"`
}
type NormalizedAudioFile struct {
INO string `json:"ino"`
Index int `json:"index"`
Path string `json:"path"`
Filename string `json:"filename"`
Extension string `json:"extension"`
DurationSeconds float64 `json:"durationSeconds"`
SizeBytes int64 `json:"sizeBytes"`
}
type NormalizedLibraryItem struct {
ItemID string `json:"itemId"`
LibraryID string `json:"libraryId"`
MediaType string `json:"mediaType"`
Path string `json:"path"`
RelPath string `json:"relPath"`
IsFile bool `json:"isFile"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Description string `json:"description"`
Publisher string `json:"publisher"`
Language string `json:"language"`
ISBN string `json:"isbn"`
ASIN string `json:"asin"`
PublishedYear string `json:"publishedYear"`
PublishedDate string `json:"publishedDate"`
Explicit bool `json:"explicit"`
DurationSeconds float64 `json:"durationSeconds"`
SizeBytes int64 `json:"sizeBytes"`
Authors []NormalizedAuthor `json:"authors"`
Narrators []string `json:"narrators"`
Series []NormalizedSeries `json:"series"`
Genres []string `json:"genres"`
AudioFiles []NormalizedAudioFile `json:"audioFiles"`
EbookPath string `json:"ebookPath,omitempty"`
EbookINO string `json:"ebookIno,omitempty"`
DetailFetched bool `json:"detailFetched"`
ResolvedAuthorForeignID string `json:"resolvedAuthorForeignId,omitempty"`
ResolvedAuthorName string `json:"resolvedAuthorName,omitempty"`
ResolvedBookForeignID string `json:"resolvedBookForeignId,omitempty"`
ResolvedBookTitle string `json:"resolvedBookTitle,omitempty"`
EditedTitle string `json:"editedTitle,omitempty"`
}
func (i LibraryItem) DetailFetchReasons() []string {
reasons := make([]string, 0, 5)
if !i.IsFile {
reasons = append(reasons, "folder-backed item")
}
if len(i.Media.AudioFiles) == 0 && i.Media.EbookFile == nil {
reasons = append(reasons, "missing audio files")
}
if i.Media.Duration == nil || *i.Media.Duration <= 0 {
reasons = append(reasons, "missing duration")
}
if i.Media.Size == nil || *i.Media.Size <= 0 {
reasons = append(reasons, "missing size")
}
if len(i.Media.Metadata.Authors) == 0 {
reasons = append(reasons, "missing authors")
}
for _, series := range i.Media.Metadata.Series {
if strings.TrimSpace(series.ID) == "" || strings.TrimSpace(series.Name) == "" {
reasons = append(reasons, "incomplete series metadata")
break
}
}
return reasons
}
func MergeLibraryItem(listItem, detailItem LibraryItem) LibraryItem {
merged := detailItem
if merged.ID == "" {
merged.ID = listItem.ID
}
if merged.LibraryID == "" {
merged.LibraryID = listItem.LibraryID
}
if merged.FolderID == "" {
merged.FolderID = listItem.FolderID
}
if merged.Path == "" {
merged.Path = listItem.Path
}
if merged.RelPath == "" {
merged.RelPath = listItem.RelPath
}
if merged.MediaType == "" {
merged.MediaType = listItem.MediaType
}
if merged.Media.LibraryItemID == "" {
merged.Media.LibraryItemID = listItem.Media.LibraryItemID
}
if merged.Media.Metadata.Title == "" {
merged.Media.Metadata.Title = listItem.Media.Metadata.Title
}
if merged.Media.Metadata.Subtitle == "" {
merged.Media.Metadata.Subtitle = listItem.Media.Metadata.Subtitle
}
if len(merged.Media.Metadata.Authors) == 0 {
merged.Media.Metadata.Authors = listItem.Media.Metadata.Authors
}
if len(merged.Media.Metadata.Narrators) == 0 {
merged.Media.Metadata.Narrators = listItem.Media.Metadata.Narrators
}
if len(merged.Media.Metadata.Series) == 0 {
merged.Media.Metadata.Series = listItem.Media.Metadata.Series
}
if len(merged.Media.Metadata.Genres) == 0 {
merged.Media.Metadata.Genres = listItem.Media.Metadata.Genres
}
if merged.Media.Metadata.PublishedYear == "" {
merged.Media.Metadata.PublishedYear = listItem.Media.Metadata.PublishedYear
}
if merged.Media.Metadata.PublishedDate == "" {
merged.Media.Metadata.PublishedDate = listItem.Media.Metadata.PublishedDate
}
if merged.Media.Metadata.Publisher == "" {
merged.Media.Metadata.Publisher = listItem.Media.Metadata.Publisher
}
if merged.Media.Metadata.Description == "" {
merged.Media.Metadata.Description = listItem.Media.Metadata.Description
}
if merged.Media.Metadata.Language == "" {
merged.Media.Metadata.Language = listItem.Media.Metadata.Language
}
if merged.Media.Metadata.ISBN == "" {
merged.Media.Metadata.ISBN = listItem.Media.Metadata.ISBN
}
if merged.Media.Metadata.ASIN == "" {
merged.Media.Metadata.ASIN = listItem.Media.Metadata.ASIN
}
if merged.Media.Duration == nil {
merged.Media.Duration = listItem.Media.Duration
}
if merged.Media.Size == nil {
merged.Media.Size = listItem.Media.Size
}
if len(merged.Media.AudioFiles) == 0 {
merged.Media.AudioFiles = listItem.Media.AudioFiles
}
if merged.Media.EbookFile == nil {
merged.Media.EbookFile = listItem.Media.EbookFile
}
if !merged.IsFile && listItem.IsFile {
merged.IsFile = true
}
return merged
}
func NormalizeLibraryItem(item LibraryItem, detailFetched bool) NormalizedLibraryItem {
out := NormalizedLibraryItem{
ItemID: item.ID,
LibraryID: item.LibraryID,
MediaType: item.MediaType,
Path: item.Path,
RelPath: item.RelPath,
IsFile: item.IsFile,
Title: item.Media.Metadata.Title,
Subtitle: item.Media.Metadata.Subtitle,
Description: textutil.CleanDescription(item.Media.Metadata.Description),
Publisher: item.Media.Metadata.Publisher,
Language: item.Media.Metadata.Language,
ISBN: item.Media.Metadata.ISBN,
ASIN: item.Media.Metadata.ASIN,
PublishedYear: item.Media.Metadata.PublishedYear,
PublishedDate: item.Media.Metadata.PublishedDate,
Explicit: item.Media.Metadata.Explicit,
Genres: append([]string(nil), item.Media.Metadata.Genres...),
Narrators: append([]string(nil), item.Media.Metadata.Narrators...),
DetailFetched: detailFetched,
}
if item.Media.Duration != nil {
out.DurationSeconds = *item.Media.Duration
}
if item.Media.Size != nil {
out.SizeBytes = *item.Media.Size
}
if item.Media.EbookFile != nil {
out.EbookPath = item.Media.EbookFile.Path
out.EbookINO = item.Media.EbookFile.INO
}
out.Authors = make([]NormalizedAuthor, 0, len(item.Media.Metadata.Authors))
for _, author := range item.Media.Metadata.Authors {
out.Authors = append(out.Authors, NormalizedAuthor(author))
}
out.Series = make([]NormalizedSeries, 0, len(item.Media.Metadata.Series))
for _, series := range item.Media.Metadata.Series {
out.Series = append(out.Series, NormalizedSeries(series))
}
out.AudioFiles = make([]NormalizedAudioFile, 0, len(item.Media.AudioFiles))
for _, file := range item.Media.AudioFiles {
audio := NormalizedAudioFile{
INO: file.INO,
Index: file.Index,
Path: file.Path,
}
if file.Metadata != nil {
audio.Filename = file.Metadata.Filename
audio.Extension = file.Metadata.Ext
}
if file.Duration != nil {
audio.DurationSeconds = *file.Duration
}
if file.Size != nil {
audio.SizeBytes = *file.Size
}
out.AudioFiles = append(out.AudioFiles, audio)
}
return out
}