123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- var querystring = require("querystring");
- var fs = require("fs");
- var pathlib = require("path");
-
- var basepath = "/admin/api/";
-
- // Used in every method handler to make sure the correct arguments are provided
- function hasargs(query, respond, expected) {
- var missing = [];
- for (var e of expected) {
- if (query[e] === undefined)
- missing.push(e);
- }
-
- if (missing.length > 0) {
- respond("Missing arguments: "+missing.join(", "));
- return false;
- } else {
- return true;
- }
- }
-
- // Method handlers are defined here
- var methods = {
-
- // List all slides in the slides directory
- list_slides: function(query, conf, req, respond) {
- fs.readdir(conf.slides, (err, files) => {
- if (err)
- return respond(err);
-
- respond(null, files);
- });
- },
-
- // Get metadata about a slide
- slide_meta: function(query, conf, req, respond) {
- if (!hasargs(query, respond, [ "slide" ])) return;
-
- var path = pathlib.join(conf.slides, query.slide);
- fs.stat(path, (err, stat) => {
- if (err || !stat.isDirectory())
- return respond(path+" is not a slide.");
-
- fs.readFile(pathlib.join(path, "meta.json"), (err, res) => {
- if (err && err.code === "ENOENT")
- return respond(null, {});
- else if (err)
- return respond(err);
-
- try {
- respond(null, JSON.parse(res));
- } catch (err) {
- respond(err);
- }
- });
- });
- },
-
- // Get a list of files of a slide
- slide_file_list: function(query, conf, req, respond) {
- if (!hasargs(query, respond, [ "slide" ])) return;
-
- var dir = pathlib.join(conf.slides, query.slide);
- fs.readdir(dir, (err, files) => {
- if (err)
- return respond(err);
-
- respond(null, { files: files });
- });
- },
-
- // Get a slide's HTML
- slide_html: function(query, conf, req, respond) {
- if (!hasargs(query, respond, [ "slide" ])) return;
-
- var path = pathlib.join(conf.slides, query.slide, "index.html");
- fs.readFile(path, "utf-8", (err, text) => {
- if (err && err.code === "ENOENT")
- return respond(null, { text: "" });
- else if (err)
- return respond(err);
-
- respond(null, { text: text });
- });
- },
-
- // Update a slide's HTML
- slide_html_update: function(query, conf, req, respond) {
- if (!hasargs(query, respond, [ "slide", "text" ])) return;
-
- var path = pathlib.join(conf.slides, query.slide, "index.html");
- fs.writeFile(path, query.text, err => {
- if (err)
- respond(err);
- else
- respond();
- });
- },
- }
-
- exports.canServe = function(parts) {
- // Temporary, while working on stuff
- return false;
- return methods[parts.pathname.replace(basepath, "")] !== undefined;
- }
-
- exports.serve = function(parts, conf, req, res) {
- var fn = methods[parts.pathname.replace(basepath, "")];
- if (!fn) {
- res.writeHead(404);
- res.end();
- return;
- }
-
- var query = querystring.parse(parts.query);
- for (var i in query) {
- query[i] = decodeURIComponent(query[i]);
- }
-
- // Better than manually doing res.end(JSON.stringify(obj)) everywhere
- function respond(err, obj) {
- var result = {
- obj: obj,
- err: err ? err.toString() : null
- };
-
- if (err)
- res.writeHead(400);
- else
- res.writeHead(200);
-
- res.end(JSON.stringify(result));
- }
-
- // Finally, call method handler
- fn(query, conf, req, respond);
- }
|