123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- var serverSettings = require("../lib/settings/"),
- spawn = require("child_process").spawn,
- path = require("path"),
- _ = require("underscore"),
- logger = require("../lib/logger"),
- transports = require("../lib/transports");
-
- function validate(req, res, next) {
-
- try {
-
- req.body.theme = JSON.parse(req.body.theme);
-
- } catch(e) {
-
- return res.status(500).send("Unknown settings error.");
-
- }
-
- var file;
-
- if (req.files) {
- if (req.files['audio']) {
- file = req.files['audio'][0];
- }
- } else {
- if (req.file) {
- file = req.file;
- }
- }
-
- if (!file) {
- return res.status(500).send("No valid audio received.");
- }
-
- // Start at the beginning, or specified time
- if (req.body.start) {
- req.body.start = +req.body.start;
- }
-
- if (req.body.end) {
- req.body.end = +req.body.end;
- }
-
- return next();
-
- }
-
- function route(req, res) {
-
- var file;
-
- if (req.files) {
- if (req.files['audio']) {
- file = req.files['audio'][0];
- }
- } else {
- if (req.file) {
- file = req.file;
- }
- }
-
- if (!file) {
- return res.status(500).send("No valid audio received.");
- }
-
- var id = file.destination.split(path.sep).pop();
-
- transports.uploadAudio(path.join(file.destination, "audio"), "audio/" + id,function(err) {
-
- if (err) {
- throw err;
- }
-
- // Queue up the job with a timestamp
- transports.addJob(_.extend({ id: id, created: (new Date()).getTime() }, req.body));
-
- res.json({ id: id });
-
- // If there's no separate worker, spawn one right away
- if (!serverSettings.worker) {
-
- logger.debug("Spawning worker");
-
- // Empty args to avoid child_process Linux error
- spawn("bin/worker", [], {
- stdio: "inherit",
- cwd: path.join(__dirname, ".."),
- env: _.extend({}, process.env, { SPAWNED: true })
- });
-
- }
-
- });
-
- };
-
- module.exports = {
- validate: validate,
- route: route
- };
|