-
Notifications
You must be signed in to change notification settings - Fork 7
/
sqlRegistry.js
1349 lines (1158 loc) · 46.6 KB
/
sqlRegistry.js
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*jslint node: true, nomen: true, esversion: 6 */
'use strict';
const MemoryRegistry = require('./memoryRegistry');
const sqlLayer = require('./sqlLayer');
const logger = require('../logger');
const debug = require('debug')('upnpserver:sqlRegistry');
const Path = require('path');
const PathNormalizer = require('../util/pathNormalizer');
const assert = require('assert');
const Async = require('async');
const vsprintf = require('sprintf-js').vsprintf;
const Semaphore = require('../util/semaphore');
const Uuid = require('uuid/v4');
const Configuration = require('../configuration');
const crypto = require('crypto');
class SQLRegistry extends MemoryRegistry {
initialize(callback) {
debug('initialize: ENTER');
this._changedItems = {};
const startTime = Date.now();
Configuration.getDataDir((err, dir) => {
if (err) return callback(err);
const path = Path.normalize(Path.join(dir, 'mms.db'));
this.sql = new sqlLayer(path);
this.createDBStructure(err => {
if (err) return callback(err);
var dt = Date.now() - startTime;
logger.info(`Database loaded in ${dt>3000?Math.round(dt/1000):(dt/1000).toFixed(2)} seconds`);
this.prepareStatements(err => {
if (err) return callback(err);
var d2t = Date.now() - startTime - dt;
logger.info(`Prepared statements in ${d2t>3000?Math.round(d2t/1000):(d2t/1000).toFixed(2)} seconds`);
super.initialize(callback);
});
});
});
}
prepareStatements(callback) {
debug('prepareStatements: ENTER');
const sqlStatements = {
insertUser: 'INSERT INTO users (_id, name, display_name, role_key, password) VALUES (?,?,?,?,?)',
getUserByName: 'SELECT * FROM users WHERE name = ?',
getUserById: 'SELECT * FROM users WHERE _id = ?',
updateUser: 'UPDATE users SET name = ?, display_name = ?, role_key = ?, password = ? WHERE _id = ?',
getUserFromSessionToken: 'SELECT * FROM users WHERE _id IN ( SELECT user_id FROM user_sessions WHERE token = ? AND token_expire > ? )',
insertSession: 'INSERT INTO user_sessions (token, token_expire, token_created, user_id, useragent, ip) VALUES (?,?,?,?,?,?)',
pruneOldSessions: 'DELETE FROM user_sessions WHERE token_expire < ?',
getRole: 'SELECT * FROM user_roles WHERE key=?',
getRoles: 'SELECT * FROM user_roles ORDER BY access_level',
getMedia: 'SELECT * from media',
getMediaByPath: 'SELECT * FROM media WHERE path = ?',
getMediaById: 'SELECT * FROM media WHERE _id = ?',
getMediaBySyncId: 'SELECT * FROM media WHERE sync_id = ?',
getMediaByTextSearch: 'SELECT * FROM media WHERE _id IN (SELECT rowid FROM MediaText WHERE MediaText MATCH ?)',
getIdAndSyncIdByPath: 'SELECT _id, sync_id FROM media WHERE path = ?',
getIdBySyncId: 'SELECT _id, sync_id FROM media WHERE sync_id = ?',
updateMediaBySyncId: 'UPDATE media SET path = ?, title = ?, album = ?, artist = ?, album_artist = ?, actor = ?, composer = ?, producer = ?, director = ?, publisher = ?, contributor= ?, lyricist = ?, conductor = ?, bpm = ?, genre = ?, rating = ?, playcount = ?, skipcount = ?, bookmark = ?, comment = ?, duration = ?, size = ?, mime_type = ?, year = ?, trackNumber = ?, discNumber = ?, lyrics = ?, last_time_played = ?, volumeLeveling = ?, normalizeTrack = ?, normalizeAlbum = ?, parental_rating = ?, grouping = ?, tempo = ?, mood = ?, occasion = ?, quality = ?, isrc = ?, initialKey = ?, originalTitle = ?, originalArtist = ?, originalLyricist = ?, originalDate = ?, copyright = ?, encoder = ?, subtitle = ?, custom1 = ?, custom2 = ?, custom3 = ?, custom4 = ?, custom5 = ?, custom6 = ?, custom7 = ?, custom8 = ?, custom9 = ?, custom10 = ?, album_art = ? WHERE sync_id = ?',
insertMedia: 'INSERT INTO media (path, title, album, artist, album_artist, actor, composer, producer, director, publisher, contributor, lyricist, conductor, bpm, genre, rating, playcount, skipcount, bookmark, comment, duration, size, mime_type, year, trackNumber, discNumber, lyrics, last_time_played, volumeLeveling, normalizeTrack, normalizeAlbum, parental_rating, grouping, tempo, mood, occasion, quality, isrc, initialKey, originalTitle, originalArtist, originalLyricist, originalDate, copyright, encoder, subtitle, custom1, custom2, custom3, custom4, custom5, custom6, custom7, custom8, custom9, custom10, album_art, sync_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
deleteMediaByPath: 'DELETE FROM media WHERE path = ?',
deleteMediaByIdList: 'DELETE FROM media WHERE _id IN (?)',
getCollections: 'SELECT * FROM collections',
getCollectionById: 'SELECT * FROM collections WHERE _id = ?',
updateCollectionById: 'UPDATE collections SET title = ?, config = ? WHERE _id = ?',
insertCollection: 'INSERT INTO collections (title, config) VALUES (?,?)',
getPlaylistsByParentGuid: 'SELECT * FROM playlists WHERE parent_guid = ?',
getPlaylistContent: 'SELECT media.* FROM media, playlist_items_map WHERE media._id = playlist_items_map.media_id AND playlist_items_map.playlist_guid = ? ORDER BY playlist_items_map.play_order',
getPlaylistLastModifiedByGuid: 'SELECT last_modified FROM playlists WHERE guid = ?',
updatePlaylistByGuid: 'UPDATE playlists SET title = ?, parent_guid = ?, last_modified = ?, criteria = ? WHERE guid = ?',
insertPlaylist: 'INSERT INTO playlists (title, parent_guid, last_modified, criteria, guid) VALUES (?,?,?,?,?)',
deletePlaylistByGuid: 'DELETE FROM playlists WHERE guid = ?',
clearUnusedPlaylists: 'DELETE FROM playlists WHERE parent_guid IS NOT NULL AND parent_guid NOT IN (SELECT guid FROM playlists)',
deletePlaylistItems: 'DELETE FROM playlist_items_map WHERE playlist_guid = ?',
insertPlaylistItem: 'INSERT INTO playlist_items_map (playlist_guid, media_id, play_order) VALUES (?,?,?)',
checkContentPointsByToken: 'SELECT null FROM content_points WHERE token = ?',
insertContentToken: 'INSERT INTO content_points (token) VALUES (?)',
getLastContentToken: 'SELECT token FROM content_points ORDER BY _id DESC LIMIT 1',
getContentChangesByToken: 'SELECT * FROM content_changes WHERE token > ?',
insertContentChange: 'INSERT INTO content_changes (token, timestamp, item_type, item_oper, item_sync_id) VALUES (?,?,?,?,?)',
deleteContentChange: 'DELETE FROM content_changes WHERE item_type = ? AND item_oper IN (?,?) AND item_sync_id = ?',
};
this.statements = {};
try {
for (var stmtId in sqlStatements) {
this.sql.addStatement(stmtId, sqlStatements[stmtId]);
}
callback(null);
}
catch (err) {
console.error(err);
console.log(`Possible problematic statement: ${stmtId}`);
callback(err);
}
}
createDBStructure(callback) {
debug('createDbStructure: ENTER');
try {
this.sql.prepare('CREATE TABLE IF NOT EXISTS db_info (version INTEGER)').run();
var version = 0;
var row = this.sql.prepare('SELECT * FROM db_info').get();
if (row) version = row.version;
var text_unicode = 'TEXT'; // 'TEXT COLLATE UNICODE'; // TODO: UNICODE collation
var queries = [];
if (version < 1) {
queries.push('PRAGMA foreign_keys = ON'); // needs to be enabled to FOREIGN KEY (and ON DELETE CASCADE) to work
queries.push('CREATE TABLE IF NOT EXISTS media (' +
'_id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'sync_id TEXT UNIQUE NOT NULL,' +
'title ' + text_unicode + ',' +
'album ' + text_unicode + ',' +
'album_art ' + text_unicode + ',' +
'path TEXT,' +
'mime_type ' + text_unicode + ',' +
'date_added TEXT,' +
'date_sync TEXT,' +
'duration INTEGER,' +
'bpm INTEGER,' +
'trackNumber ' + text_unicode + ',' +
'discNumber ' + text_unicode + ',' +
'year INTEGER,' +
'type INTEGER,' +
'rating INTEGER DEFAULT -1 NOT NULL,' +
'last_time_played TEXT,' +
'bookmark INTEGER DEFAULT 0 NOT NULL,' +
'playcount INTEGER DEFAULT 0 NOT NULL,' +
'skipcount INTEGER DEFAULT 0 NOT NULL,' +
'volumeLeveling DOUBLE DEFAULT 0.0 NOT NULL,' +
'normalizeTrack DOUBLE DEFAULT 0.0 NOT NULL,' +
'normalizeAlbum DOUBLE DEFAULT 0.0 NOT NULL,' +
'lyrics ' + text_unicode + ',' +
'size INTEGER,' +
'bitrate INTEGER,' +
'resolution INTEGER,' +
'framerate INTEGER,' +
'channels INTEGER,' +
'samplerate INTEGER,' +
'bitsPerSample INTEGER,' +
'date_release INTEGER,' +
'language ' + text_unicode + ',' +
'artist ' + text_unicode + ',' +
'album_artist ' + text_unicode + ',' +
'genre ' + text_unicode + ',' +
'composer ' + text_unicode + ',' +
'conductor ' + text_unicode + ',' +
'lyricist ' + text_unicode + ',' +
'producer ' + text_unicode + ',' +
'publisher ' + text_unicode + ',' +
'actor ' + text_unicode + ',' +
'director ' + text_unicode + ',' +
'contributor ' + text_unicode + ',' +
'originalTitle ' + text_unicode + ',' +
'originalArtist ' + text_unicode + ',' +
'originalLyricist ' + text_unicode + ',' +
'originalDate INTEGER,' +
'parental_rating ' + text_unicode + ',' +
'grouping ' + text_unicode + ',' +
'tempo ' + text_unicode + ',' +
'mood ' + text_unicode + ',' +
'occasion ' + text_unicode + ',' +
'quality ' + text_unicode + ',' +
'comment ' + text_unicode + ',' +
'isrc ' + text_unicode + ',' +
'initialKey ' + text_unicode + ',' +
'custom1 ' + text_unicode + ',' +
'custom2 ' + text_unicode + ',' +
'custom3 ' + text_unicode + ',' +
'custom4 ' + text_unicode + ',' +
'custom5 ' + text_unicode +
')');
queries.push('CREATE INDEX IF NOT EXISTS idx_media_path ON media (path)');
queries.push('CREATE INDEX IF NOT EXISTS idx_media_sync_id ON media (sync_id)');
queries.push(
'CREATE TABLE IF NOT EXISTS playlists (' +
'title TEXT,' +
'criteria TEXT,' + // for the future auto-playlist features
'guid TEXT PRIMARY KEY,' +
'last_modified TEXT,' +
'parent_guid TEXT' +
')');
queries.push(
'CREATE TABLE IF NOT EXISTS playlist_items_map (' +
'_id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'media_id TEXT NOT NULL,' +
'playlist_guid TEXT NOT NULL,' +
'play_order INTEGER,' +
'FOREIGN KEY ( media_id ) REFERENCES media ( _id ) ON DELETE CASCADE,' +
'FOREIGN KEY ( playlist_guid ) REFERENCES playlists ( guid ) ON DELETE CASCADE' +
')');
queries.push('CREATE INDEX IF NOT EXISTS idx_playlist_guid ON playlists (guid)');
queries.push('CREATE INDEX IF NOT EXISTS idx_playlist_parent_guid ON playlists (parent_guid)');
queries.push('CREATE INDEX IF NOT EXISTS idx_playlist_map_media_id ON playlist_items_map (media_id)');
queries.push('CREATE INDEX IF NOT EXISTS idx_playlist_map_playlist_guid ON playlist_items_map (playlist_guid)');
queries.push(
'CREATE TABLE IF NOT EXISTS collections (' +
'_id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'title TEXT,' +
'config TEXT' +
')');
queries.push('INSERT INTO db_info (version) VALUES (1)');
}
if (version < 2) {
queries.push('ALTER TABLE media ADD COLUMN copyright ' + text_unicode);
queries.push('ALTER TABLE media ADD COLUMN encoder ' + text_unicode);
queries.push('ALTER TABLE media ADD COLUMN subtitle ' + text_unicode);
queries.push('UPDATE db_info SET version = 2');
}
if (version < 3) {
// FTS creation moved to the next update (because further custom fields were added)
queries.push('UPDATE db_info SET version = 3');
}
if (version < 4) {
queries.push('ALTER TABLE media ADD COLUMN custom6 ' + text_unicode);
queries.push('ALTER TABLE media ADD COLUMN custom7 ' + text_unicode);
queries.push('ALTER TABLE media ADD COLUMN custom8 ' + text_unicode);
queries.push('ALTER TABLE media ADD COLUMN custom9 ' + text_unicode);
queries.push('ALTER TABLE media ADD COLUMN custom10 ' + text_unicode);
//queries.push('PRAGMA compile_options; ENABLE_FTS5');
queries.push('DROP table IF EXISTS MediaText');
var comma_fields = 'path, title, album, artist, album_artist, actor, composer, producer, director, publisher, contributor, lyricist, conductor, genre, lyrics, parental_rating, grouping, tempo, mood, occasion, quality, comment, isrc, initialKey, originalTitle, originalArtist, originalLyricist, copyright, encoder, subtitle, custom1, custom2, custom3, custom4, custom5, custom6, custom7, custom8, custom9, custom10';
queries.push('CREATE VIRTUAL TABLE MediaText USING FTS5 ( tokenize = \'unicode61\', content=\'media\', content_rowid=\'_id\',' + comma_fields + ')');
queries.push('DROP TRIGGER IF EXISTS insert_media');
queries.push('CREATE TRIGGER insert_media AFTER INSERT ON media ' +
'BEGIN' +
' INSERT INTO MediaText (rowid, path, title, album, artist, album_artist, actor, composer, producer, director, publisher, contributor, lyricist, conductor, genre, comment, lyrics, parental_rating, grouping, tempo, mood, occasion, quality, isrc, initialKey, originalTitle, originalArtist, originalLyricist, copyright, encoder, subtitle, custom1, custom2, custom3, custom4, custom5, custom6, custom7, custom8, custom9, custom10)' +
' VALUES (new._id, new.path, new.title, new.album, new.artist, new.album_artist, new.actor, new.composer, new.producer, new.director, new.publisher, new.contributor, new.lyricist, new.conductor, new.genre, new.comment, new.lyrics, new.parental_rating, new.grouping, new.tempo, new.mood, new.occasion, new.quality, new.isrc, new.initialKey, new.originalTitle, new.originalArtist, new.originalLyricist, new.copyright, new.encoder, new.subtitle, new.custom1, new.custom2, new.custom3, new.custom4, new.custom5, new.custom6, new.custom7, new.custom8, new.custom9, new.custom10);' +
'END;');
queries.push('DROP TRIGGER IF EXISTS delete_media');
queries.push('CREATE TRIGGER delete_media DELETE ON media ' +
'BEGIN' +
' DELETE FROM MediaText WHERE rowid=old._id;' +
'END;');
var createFullTextTrigger = (field) => {
queries.push(vsprintf('DROP TRIGGER IF EXISTS update_media_%s', [field]));
queries.push(vsprintf('CREATE TRIGGER update_media_%s UPDATE OF %s ON media WHEN new.%s<>old.%s ' +
'BEGIN' +
' UPDATE MediaText SET %s=new.%s WHERE rowid=new._id;' +
'END;', [field, field, field, field, field, field]));
};
var c = createFullTextTrigger;
c('path');
c('title');
c('album');
c('artist');
c('album_artist');
c('actor');
c('composer');
c('producer');
c('director');
c('publisher');
c('contributor');
c('lyricist');
c('conductor');
c('genre');
c('comment');
c('lyrics');
c('parental_rating');
c('grouping');
c('tempo');
c('mood');
c('occasion');
c('quality');
c('isrc');
c('initialKey');
c('originalTitle');
c('originalArtist');
c('originalLyricist');
c('copyright');
c('encoder');
c('subtitle');
c('custom1');
c('custom2');
c('custom3');
c('custom4');
c('custom5');
c('custom6');
c('custom7');
c('custom8');
c('custom9');
c('custom10');
queries.push('INSERT INTO MediaText (rowid,' + comma_fields + ') SELECT _id AS rowid,' + comma_fields + ' FROM media');
queries.push('UPDATE db_info SET version = 4');
}
if (version < 5) {
queries.push(
'CREATE TABLE IF NOT EXISTS content_points (' +
'_id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'token INTEGER' +
')');
queries.push(
'CREATE TABLE IF NOT EXISTS content_changes (' +
'_id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'timestamp INTEGER,' +
'token INTEGER,' +
'item_type TEXT NOT NULL,' +
'item_oper TEXT NOT NULL,' +
'item_sync_id TEXT' +
')');
queries.push('CREATE INDEX IF NOT EXISTS idx_content_changes_token ON content_changes (token)');
queries.push('UPDATE db_info SET version = 5');
}
if (version < 6) {
queries.push(
'CREATE TABLE IF NOT EXISTS user_roles (' +
'key TEXT NOT NULL UNIQUE,' +
'label TEXT NOT NULL UNIQUE,' +
'access_level INTEGER NOT NULL UNIQUE,' +
'PRIMARY KEY(key)' +
')'
);
queries.push({
sql: 'INSERT INTO user_roles (key, label, access_level) VALUES (?, ?, ?)',
params: ['viewer', 'Viewer', 0]
});
queries.push({
sql: 'INSERT INTO user_roles (key, label, access_level) VALUES (?, ?, ?)',
params: ['editor', 'Editor', 1],
});
queries.push({
sql: 'INSERT INTO user_roles (key, label, access_level) VALUES (?, ?, ?)',
params: ['admin', 'Admin', 2],
});
queries.push('UPDATE db_info SET version = 6');
}
if (version < 7) {
queries.push(
'CREATE TABLE IF NOT EXISTS users (' +
'_id TEXT NOT NULL UNIQUE,' +
'name TEXT NOT NULL UNIQUE,' +
'display_name TEXT NOT NULL,' +
'role_key TEXT,' +
'password TEXT NOT NULL,' +
'FOREIGN KEY(role_key) REFERENCES user_roles(key),' +
'PRIMARY KEY(_id)' +
')'
);
//Default admin user with password 'admin'
queries.push({
sql: 'INSERT INTO users (_id, name, display_name, role_key, password) VALUES (?,?,?,?,?)',
params: ['pFHsj0iAabgvX1Mp', 'admin', 'Admin', 'admin', '$2a$04$DWc3YDK8zbOoAcWB81LZ0eegQ8nnx7XE9xLli3njgfVlP93JdYDSC']
});
queries.push('UPDATE db_info SET version = 7');
}
if (version < 8) {
queries.push(
'CREATE TABLE IF NOT EXISTS user_sessions (' +
'token TEXT NOT NULL UNIQUE,' +
'token_expire INTEGER NOT NULL,' + // Note: JS Date() format
'token_created INTEGER NOT NULL,' +
'user_id TEXT NOT NULL,' +
'useragent TEXT,' + // TODO: Configurable option to enable/disable useragent+IP session storage
'ip TEXT,' + // Purpose: Someone may wish to share their media server to the public & may wish to audit usage
'FOREIGN KEY(user_id) REFERENCES users(_id),' +
'PRIMARY KEY(token)' +
')'
);
queries.push('UPDATE db_info SET version = 8');
}
//execute queries in order
for (var query of queries) {
var stmt;
//if query is an object with sql + params
if (query.sql && query.params) {
stmt = this.sql.prepare(query.sql);
stmt.run(query.params);
}
//if query is just sql
else {
stmt = this.sql.prepare(query);
stmt.run();
}
}
callback();
}
catch (err) {
if (query) console.log(query);
console.log(err);
callback(err);
}
}
getMetas(path, topic, callback) {
debug('getMetas: ENTER');
if (typeof path != 'string') return callback('Path must be string');
//With prepared statements, we no longer need the _doubleApostrophe function.
var row = this.sql.getStatement('getMediaByPath').get(path);
if (row) {
var metas = this._assignMetasFromRow(row);
callback(null, metas);
}
else {
//callback('Query did not return any data');
callback(null, null);
}
}
/**
* Save metas of a particular track/media to db
* @param {string} path Path of media
* @param {any} mtime ?
* @param {import('../DataStructures').Metas} metas Track metas
* @param {function} callback Callbcak
*/
putMetas(path, mtime, metas, callback) {
debug('putMetas: ENTER');
if (!metas) {
callback('metas undefined');
return;
}
if (metas.mimeType == 'inode/directory') {
callback();
return;
}
path = PathNormalizer.normalize(path);
logger.verbose('putMetas: ' + path + ' metas: ' + Object.keys(metas));
//Determine which prepared SQL statement to execute
var selectorStmtId, statementParam;
if (!metas.sync_id) {
metas.sync_id = this._newSyncId(metas);
selectorStmtId = 'getIdAndSyncIdByPath';
statementParam = path;
}
else {
selectorStmtId = 'getIdBySyncId';
statementParam = metas.sync_id;
}
// Lock transaction so that the same sync_id isn't processed by another scanning thread at the same time (resulting in double insertion)
this.takeLock('media_insert', () => {
var itemExists = false;
//Check if item exists in the database
var row = this.sql.getStatement(selectorStmtId).get(statementParam);
if (row) {
itemExists = true;
metas.db_id = row._id;
metas.sync_id = row.sync_id;
}
var mv = this._multiValueArr2Str;
var nn = this._notNull;
var rating = -1;
if (metas.rating) rating = metas.rating;
else if (metas.ratings && metas.ratings.length) rating = metas.ratings[0].rating;
if (!metas.mimeType) metas.mimeType = PathNormalizer.getMime(path);
var updateStmtId = itemExists ? 'updateMediaBySyncId' : 'insertMedia';
var updateStmtParams = [path, metas.title, metas.album, mv(metas.artists), mv(metas.albumArtists), mv(metas.actors), mv(metas.authors), mv(metas.producers), mv(metas.directors), mv(metas.publishers), mv(metas.contributors), mv(metas.lyricists), mv(metas.conductors), metas.bpm, mv(metas.genres), rating, nn(metas.playcount), nn(metas.skipcount), nn(metas.bookmark), metas.comment, metas.duration * 1000, metas.size, metas.mimeType, metas.year, metas.originalTrackNumber, metas.originalDiscNumber, metas.lyrics, metas.last_time_played, nn(metas.volumeLeveling), nn(metas.volumeLevelTrack), nn(metas.volumeLevelAlbum), metas.parental_rating, metas.grouping, metas.tempo, metas.mood, metas.occasion, metas.quality, metas.isrc, metas.initialKey, metas.originalTitle, metas.originalArtist, metas.originalLyricist, metas.originalDate, metas.copyright, metas.encoder, metas.subtitle, metas.custom1, metas.custom2, metas.custom3, metas.custom4, metas.custom5, metas.custom6, metas.custom7, metas.custom8, metas.custom9, metas.custom10, JSON.stringify(metas.albumArts),
metas.sync_id //needs to be the last
];
var info = this.sql.getStatement(updateStmtId).run(updateStmtParams);
if (!itemExists) metas.db_id = info.lastInsertRowid;
assert(metas.db_id, 'db_id missing for ' + metas.sync_id);
if (info.changes < 1) {
console.warn('Changes < 1');
}
this.leaveLock('media_insert');
this._cleanCacheFor(['getTracklist', 'getFiles', 'filteredFileEntry']);
if (itemExists) this._logContentChange('media', 'updated', metas.sync_id);
else this._logContentChange('media', 'added', metas.sync_id);
callback();
});
}
/**
*
* @param {import('../DataStructures').Metas} metas
* @param {function} callback
*/
deleteMetas(metas, callback) {
debug('deleteMetas: ENTER');
assert(metas.db_id && metas.sync_id, 'DB or sync id missing for ' + metas.title + ', path: ' + metas.path);
logger.info('deleteMetas: db_id: ' + metas.db_id);
this.sql.getStatement('deleteMediaByPath').run(metas.db_id);
this._logContentChange('media', 'deleted', metas.sync_id);
if (callback) callback();
this._cleanCacheFor(['getTracklist', 'getFiles']);
}
getPlaylistContent(parent_guid, callback) {
debug('getPlaylistContent: ENTER');
assert(parent_guid, 'parent_guid must be specified.');
var playlists = [];
var playlistRows = this.sql.getStatement('getPlaylistsByParentGuid').all(parent_guid);
for (let row of playlistRows) {
playlists.push(this._playlistFromRow(row));
}
this.markItemContentChanged(parent_guid, false);
if (parent_guid) {
var files = [];
var fileRows = this.sql.getStatement('getPlaylistContent').all(parent_guid);
for (let row of fileRows) {
var file = this._assignMetasFromRow(row);
files.push(file);
}
debug(`getPlaylistContent: callback with playlists & files; lengths = ${playlists.length}, ${files.length}`);
callback(null, playlists, files);
}
else {
debug(`getPlaylistContent: callback with playlists, length = ${playlists.length}`);
callback(null, playlists);
}
}
/**
* Get playlists by [params]
* @param {object} params params
* @param {array} params.guidArray guidArray
*/
getPlaylistsBy(params) {
debug('getPlaylistsBy: ENTER');
var playlists = [];
assert(params.guidArray, 'getPlaylistsBy params: only guidArray is supported now');
if (params.guidArray.length != 0) {
var rows = this.sql.db.exec('SELECT * FROM playlists WHERE guid IN ("' + params.guidArray.join('","') + '")');
if (rows && rows[0]) {
for (var row of rows) {
playlists.push(this._playlistFromRow(row));
}
}
}
return playlists;
}
getFiles(callback) {
debug('getFiles: ENTER');
var cache = this._getCacheFor('getFiles');
if (cache.files) {
debug(`getFiles: returning cache.files, cache.files=${typeof cache.files}, length=${cache.files.length}`);
callback(null, cache.files);
} else {
cache.files = [];
var files = cache.files;
var startTime = Date.now();
var rows = this.sql.getStatement('getMedia').all();
for (var row of rows) {
var metas = this._assignMetasFromRow(row);
files.push(metas);
}
logger.info(`${files.length} files have been read from database in ${(Date.now() - startTime)} ms`);
//console.trace();
callback(null, files);
}
}
/**
* Get files by search phrase
* @param {object} params params
* @param {string} [params.searchPhrase] Search phrase
* @param {function} callback callback
* @returns {Array<import('../DataStructures').Metas>} files
*/
getFilesBy(params, callback) {
debug('getFilesBy: ENTER');
if (!params || !params.searchPhrase) {
// all files
return this.getFiles(callback);
}
else {
logger.info(`Full text search for: ${params.searchPhrase}`);
var files = [];
var rows = this.sql.getStatement('getMediaByTextSearch').all(params.searchPhrase);
for (var row of rows) {
var metas = this._assignMetasFromRow(row);
files.push(metas);
}
callback(null, files);
}
}
/**
* Get individual file by id or sync id
* @param {object} criteria search criteria
* @param {number} [criteria.id] ID
* @param {number} [criteria.sync_id] sync ID
* @param {function} callback callback
*/
getFile(criteria, callback) {
debug('getFile: ENTER');
assert(criteria.id || criteria.sync_id, 'file id undefined');
var file;
var row;
if (criteria.id) row = this.sql.getStatement('getMediaById').get(criteria.id);
else if (criteria.sync_id) row = this.sql.getStatement('getMediaBySyncId').get(criteria.sync_id);
if (row) {
file = this._assignMetasFromRow(row);
callback(null, file);
}
else {
callback (`File with ${criteria.id?'id':'sync_id'} ${criteria.id||criteria.sync_id} not found`);
}
}
garbageFilesOutOfFolders(folders, callback) {
debug('garbageFilesOutOfFolders: ENTER');
this.getFiles((err, files) => {
var delete_ids = [];
var deleted_files = [];
for (var f of files) {
if (!this._fileIsInFolders(f, folders)) {
delete_ids.push(f.db_id);
deleted_files.push(f);
}
}
if (delete_ids.length) {
var result = this.sql.getStatement('deleteMediaByIdList').run(delete_ids.join(','));
logger.info(`garbageFilesOutOfFolders: result=${JSON.stringify(result)}`);
this._cleanCacheFor(['getTracklist', 'getFiles']);
Async.eachSeries(deleted_files,
(metas, cbk) => {
this._logContentChange('media', 'deleted', metas.sync_id);
if (cbk) cbk();
}, () => {
if (callback)
callback(err, deleted_files);
}
);
} else {
if (callback)
callback(null, deleted_files);
}
});
}
/**
*
* @param {import('../DataStructures').Playlist} item Playlist to insert
* @param {*} service
* @param {*} callback
*/
putPlaylist(item, service, callback) {
debug('putPlaylist: ENTER');
logger.info('putPlaylist: ' + item.name + ', guid: ' + item.guid + ', last_modified: ' + item.last_modified);
var itemExists = false;
var lastModified;
var row = this.sql.getStatement('getPlaylistLastModifiedByGuid').get(item.guid);
if (row) {
itemExists = true;
//Not sure if a "select x from table" statement returns an object or just the single variable
//Might have to re-code it if this becomes an error
if (typeof row != 'object') throw new TypeError('hey there, sorry, you\'ll have to fix my code lol. Check the comment above this line');
lastModified = row.last_modified;
if (lastModified > item.last_modified) {
return callback('Playlist to put is older: ' + item.last_modified + ' than current ' + lastModified);
}
else if (lastModified == item.last_modified) {
//up to date, already in database
return callback();
}
}
//determine whether to update playlist or insert playlist
// Parameters are the same either way
var statementId = itemExists ? 'updatePlaylistByGuid' : 'insertPlaylist';
var parameters = [item.name, item.parent_guid, item.last_modified, item.criteria, item.guid];
//write playlist
var plWriteResult = this.sql.getStatement(statementId).run(parameters);
//clear playlistItems
var plItemsDeleteResult = this.sql.getStatement('deletePlaylistItems').run(item.guid);
//insert playlistItems
var stmtInsert = this.sql.getStatement('insertPlaylistItem');
for (var i = 0; i < item.track_ids.length; i++) {
stmtInsert.run(item.guid, item.track_ids[i], i);
}
//log content change
this.markItemContentChanged(item.parent_guid, true);
if (itemExists) this._logContentChange('playlist', 'updated', item.guid);
else this._logContentChange('playlist', 'added', item.guid);
logger.trace(`playlistWriteResult=${JSON.stringify(plWriteResult)}, playlistItemsDeleteResult=${JSON.stringify(plItemsDeleteResult)}`);
//end
callback();
}
/**
*
* @param {import('../DataStructures').Playlist|object} item playlist to delete
* @param {function} callback callback
*/
deletePlaylist(item, callback) {
debug('deletePlaylist: ENTER');
logger.info('deletePlaylist: guid: ' + item.guid);
this.sql.getStatement('deletePlaylistByGuid').run(item.guid);
this.sql.getStatement('clearUnusedPlaylists').run();
this.markItemContentChanged(item.parent_guid, true);
this._logContentChange('playlist', 'deleted', item.guid);
callback();
}
/**
*
* @param {number} token token
* @param {function} callback callback
*/
getContentChanges(token, callback) {
debug('getContentChanges: ENTER');
//2019-09-19 JL: changed deprecation callback into an assert
assert((!isNaN(token)), 'non-integer tokens deprecated');
var tokenExists;
var changes = [];
var tokenCheck = this.sql.getStatement('checkContentPointsByToken').get(token);
if (tokenCheck) tokenExists = true;
if (!tokenExists) return callback(`Token ${token} does not exist`);
else {
//get changes after token
var rows = this.sql.getStatement('getContentChangesByToken').all(token);
if (rows && rows[0]) {
for (var row of rows) {
changes.push({
item_type: row.item_type,
item_oper: row.item_oper,
item_sync_id: row.item_sync_id
});
}
callback(null, changes);
}
//2020-09-19 JL: Added a catch for when no content changes were found.
// Not sure if we want to keep it this way. Easu to revert.
else {
callback('No content changes were found.');
}
}
}
/**
* Get user by name
* @param {string} username Username
* @returns {import('../DataStructures').User} user data
*/
getUserByName(username) {
debug('getUserByName: ENTER');
if (typeof username == 'string') {
return this.sql.getStatement('getUserByName').get(username);
}
else {
debug('getUserByName: Username not provided');
return null;
}
}
/**
* Get user by ID
* @param {string} _id User ID
* @returns {import('../DataStructures').User} user data
*/
getUserById(_id) {
debug('getUserById: ENTER');
return this.sql.getStatement('getUserById').get(_id);
}
/**
* Create user with given data
* @param {Object} userData User data to insert
* @returns {import('better-sqlite3').RunResult} Run Result
*/
insertUser(userData) {
debug('insertUser: ENTER');
if (typeof userData != 'object')throw new TypeError('sqlRegistry/insertUser: User must be of type object.');
var _id = crypto.randomBytes(12).toString('base64');
var name = userData.username;
var display_name = userData.displayname || userData.display_name || '?';
var role_key = userData.role_key;
var password = userData.password;
if (!name || !role_key || !password) {
throw new Error('sqlRegistry/insertUser: Must specify username, role_key, and password');
}
//Check if _id has a clash; if so, just call this function again
var idClashCheck = this.sql.getStatement('getUserById').get(_id);
if (idClashCheck) {
return this.insertUser(userData);
}
else {
return this.sql.getStatement('insertUser').run(_id, name, display_name, role_key, password);
}
}
/**
* Update user with given data
* @param {string} userId userID
* @param {Object} userData User data to update
* @returns {import('better-sqlite3').RunResult} Run Result
*/
updateUser(userId, userData) {
debug('updateUser: ENTER');
if (typeof userId != 'string') throw new TypeError('userId must be a string');
if (typeof userData != 'object') throw new TypeError('userData must be an object');
var existingUserData = this.getUserById(userId);
const name = userData.name || existingUserData.name;
const display_name = userData.display_name || existingUserData.display_name;
const role_key = userData.role_key || existingUserData.role_key;
const password = userData.password || existingUserData.password;
return this.sql.getStatement('updateUser').run(name, display_name, role_key, password, userId);
}
/**
* Get user from session token
* @param {string} token Auth token
* @returns {import('../DataStructures').User} user data
*/
getUserFromSessionToken(token) {
debug('getUserFromSessionToken: ENTER');
return this.sql.getStatement('getUserFromSessionToken').get(token, Date.now());
}
/**
*
* @param {string} token New token
* @param {Date|Number} token_expire Expiration time
* @param {string} userId User ID
* @param {string} [useragent] Useragent, for logging
* @param {string} [ip] IP address of user, for logging
*/
insertSession(token, token_expire, userId, useragent, ip) {
debug('insertSession: ENTER');
token_expire = new Date(token_expire).valueOf();
assert(!isNaN(token_expire.valueOf()), 'token_expire is an invalid date.');
assert(typeof userId === 'string', 'userId must be string.');
assert(typeof token === 'string', 'token must be a string,');
if (!useragent) useragent = null;
if (!ip) ip = null;
return this.sql.getStatement('insertSession').run(token, token_expire, Date.now(), userId, useragent, ip);
}
/**
* Get role by key
* @param {string} key Role key
* @returns {import('../DataStructures').Role} Role info
*/
getRole(key) {
debug('getRole: ENTER');
return this.sql.getStatement('getRole').get(key);
}
/**
* Get list of all roles, in order of access level
* @returns {Array<import('../DataStructures').Role>} Role info
*/
getRoles() {
debug('getRoles: ENTER');
return this.sql.getStatement('getRoles').all();
}
// == Public General Functions ==
putConfig(config, callback) {
debug('putConfig: ENTER');
//console.log(config.collections);
this._putCollections(config.collections, callback);
}
getConfig(config, callback) {
debug('getConfig: ENTER');
var rows = this.sql.getStatement('getCollections').all();
var collections = [];
if (rows && rows[0]) {
for (var row of rows) {
var _config = JSON.parse(row.config);
collections.push({
id: row._id,
name: row.title,
folders: _config.folders,
type: _config.type
});
}
//Only override collections if some already exist in the database
config.collections = collections;
}
callback(null, config);
}
takeLock(lockName, callback) {
debug('takeLock: ENTER');
var semaphores = this._semaphores;
if (!semaphores) {
semaphores = {};
this._semaphores = semaphores;
}
var semaphore = semaphores[lockName];
if (!semaphore) {
semaphore = new Semaphore(lockName);
semaphores[lockName] = semaphore;
}
semaphore.take(callback);
}
leaveLock(lockName) {
debug('leaveLock: ENTER');