-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (56 loc) · 1.35 KB
/
index.js
File metadata and controls
67 lines (56 loc) · 1.35 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
'use strict'
const path = require('path')
const co = require('co')
const pify = require('promise.ify')
const fs = pify.all(require('fs-extra'))
const _ = require('lodash')
const parser = require('./lib/parser.js')
const LocalUtil = require('./lib/util.js')
const FILE_TOO_SMALL = 'file too small'
/**
* get ID3 info
*/
module.exports = co.wrap(function*(fd) {
if (typeof fd !== 'number') {
const filename = path.resolve(fd)
fd = yield fs.openAsync(filename, 'r')
}
const stat = yield fs.fstatAsync(fd)
if (stat.size < 10) {
throw new Error(FILE_TOO_SMALL)
}
const buf = Buffer.alloc(4)
yield fs.readAsync(
fd, buf,
0, buf.length, 6 // offset, length, position
)
const minSize = LocalUtil.getID3TotalSize(buf)
if (stat.size <= minSize) {
throw new Error(FILE_TOO_SMALL)
}
const stream = fs.createReadStream('', {
fd,
})
const p = parser.create()
process.nextTick(() => {
stream.pipe(p)
})
const info = yield new Promise(resolve => {
p.on('readable', function() {
resolve(p.read())
})
})
const ret = {}
let singer = _.find(info.ID3, ['id', 'TPE1'])
let title = _.find(info.ID3, ['id', 'TIT2'])
let album = _.find(info.ID3, ['id', 'TALB'])
singer = singer.content
title = title.content
album = album.content
return {
singer,
title,
album,
raw: info,
}
})