Skip to content

Commit

Permalink
Merge pull request #181 from hypermodules/cabs
Browse files Browse the repository at this point in the history
Remove level dependency
  • Loading branch information
bcomnes authored Jun 17, 2017
2 parents 8e283a8 + e2a00f6 commit 04dffc3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
22 changes: 19 additions & 3 deletions main/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var { app, ipcMain, globalShortcut } = require('electron')
var get = require('lodash.get')
var audio = require('./audio')
var menu = require('./menu')
var player = require('./player')
Expand Down Expand Up @@ -53,11 +54,26 @@ app.on('ready', () => {
state.currentIndex = newIndex
broadcast('queue', newIndex)
var key = state.trackOrder[newIndex]
artwork.cache.getPath(key, handleArtworkPath)
updateArtwork(key)
}

function handleArtworkPath (err, blobPath) {
if (err) return console.error(err)
function updateArtwork (key) {
// Store this kind of crap in main library db
var blobPath = get(state, `trackDict[${key}].artwork`)
if (blobPath) {
return handleArtworkPath(blobPath)
} else {
artwork.cache.getPath(key, handleGetPath)
}

function handleGetPath (err, blobPath) {
if (err) return console.error(err)
state.trackDict[key].artwork = blobPath
handleArtworkPath(blobPath)
}
}

function handleArtworkPath (blobPath) {
if (state.artwork !== blobPath) {
state.artwork = blobPath
if (player.win) player.win.send('artwork', blobPath)
Expand Down
39 changes: 5 additions & 34 deletions main/lib/artwork-cache/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var level = require('level')
// var electron = require('electron')
var path = require('path')
// var mkdirp = require('mkdirp')
Expand All @@ -19,61 +18,33 @@ class ArtworkCache {
this._directory = dir || path.join(process.cwd(), 'artwork-cache')
mkdirp.sync(this._directory)
this._algo = 'sha256'
this._db = level(path.join(this._directory, 'cache-db'))
// this._db = level(path.join(this._directory, 'cache-db'))
this._blobs = blobs({
path: path.join(this._directory, 'blobs'),
algo: 'sha256'
})
}

extractArt (filePath, cb) {
getPath (filePath, cb) {
var self = this
artwork(filePath, function (err, buff) {
if (err) return cb(err)
if (buff === null) {
return self._db.put(filePath, false, function (err) {
cb(err, err ? undefined : null)
})
}
if (buff === null) return cb(null, null)
var digest = crypto.createHash(self._algo).update(buff).digest('hex')
self._blobs.resolve(digest, function (err, blobPath) {
if (err) return cb(err)
if (blobPath) {
self._db.put(filePath, digest, function (err) {
return cb(err, err ? null : blobPath)
})
return cb(null, blobPath)
} else {
var writeStream = self._blobs.createWriteStream()
pump(fromBuffer(buff), writeStream, function (err) {
if (err) return cb(err)
self._db.put(filePath, writeStream.key, function (err) {
if (err) return cb(err)
return self.getPath(filePath, cb)
})
return self._blobs.resolve(writeStream.key, cb)
})
}
})
})
}

getPath (filePath, cb) {
var self = this
this._db.get(filePath, function (err, key) {
if (err) {
if (err.notFound) return self.extractArt(filePath, cb)
return cb(err)
}
// image was determined to be missing
if (key === false) return cb(null, null)
// we have a key, lets get the file path
self._blobs.resolve(key, function (err, blobPath) {
if (err) return cb(err)
if (blobPath) return cb(null, blobPath)
// We have a key, but the blob is missing. regenerate
return self.extractArt(filePath, cb)
})
})
}
}

module.exports = ArtworkCache
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"format-duration": "^1.0.0",
"from2": "^2.3.0",
"global": "^4.3.2",
"level": "^1.7.0",
"lodash.debounce": "^4.0.8",
"lodash.get": "^4.4.2",
"lodash.throttle": "^4.1.1",
Expand Down

0 comments on commit 04dffc3

Please sign in to comment.