-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (158 loc) · 4.51 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
// var assert = require('assert')
var Idx = require('level-idx')
var extend = require('xtend')
var Level = require('level')
var sub = require('subleveldown')
var path = require('path')
var bytewise = require('bytewise')
var walker = require('folder-walker')
var filter = require('through2-filter')
var through = require('through2')
var LevelBatch = require('level-batch-stream')
var map = require('through2-map')
var parallel = require('concurrent-writable')
var byteStream = require('byte-stream')
var pump = require('pump')
var fs = require('fs')
var mm = require('musicmetadata')
function keyFn (filePath) {
return path.normalize(filePath).split(path.sep)
}
var validExtensions = ['m4a', 'mp3']
function isValidFile (data) {
if (data.type !== 'file') return false
let ext = path.extname(data.basename).substring(1)
return validExtensions.includes(ext)
}
function MusicLibrary (location, paths, opts) {
if (!(this instanceof MusicLibrary)) return new MusicLibrary(location, paths, opts)
if (!opts) opts = {}
if (!paths) paths = []
if (!Array.isArray(paths)) paths = [paths]
var dbLevel = Level(path.join(location, 'db'))
var idbLevel = Level(path.join(location, 'idb'))
this.paths = paths
this.db = sub(dbLevel, 'files', {keyEncoding: bytewise, valueEncoding: 'json'})
this.idb = idbLevel
this.index = Idx(this.db, this.idb, {keyEncoding: bytewise})
.by('AlbumArtistYear', [
'meta.albumartist',
'meta.artist',
'meta.year',
'meta.album',
'meta.disk.no',
'meta.track.no',
'meta.title',
'filepath'
])
}
MusicLibrary.prototype.parseMetadata = function (filepath, opts, cb) {
if (typeof opts === 'function') return this.parseMetadata(filepath, {}, opts)
console.log(filepath)
var fileStream = fs.createReadStream(filepath)
mm(fileStream, opts, handleMM)
function handleMM (err, meta) {
fileStream.close()
if (!meta) meta = {}
if (err) {
err.message += ` (file: ${filepath})`
meta.error = err
}
if (!meta.title) {
var basename = path.basename(filepath)
var ext = path.extname(basename)
meta.title = path.basename(basename, ext)
}
cb(null, meta)
}
}
MusicLibrary.prototype.scan = function (opts, cb) {
if (typeof opts === 'function') return this.scan({}, opts)
var self = this
var db = this.db
var fileStream = walker(this.paths)
var filterInvalid = filter.obj(isValidFile)
var filterAdded = through.obj(dbStat)
var levelBatch = new LevelBatch(db)
var makeOp = map.obj(operation)
var batcher = byteStream({time: opts.time || 200, limit: opts.limit || 100})
var paralleLevelBatch = parallel(levelBatch, opts.parallel || 10)
var parseMetaData = through.obj(parser)
function parser (chunk, enc, cb) {
self.parseMetadata(chunk.filepath, handleParse.bind(this))
function handleParse (err, meta) {
if (err) return cb(err)
delete meta.picture
this.push(extend(chunk, {meta: meta}))
cb()
}
}
function operation (chunk) {
var key = keyFn(chunk.filepath)
return {
type: 'put',
key: key,
value: chunk
}
}
function dbStat (chunk, enc, cb) {
db.get(keyFn(chunk.filepath), addFound.bind(this))
function addFound (err, value) {
if (err && err.notFound) {
this.push(chunk)
return cb()
}
return cb(err)
}
}
return pump(
fileStream,
filterInvalid,
filterAdded,
parseMetaData,
makeOp,
batcher,
paralleLevelBatch,
cb
)
}
MusicLibrary.prototype.clean = function (opts, cb) {
if (typeof opts === 'function') return this.clean({}, opts)
var dbStream = this.db.createReadStream()
var filterStated = through.obj(fsStat)
var makeOp = map.obj(operation)
var batcher = byteStream({time: opts.time || 200, limit: opts.limit || 100})
var levelBatch = new LevelBatch(this.db)
var paralleLevelBatch = parallel(levelBatch, opts.parallel || 10)
var paths = this.paths
function operation (chunk) {
var key = keyFn(chunk.value.filepath)
return {
type: 'del',
key: key
}
}
function fsStat (chunk, enc, cb) {
if (!paths.some(inPaths)) {
this.push(chunk)
return cb()
}
fs.stat(chunk.value.filepath, pushMissing.bind(this))
function pushMissing (err, value) {
if (err) this.push(chunk)
return cb()
}
function inPaths (path) {
return chunk.value.root === path
}
}
return pump(
dbStream,
filterStated,
makeOp,
batcher,
paralleLevelBatch,
cb
)
}
module.exports = MusicLibrary