fix upload for multiple files

also when auto queuing, sort by position in library
This commit is contained in:
Andrew Kelley 2014-03-24 17:12:54 -07:00
parent 3be4ad9cb6
commit 11068623b5
5 changed files with 65 additions and 33 deletions

View file

@ -23,8 +23,12 @@ DedupedQueue.prototype.idInQueue = function(id) {
};
DedupedQueue.prototype.add = function(id, item, cb) {
if (this.pendingSet[id]) return;
var queueItem = new QueueItem(id, item);
var queueItem = this.pendingSet[id];
if (queueItem) {
if (cb) queueItem.cbs.push(cb);
return;
}
queueItem = new QueueItem(id, item);
if (cb) queueItem.cbs.push(cb);
this.pendingSet[id] = queueItem;
this.pendingQueue.push(queueItem);

View file

@ -21,7 +21,7 @@ var levelup = require('level');
var crypto = require('crypto');
var net = require('net');
var safePath = require('./safe_path');
var multipart = require('connect-multiparty')();
var MultipartForm = require('multiparty').Form;
var createGzipStatic = require('connect-static');
module.exports = GrooveBasin;
@ -268,31 +268,39 @@ GrooveBasin.prototype.initializeDownload = function() {
GrooveBasin.prototype.initializeUpload = function() {
var self = this;
self.app.post('/upload', multipart, function(request, response) {
var keys = [];
var pend = new Pend();
for (var key in request.files) {
var file = request.files[key];
pend.go(makeImportFn(file));
}
pend.wait(function() {
response.json(keys);
});
self.app.post('/upload', function(request, response, next) {
var form = new MultipartForm();
form.parse(request, function(err, fields, files) {
if (err) return next(err);
function makeImportFn(file) {
return function(cb) {
self.player.importFile(file.path, file.originalFilename, function(err, dbFile) {
if (err) {
console.error("Unable to import file:", file.path, "error:", err.stack);
} else if (!dbFile) {
console.error("Unable to locate new file due to race condition");
} else {
keys.push(dbFile.key);
}
cb();
});
};
}
var keys = [];
var pend = new Pend();
for (var key in files) {
var arr = files[key];
for (var i = 0; i < arr.length; i += 1) {
var file = arr[i];
pend.go(makeImportFn(file));
}
}
pend.wait(function() {
response.json(keys);
});
function makeImportFn(file) {
return function(cb) {
self.player.importFile(file.path, file.originalFilename, function(err, dbFile) {
if (err) {
console.error("Unable to import file:", file.path, "error:", err.stack);
} else if (!dbFile) {
console.error("Unable to locate new file due to race condition");
} else {
keys.push(dbFile.key);
}
cb();
});
};
}
});
});
};

View file

@ -35,10 +35,10 @@
"music-library-index": "^1.1.0",
"keese": "~1.0.0",
"temp": "~0.7.0",
"connect-multiparty": "~1.0.3",
"ws": "~0.4.31",
"jsondiffpatch": "~0.1.4",
"connect-static": "^1.1.0"
"connect-static": "^1.1.0",
"multiparty": "^3.2.3"
},
"devDependencies": {
"handlebars": "1.0.7",

View file

@ -1619,7 +1619,6 @@ function uploadFiles(files) {
var file = files[i];
formData.append("file", file);
}
formData.append("autoQueue", localState.autoQueueUploads);
var $progressBar = $('<div></div>');
$progressBar.progressbar();
@ -1645,7 +1644,8 @@ function uploadFiles(files) {
function onLoad(e) {
if (localState.autoQueueUploads) {
var keys = JSON.parse(this.response);
player.queueTracks(keys);
// sort them the same way the library is sorted
player.queueTracks(player.sortKeys(keys));
}
cleanup();
}

View file

@ -111,7 +111,7 @@ PlayerClient.prototype.handleConnectionStart = function(){
PlayerClient.prototype.updateTrackStartDate = function() {
this.trackStartDate = (this.serverTrackStartDate != null) ?
new Date(new Date(this.serverTrackStartDate) - this.serverTimeOffset) : null;
}
};
PlayerClient.prototype.updateCurrentItem = function() {
this.currentItem = (this.currentItemId != null) ?
@ -134,7 +134,7 @@ PlayerClient.prototype.updatePlaylistIndex = function() {
}
this.refreshPlaylistList();
this.updateCurrentItem();
}
};
PlayerClient.prototype.search = function(query) {
query = query.trim();
@ -470,6 +470,26 @@ PlayerClient.prototype.refreshPlaylistList = function(){
}
};
// sort keys according to how they appear in the library
PlayerClient.prototype.sortKeys = function(keys) {
var realLib = this.library;
var lib = new MusicLibraryIndex();
keys.forEach(function(key) {
var track = realLib.trackTable[key];
if (track) lib.addTrack(track);
});
lib.rebuild();
var results = [];
lib.artistList.forEach(function(artist) {
artist.albumList.forEach(function(album) {
album.trackList.forEach(function(track) {
results.push(track.key);
});
});
});
return results;
};
PlayerClient.prototype.resetServerState = function(){
this.haveFileListCache = false;
this.library = new MusicLibraryIndex({