media-library
Advanced tools
Comparing version 0.0.11 to 0.0.12
(function() { | ||
var Datastore, MediaLibrary, Q, activeScan, escapeRegExp, indexPath, joinPath, _; | ||
var Datastore, MediaLibrary, Q, basename, dirname, escapeRegExp, fs, getPathRegex, indexPath, joinPath, mapPathToFolder, mapTrackToFile, normalizePath, pathSep, _; | ||
fs = require('fs'); | ||
joinPath = require('path').join; | ||
basename = require('path').basename; | ||
dirname = require('path').dirname; | ||
pathSep = require('path').sep; | ||
Datastore = require('nedb'); | ||
@@ -20,88 +28,144 @@ | ||
activeScan = null; | ||
mapPathToFolder = function(path) { | ||
return { | ||
path: path, | ||
name: basename(path), | ||
type: 'folder' | ||
}; | ||
}; | ||
mapTrackToFile = function(track) { | ||
return { | ||
path: track.path, | ||
name: basename(track.path), | ||
type: 'file', | ||
track: track | ||
}; | ||
}; | ||
normalizePath = function(path) { | ||
path = path.replace(/(?:\\|\/)/g, pathSep); | ||
if (path.slice(0, 2) === ('.' + pathSep)) { | ||
path = path.slice(2); | ||
} | ||
if (path.length > 1 && path[0] === pathSep && path[1] !== pathSep) { | ||
path = path.slice(1); | ||
} | ||
if (path.length > 1 && path.slice(-1) === pathSep) { | ||
path = path.slice(0, -1); | ||
} | ||
return path; | ||
}; | ||
getPathRegex = function(path) { | ||
return new RegExp(['^', escapeRegExp(path + pathSep), '[^', escapeRegExp(pathSep), ']+', '$'].join("")); | ||
}; | ||
MediaLibrary = (function() { | ||
function MediaLibrary(opts) { | ||
var filename; | ||
var trackdbpath; | ||
this.opts = opts; | ||
if (this.opts.databasePath) { | ||
filename = joinPath(this.opts.databasePath, 'ml-tracks.db'); | ||
trackdbpath = joinPath(this.opts.databasePath, 'ml-tracks.db'); | ||
} | ||
this.db = new Datastore({ | ||
filename: filename, | ||
autoload: true | ||
}); | ||
this.dbfind = Q.nbind(this.db.find, this.db); | ||
this.db = { | ||
track: new Datastore({ | ||
filename: trackdbpath, | ||
autoload: true | ||
}) | ||
}; | ||
this.dbfind = { | ||
track: Q.nbind(this.db.track.find, this.db.track) | ||
}; | ||
this.opts.paths = this.opts.paths.map(normalizePath); | ||
} | ||
MediaLibrary.prototype.addTrack = function(root, path, metadata) { | ||
var db, deferred; | ||
db = this.db; | ||
deferred = Q.defer(); | ||
db.findOne({ | ||
path: path | ||
}, function(err, result) { | ||
if (result) { | ||
deferred.resolve(result); | ||
return; | ||
} | ||
metadata.picture = void 0; | ||
metadata.root = root; | ||
metadata.path = path; | ||
return db.insert(metadata, function(err, metadata) { | ||
if (err) { | ||
deferred.reject(new Error(err)); | ||
return; | ||
} | ||
return deferred.resolve(metadata); | ||
}); | ||
}); | ||
return deferred.promise; | ||
return Q.Promise((function(_this) { | ||
return function(resolve, reject) { | ||
var db; | ||
db = _this.db.track; | ||
return db.findOne({ | ||
path: path | ||
}, function(err, result) { | ||
if (result) { | ||
return resolve(result); | ||
} | ||
metadata.picture = void 0; | ||
metadata.root = root; | ||
metadata.path = path; | ||
return db.insert(metadata, function(err, metadata) { | ||
if (err) { | ||
return reject(new Error(err)); | ||
} | ||
return resolve(metadata); | ||
}); | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
MediaLibrary.prototype.scan = function() { | ||
var addPromise, addPromises, deferred, pathDonePromise, promises, root, trackCount; | ||
if (activeScan) { | ||
return activeScan; | ||
if (this._activeScan) { | ||
return this._activeScan; | ||
} | ||
deferred = Q.defer(); | ||
trackCount = 0; | ||
promises = (function() { | ||
var _i, _len, _ref, _results; | ||
_ref = this.opts.paths; | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
root = _ref[_i]; | ||
addPromises = []; | ||
addPromise = null; | ||
pathDonePromise = indexPath(root, (function(_this) { | ||
return function(path, metadata) { | ||
addPromise = _this.addTrack(root, path, metadata); | ||
addPromise.then(function(track) { | ||
return deferred.notify(track); | ||
}); | ||
addPromises.push(addPromise); | ||
return trackCount++; | ||
}; | ||
})(this)); | ||
_results.push(pathDonePromise.then(function() { | ||
return Q.all(addPromises); | ||
})); | ||
} | ||
return _results; | ||
}).call(this); | ||
Q.all(promises).then(deferred.resolve); | ||
activeScan = deferred.promise.then(function() { | ||
activeScan = null; | ||
return trackCount; | ||
}); | ||
return activeScan; | ||
return this._activeScan = Q.Promise((function(_this) { | ||
return function(resolve, reject, notify) { | ||
var addPromise, addPromises, pathDonePromise, pathPromises, root, trackCount; | ||
trackCount = 0; | ||
pathPromises = (function() { | ||
var _i, _len, _ref, _results; | ||
_ref = this.opts.paths; | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
root = _ref[_i]; | ||
addPromises = []; | ||
addPromise = null; | ||
pathDonePromise = indexPath(root, (function(_this) { | ||
return function(path, metadata) { | ||
addPromise = _this.addTrack(root, path, metadata); | ||
addPromise.then(function(track) { | ||
return notify(track); | ||
}); | ||
addPromises.push(addPromise); | ||
return trackCount++; | ||
}; | ||
})(this)); | ||
_results.push(pathDonePromise.then(function() { | ||
return Q.all(addPromises); | ||
})); | ||
} | ||
return _results; | ||
}).call(_this); | ||
return Q.all(pathPromises).then(function() { | ||
_this._activeScan = null; | ||
return resolve(trackCount); | ||
})["catch"](reject); | ||
}; | ||
})(this)); | ||
}; | ||
MediaLibrary.prototype.tracks = function() { | ||
return this.dbfind({}); | ||
MediaLibrary.prototype.tracks = function(query) { | ||
var q; | ||
if (query == null) { | ||
query = {}; | ||
} | ||
q = _.clone(query); | ||
if (q.artist) { | ||
q.artist = [query.artist]; | ||
} | ||
return this.dbfind.track(q); | ||
}; | ||
MediaLibrary.prototype.artists = function() { | ||
return this.dbfind({}).then(function(tracks) { | ||
MediaLibrary.prototype.artists = function(query) { | ||
var q; | ||
if (query == null) { | ||
query = {}; | ||
} | ||
q = _.clone(query); | ||
if (q.name) { | ||
q.artist = [query.name]; | ||
delete q.name; | ||
} | ||
return this.dbfind.track({}).then(function(tracks) { | ||
return tracks.filter(function(t) { | ||
@@ -113,8 +177,65 @@ return t.artist && t.artist.length; | ||
}).then(_.uniq).then(function(artists) { | ||
return artists.sort(); | ||
return artists.sort().map(function(a) { | ||
return { | ||
name: a | ||
}; | ||
}); | ||
}); | ||
}; | ||
MediaLibrary.prototype.find = function(track) { | ||
var artistRegex, query, titleRegex; | ||
MediaLibrary.prototype.albums = function(query) { | ||
var q; | ||
if (query == null) { | ||
query = {}; | ||
} | ||
q = _.clone(query); | ||
if (q.title) { | ||
q.album = q.title; | ||
delete q.title; | ||
} | ||
if (q.artist) { | ||
q.artist = [query.artist]; | ||
} | ||
return this.dbfind.track(q).then(function(tracks) { | ||
return tracks.filter(function(t) { | ||
return !!t.album; | ||
}).map(function(t) { | ||
var _ref, _ref1; | ||
return { | ||
title: t.album, | ||
artist: (_ref = t.artist) != null ? _ref[0] : void 0, | ||
id: [(_ref1 = t.artist) != null ? _ref1[0] : void 0, t.album].join('|||') | ||
}; | ||
}); | ||
}).then(function(albums) { | ||
return _.uniq(albums, function(a) { | ||
return a.id; | ||
}); | ||
}).then(function(albums) { | ||
return _.sortBy(albums, 'id'); | ||
}); | ||
}; | ||
MediaLibrary.prototype.files = function(path) { | ||
var folders, names; | ||
if (path == null) { | ||
return Q(this.opts.paths.map(mapPathToFolder)); | ||
} else { | ||
path = normalizePath(path); | ||
names = fs.readdirSync(path); | ||
folders = names.map(function(name) { | ||
return joinPath(path, name); | ||
}).filter(function(path) { | ||
return fs.statSync(path).isDirectory(); | ||
}).map(mapPathToFolder); | ||
return this.dbfind.track({ | ||
path: getPathRegex(path) | ||
}).then(function(tracks) { | ||
return folders.concat(tracks.map(mapTrackToFile)); | ||
}); | ||
} | ||
}; | ||
MediaLibrary.prototype.findTracks = function(track) { | ||
var albumRegex, artistRegex, query, titleRegex; | ||
query = {}; | ||
@@ -129,3 +250,7 @@ if (track.artist) { | ||
} | ||
return this.dbfind(query); | ||
if (track.album) { | ||
albumRegex = new RegExp([escapeRegExp(track.album)].join(""), "i"); | ||
query.album = albumRegex; | ||
} | ||
return this.dbfind.track(query); | ||
}; | ||
@@ -132,0 +257,0 @@ |
{ | ||
"name": "media-library", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "a media library (indexer and search/browse api)", | ||
@@ -32,3 +32,2 @@ "main": "./lib/medialibrary", | ||
"gulp-mocha": "^1.0.0", | ||
"gulp-watch": "^0.6.9", | ||
"should": "^4.0.4" | ||
@@ -35,0 +34,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37978
6
304
2