Pictures!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var querystring = require("querystring");
  2. var fs = require("fs");
  3. var pathlib = require("path");
  4. var basepath = "/admin/api/";
  5. // Used in every method handler to make sure the correct arguments are provided
  6. function hasargs(query, respond, expected) {
  7. var missing = [];
  8. for (var e of expected) {
  9. if (query[e] === undefined)
  10. missing.push(e);
  11. }
  12. if (missing.length > 0) {
  13. respond("Missing arguments: "+missing.join(", "));
  14. return false;
  15. } else {
  16. return true;
  17. }
  18. }
  19. // Method handlers are defined here
  20. var methods = {
  21. // List all slides in the slides directory
  22. list_slides: function(query, conf, req, respond) {
  23. fs.readdir(conf.slides, (err, files) => {
  24. if (err)
  25. return respond(err);
  26. respond(null, files);
  27. });
  28. },
  29. // Get metadata about a slide
  30. slide_meta: function(query, conf, req, respond) {
  31. if (!hasargs(query, respond, [ "slide" ])) return;
  32. var path = pathlib.join(conf.slides, query.slide);
  33. fs.stat(path, (err, stat) => {
  34. if (err || !stat.isDirectory())
  35. return respond(path+" is not a slide.");
  36. fs.readFile(pathlib.join(path, "meta.json"), (err, res) => {
  37. if (err && err.code === "ENOENT")
  38. return respond(null, {});
  39. else if (err)
  40. return respond(err);
  41. try {
  42. respond(null, JSON.parse(res));
  43. } catch (err) {
  44. respond(err);
  45. }
  46. });
  47. });
  48. },
  49. // Get a list of files of a slide
  50. slide_file_list: function(query, conf, req, respond) {
  51. if (!hasargs(query, respond, [ "slide" ])) return;
  52. var dir = pathlib.join(conf.slides, query.slide);
  53. fs.readdir(dir, (err, files) => {
  54. if (err)
  55. return respond(err);
  56. respond(null, { files: files });
  57. });
  58. },
  59. // Get a slide's HTML
  60. slide_html: function(query, conf, req, respond) {
  61. if (!hasargs(query, respond, [ "slide" ])) return;
  62. var path = pathlib.join(conf.slides, query.slide, "index.html");
  63. fs.readFile(path, "utf-8", (err, text) => {
  64. if (err && err.code === "ENOENT")
  65. return respond(null, { text: "" });
  66. else if (err)
  67. return respond(err);
  68. respond(null, { text: text });
  69. });
  70. },
  71. // Update a slide's HTML
  72. slide_html_update: function(query, conf, req, respond) {
  73. if (!hasargs(query, respond, [ "slide", "text" ])) return;
  74. var path = pathlib.join(conf.slides, query.slide, "index.html");
  75. fs.writeFile(path, query.text, err => {
  76. if (err)
  77. respond(err);
  78. else
  79. respond();
  80. });
  81. },
  82. }
  83. exports.canServe = function(parts) {
  84. // Temporary, while working on stuff
  85. return false;
  86. return methods[parts.pathname.replace(basepath, "")] !== undefined;
  87. }
  88. exports.serve = function(parts, conf, req, res) {
  89. var fn = methods[parts.pathname.replace(basepath, "")];
  90. if (!fn) {
  91. res.writeHead(404);
  92. res.end();
  93. return;
  94. }
  95. var query = querystring.parse(parts.query);
  96. for (var i in query) {
  97. query[i] = decodeURIComponent(query[i]);
  98. }
  99. // Better than manually doing res.end(JSON.stringify(obj)) everywhere
  100. function respond(err, obj) {
  101. var result = {
  102. obj: obj,
  103. err: err ? err.toString() : null
  104. };
  105. if (err)
  106. res.writeHead(400);
  107. else
  108. res.writeHead(200);
  109. res.end(JSON.stringify(result));
  110. }
  111. // Finally, call method handler
  112. fn(query, conf, req, respond);
  113. }