-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
289 lines (237 loc) · 8.94 KB
/
index.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
const audioExtensions = require('audio-extensions');
const fs = require('fs');
const mm = require('music-metadata');
const path = require('path');
const readdir = require('readdir-enhanced');
const watch = require('node-watch');
const { EventEmitter } = require('events');
// each of the columns in our database table
const TRACK_ATTRS = [
'path', 'mtime', 'title', 'artist', 'album', 'year', 'duration', 'track_no',
'tags', 'is_vbr', 'bitrate', 'codec', 'container'
];
const CREATE_TABLE = fs.readFileSync(path.join(__dirname, 'tracks.sql'))
.toString();
const UPSERT_TRACK =
`insert into tracks (${TRACK_ATTRS}) values ` +
`(${TRACK_ATTRS.map(() => '?').join(',')}) on conflict(path) do update ` +
`set ${TRACK_ATTRS.slice(1).map(attr => `${attr}=excluded.${attr}`)}`;
function isAudioFile(file) {
const ext = path.extname(file).slice(1);
return audioExtensions.indexOf(ext) !== -1;
}
class SyncMusicDb extends EventEmitter {
constructor({ db, dir, delay = 1000, tableName = 'tracks',
ignoreExt = true }) {
super();
this.db = db;
this.dir = path.resolve(dir);
this.tableName = tableName;
this.delay = delay;
this.ignoreExt = ignoreExt;
// { path: fs.stat.mtimeMs }
this.localMtimes = new Map();
// is the initial sync done and are we ready to listen for new changes?
this.isReady = false;
// is dir up-to-date with db?
this.isSynced = false;
}
// get each column of (TRACK_ATTRS) from the media file
static async getMetaData(path) {
try {
const { common, format } = await mm.parseFile(path);
const isVbr =
format.codec === 'MP3' && /^v/i.test(format.codecProfile);
return {
title: common.title,
artist: common.artist,
album: common.album,
year: common.year,
duration: Math.floor(format.duration),
track_no: (common.track ? common.track.no : null),
tags: JSON.stringify(common.genre),
is_vbr: isVbr,
bitrate: Math.floor(format.bitrate / 1000),
codec: format.codec,
container: format.container
};
} catch (e) {
return {};
}
}
async createTable() {
await this.db.exec(
CREATE_TABLE.replace(/\$table_name/g, this.tableName));
}
async prepareStatements() {
this.removeDirStmt =
await this.db.prepare('delete from tracks where path like ?');
this.removeTrackStmt =
await this.db.prepare('delete from tracks where path = ?');
this.upsertTrackStmt = await this.db.prepare(UPSERT_TRACK);
}
async finalizeStatements() {
for (const prefix of ['removeDir', 'removeTrack', 'upsertTrack']) {
await this[`${prefix}Stmt`].finalize();
delete this[`${prefix}Stmt`];
}
}
// remove all tracks that begin with directory
async removeDbDir(dir) {
await this.removeDirStmt.run(`${dir}${path.sep}%`);
this.emit('remove', dir);
}
// remove a single track based on path
async removeDbTrack(trackPath) {
await this.removeTrackStmt.run(trackPath);
this.emit('remove', trackPath);
}
// add a single track
async upsertDbTrack(track) {
await this.upsertTrackStmt.run(TRACK_ATTRS.map(attr => track[attr]));
this.emit('add', track);
}
// grab every file recursively in the dir specified and set their last-
// modified time in this.localMtimes map
refreshLocalMtimes() {
this.localMtimes.clear();
return new Promise((resolve, reject) => {
const dirStream = readdir.stream(this.dir, {
basePath: this.dir,
deep: true,
stats: true
});
dirStream
.on('file', stats => {
if (this.ignoreExt && !isAudioFile(stats.path)) {
return;
}
this.localMtimes.set(stats.path, Math.floor(stats.mtimeMs));
})
.on('end', () => resolve())
.on('error', err => reject(err))
.resume();
});
}
// remove tracks that don't exist on the filesystem from our database,
// and remove files from localMtimes that have up-to-date database entries
async removeDeadTracks() {
const query = 'select path as p, mtime from tracks';
await this.db.exec('begin transaction');
await this.db.each(query, async (err, { p, mtime }) => {
if (err) {
throw err;
}
const localMtime = this.localMtimes.get(p);
if (!localMtime) {
await this.removeDbTrack(p);
} else if (localMtime === mtime) {
this.localMtimes.delete(p);
}
});
await this.db.exec('commit');
}
// get the metadata from each file in localMtimes and add them to the
// database
async addUpdatedTracks() {
await this.db.exec('begin transaction');
for (const [ path, mtime ] of this.localMtimes) {
const track = await SyncMusicDb.getMetaData(path);
Object.assign(track, { path, mtime });
await this.upsertDbTrack(track);
}
await this.db.exec('commit');
this.localMtimes.clear();
}
// listen for file updates or removals and update the database accordingly
refreshWatcher() {
this.watcher = watch(this.dir, {
delay: this.delay,
recursive: true
}, async (evt, name) => {
this.isSynced = false;
this.emit('synced', this.isSynced);
try {
if (evt === 'update') {
const stats = await fs.promises.stat(name);
if (stats.isDirectory()) {
const files = await readdir(name, {
basePath: name,
deep: true,
stats: true
});
await this.db.exec('begin transaction');
for (const file of files) {
if (file.isDirectory()) {
continue;
}
if (this.ignoreExt && !isAudioFile(file.path)) {
continue;
}
const track =
await SyncMusicDb.getMetaData(file.path);
Object.assign(track, {
path: file.path,
mtime: Math.floor(file.mtimeMs)
});
await this.upsertDbTrack(track);
}
await this.db.exec('commit');
} else {
if (this.ignoreExt && !isAudioFile(name)) {
return;
}
await this.upsertDbTrack(Object.assign({
path: name,
mtime: Math.floor(stats.mtimeMs)
}, await SyncMusicDb.getMetaData(name)));
}
} else if (evt === 'remove') {
// run both because if it isn't a directory, nothing will
// get deleted
await this.removeDbTrack(name);
await this.removeDbDir(name);
}
} catch (e) {
if (e.code !== 'ENOENT') {
this.emit('error', e);
}
}
this.isSynced = true;
this.emit('synced', this.isSynced);
});
}
// start!
refresh() {
this.close()
.then(() => this.prepareStatements())
.then(() => this.refreshLocalMtimes())
.then(() => this.removeDeadTracks())
.then(() => this.addUpdatedTracks())
.then(() => {
this.refreshWatcher();
this.watcher.on('ready', () => {
this.isReady = true;
this.isSynced = true;
this.emit('synced', this.isSynced);
this.emit('ready');
});
})
.catch(err => this.emit('error', err));
return this;
}
async close() {
if (this.removeDirStmt) {
await this.finalizeStatements();
}
if (this.watcher) {
this.watcher.close();
this.watcher = null;
}
this.isReady = false;
this.isSynced = false;
this.emit('synced', this.isSynced);
}
};
SyncMusicDb.TRACK_ATTRS = TRACK_ATTRS;
module.exports = SyncMusicDb;