1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // Dependencies
- var express = require("express"),
- compression = require("compression"),
- path = require("path"),
- multer = require("multer"),
- uuid = require("uuid"),
- mkdirp = require("mkdirp"),
- bodyParser = require('body-parser');
-
- // Routes and middleware
- var logger = require("../lib/logger/"),
- render = require("./render.js"),
- status = require("./status.js"),
- fonts = require("./fonts.js"),
- themesAPI = require("./themesAPI.js"),
- imageAPI = require("./imageAPI.js"),
- errorHandlers = require("./error.js");
-
- // Settings
- var serverSettings = require("../lib/settings/");
-
- var app = express();
-
- app.use(compression());
- app.use(logger.morgan());
-
- // Options for where to store uploaded audio and max size
- var fileOptions = {
- storage: multer.diskStorage({
- destination: function(req, file, cb) {
-
- var dir = path.join(serverSettings.workingDirectory, uuid.v1());
-
- mkdirp(dir, function(err) {
- return cb(err, dir);
- });
- },
- filename: function(req, file, cb) {
- cb(null, "audio");
- }
- })
- };
-
- if (serverSettings.maxUploadSize) {
- fileOptions.limits = {
- fileSize: +serverSettings.maxUploadSize
- };
- }
-
- // On submission, check upload, validate input, and start generating a video
- app.post("/submit/", [multer(fileOptions).single("audio"), render.validate, render.route]);
-
- // If not using S3, serve videos locally
- if (!serverSettings.s3Bucket) {
- app.use("/video/", express.static(path.join(serverSettings.storagePath, "video")));
- }
-
- // Serve custom fonts
- app.get("/fonts/fonts.css", fonts.css);
- app.get("/fonts/fonts.js", fonts.js);
-
- if (serverSettings.fonts) {
- app.get("/fonts/:font", fonts.font);
- }
-
- // Check the status of a current video
- app.get("/status/:id/", status);
-
-
- // Serve background images and themes JSON statically
- app.use("/settings/", function(req, res, next) {
-
- // Limit to themes.json and bg images
- if (req.url.match(/^\/?themes.json$/i) || req.url.match(/^\/?backgrounds\/[^/]+$/i)) {
- return next();
- }
-
- return res.status(404).send("Cannot GET " + path.join("/settings", req.url));
-
- }, express.static(path.join(__dirname, "..", "settings")));
-
- // Ovewrite themes file
- app.post("/api/themes", bodyParser.json(), themesAPI);
-
- // Get & Upload Images
- app.get("/api/images", imageAPI.get);
- app.post("/api/images", bodyParser.json(), imageAPI.post);
-
- // Serve editor files statically
- app.use(express.static(path.join(__dirname, "..", "editor")));
-
- app.use(errorHandlers);
-
- module.exports = app;
|