Skip to content

Commit 7bfc482

Browse files
authored
Merge pull request #253 from hypermodules/update-metadata-parser
Update metadata parser
2 parents 50ed9fb + 5d09dad commit 7bfc482

File tree

8 files changed

+43
-100
lines changed

8 files changed

+43
-100
lines changed

main/lib/artwork-cache/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var path = require('path')
44
var pump = require('pump')
55
var crypto = require('crypto')
66
var mkdirp = require('mkdirp')
7-
var { artwork, fromBuffer } = require('./util')
7+
var { artwork } = require('./util')
8+
var BufferList = require('bl')
89

910
// var configPath = (electron.app || electron.remote.app).getPath('userData')
1011
// var artworkCachePath = path.join(configPath, 'artwork-cache')
@@ -37,7 +38,7 @@ class ArtworkCache {
3738
return cb(null, blobPath)
3839
} else {
3940
var writeStream = self._blobs.createWriteStream()
40-
pump(fromBuffer(buff), writeStream, function (err) {
41+
pump((new BufferList()).append(buff), writeStream, function (err) {
4142
if (err) return cb(err)
4243
return self._blobs.resolve(writeStream.key, cb)
4344
})

main/lib/artwork-cache/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var test = require('tape')
2-
var {artwork, fromBuffer} = require('./util')
2+
var {artwork} = require('./util')
33
var path = require('path')
44
var concatStream = require('concat-stream')
55
var isBuffer = require('is-buffer')
@@ -8,6 +8,7 @@ var bufferEqual = require('buffer-equal')
88
var tmp = require('temporary-directory')
99
var ArtworkCache = require('./index.js')
1010
var testData = require('./test-data')
11+
var BufferList = require('bl')
1112

1213
test('get artwork from file', function (t) {
1314
artwork(testData.mp3WithArtwork, function (err, imageBuf) {
@@ -22,7 +23,8 @@ test('can stream buffers', function (t) {
2223

2324
function bufferTests (err, imageBuf) {
2425
t.error(err, 'got artwork buffer')
25-
var imageBufferStream = fromBuffer(imageBuf)
26+
var imageBufferStream = new BufferList()
27+
imageBufferStream.append(imageBuf)
2628
var concat = concatStream(gotPic)
2729
var streamedBuff
2830

main/lib/artwork-cache/util.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
1-
var from = require('from2')
2-
var fs = require('fs')
31
var mm = require('music-metadata')
42
var get = require('lodash.get')
5-
6-
exports.fromBuffer = fromBuffer
7-
8-
function fromBuffer (buffer) {
9-
return from(function (size, next) {
10-
if (buffer.length <= 0) return next(null, null)
11-
var chunk = buffer.slice(0, size)
12-
buffer = buffer.slice(size)
13-
next(null, chunk)
14-
})
15-
}
3+
var fs = require('fs')
164

175
function metadata (path, cb) {
18-
var audioStream = fs.createReadStream(path)
19-
var returned = false // TODO clean up racy code
20-
audioStream.on('error', (err) => {
21-
if (!returned) {
22-
returned = true
6+
fs.stat(path, function (err, stats) {
7+
if (err) return cb(err)
8+
mm.parseFile(path, {
9+
native: true,
10+
duration: true,
11+
skipCovers: false
12+
}).then(function (md) {
13+
return cb(null, md)
14+
}).catch(function (err) {
2315
return cb(err)
24-
}
25-
})
26-
mm.parseStream(audioStream, {native: true}, function (err, metadata) {
27-
// important note, the stream is not closed by default. To prevent leaks, you must close it yourself
28-
audioStream.destroy()
29-
if (!returned) {
30-
returned = true
31-
return cb(err, err ? null : metadata)
32-
}
16+
})
3317
})
3418
}
3519

main/lib/audio-library/sort.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ function sort ([keyA, aObj], [keyB, bObj]) {
44
// if (aObj.albumartist[0] > bObj.albumartist[0]) return 1
55

66
// sort by artist
7-
if (aObj.artist[0] < bObj.artist[0]) return -1
8-
if (aObj.artist[0] > bObj.artist[0]) return 1
7+
if (aObj.artist < bObj.artist) return -1
8+
if (aObj.artist > bObj.artist) return 1
99

1010
// then by album
1111
if (aObj.album < bObj.album) return -1

main/track-dict.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
var fs = require('fs')
21
var path = require('path')
32
var walker = require('folder-walker')
4-
var mm = require('musicmetadata')
3+
var mm = require('music-metadata')
54
var writer = require('flush-write-stream')
65
var filter = require('through2-filter')
76
var pump = require('pump')
@@ -18,6 +17,7 @@ function makeTrackDict (paths, cb) {
1817

1918
function handleEos (err) {
2019
if (err) return cb(err)
20+
console.log('')
2121
cb(null, newTrackDict)
2222
}
2323
}
@@ -35,7 +35,7 @@ function concatTrackDict (obj) {
3535
parseMetadata(data, handleMeta)
3636

3737
function handleMeta (err, meta) {
38-
if (err) return cb(err)
38+
if (err) throw err
3939
obj[meta.filepath] = meta
4040
cb(null)
4141
}
@@ -45,25 +45,11 @@ function concatTrackDict (obj) {
4545

4646
function parseMetadata (data, cb) {
4747
var { filepath } = data
48-
var readableStream = fs.createReadStream(filepath)
49-
mm(readableStream, { duration: true }, (err, meta) => {
50-
readableStream.destroy()
51-
if (err) {
52-
switch (err.message) {
53-
case 'Could not find metadata header':
54-
console.warn(err.message += ` (file: ${filepath})`)
55-
break
56-
case 'expected frame header but was not found':
57-
console.warn(err.message += ` (file: ${filepath})`)
58-
break
59-
default:
60-
// Ignore errors
61-
console.warn(err.message += ` (file: ${filepath})`)
62-
return cb(null)
63-
}
64-
}
65-
// delete meta.picture
66-
// console.dir(meta, {colors: true, depth: 5})
48+
mm.parseFile(filepath, {
49+
duration: true,
50+
native: false,
51+
skipCovers: true
52+
}).then(meta => {
6753
var {
6854
albumartist,
6955
title,
@@ -72,17 +58,17 @@ function parseMetadata (data, cb) {
7258
year,
7359
track,
7460
disk,
75-
genre,
76-
duration
77-
} = meta
61+
genre
62+
} = meta.common
7863

64+
var { duration } = meta.format
7965
if (!title) {
8066
var { basename } = data
8167
var ext = path.extname(basename)
8268
title = path.basename(basename, ext)
8369
}
8470

85-
cb(null, {
71+
return Promise.resolve({
8672
albumartist,
8773
title,
8874
artist,
@@ -94,5 +80,14 @@ function parseMetadata (data, cb) {
9480
disk,
9581
genre
9682
})
83+
}).catch(err => {
84+
// Ignore errors
85+
console.log(err.message += ` (file: ${filepath})`)
86+
var { basename } = data
87+
var ext = path.extname(basename)
88+
var title = path.basename(basename, ext)
89+
return Promise.resolve({ title, filepath })
90+
}).then(meta => {
91+
cb(null, meta)
9792
})
9893
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
],
3535
"dependencies": {
3636
"@bret/truthy": "^1.0.1",
37+
"bl": "^1.2.1",
3738
"choo": "^6.0.0",
3839
"choo-devtools": "^1.5.0",
3940
"choo-log": "^7.2.1",
@@ -53,16 +54,14 @@
5354
"flush-write-stream": "^1.0.2",
5455
"folder-walker": "^3.0.0",
5556
"format-duration": "^1.0.0",
56-
"from2": "^2.3.0",
5757
"fy-shuffle": "^1.0.0",
5858
"global": "^4.3.2",
5959
"lodash.debounce": "^4.0.8",
6060
"lodash.get": "^4.4.2",
6161
"lodash.throttle": "^4.1.1",
6262
"mkdirp": "^0.5.1",
6363
"mousetrap": "^1.6.1",
64-
"music-metadata": "^0.6.1",
65-
"musicmetadata": "^2.0.4",
64+
"music-metadata": "^0.8.7",
6665
"nanobus": "^4.1.0",
6766
"nanocomponent": "^6.0.0",
6867
"nanologger": "^1.3.0",

renderer/lib/artwork.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

renderer/lib/metadata.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)