combine.fm/routes/search.js

41 lines
1 KiB
JavaScript
Raw Normal View History

"use strict";
var parse = require('url').parse;
var path = require('path');
var Q = require('q');
var services = {};
require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach(function(file) {
var service = require("../lib/services/" + file);
if (service.search) {
services[service.id] = service;
}
});
module.exports = function(req, res) {
var url = parse(req.body.url);
if (!url.host) {
2014-12-05 18:18:39 +00:00
req.flash('search-error', 'Paste a music link above to find and share the matches');
res.redirect('/');
return;
}
for (var id in services) {
var matched = services[id].match(req.body.url);
if (matched) {
services[id].parseUrl(req.body.url).then(function(result) {
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/" + id + "/" + result.type + "/" + result.id);
})
return;
}
}
req.flash('search-error', 'No match found for this link');
res.redirect('/');
};