diff --git a/main/index.js b/main/index.js index 15d41e2..871f1a6 100644 --- a/main/index.js +++ b/main/index.js @@ -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') @@ -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) diff --git a/main/lib/artwork-cache/index.js b/main/lib/artwork-cache/index.js index fc1a571..7286268 100644 --- a/main/lib/artwork-cache/index.js +++ b/main/lib/artwork-cache/index.js @@ -1,4 +1,3 @@ -var level = require('level') // var electron = require('electron') var path = require('path') // var mkdirp = require('mkdirp') @@ -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 diff --git a/package.json b/package.json index 8152417..99449ac 100644 --- a/package.json +++ b/package.json @@ -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",