Simple image host.
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.

context.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var formidable = require("formidable");
  2. var crypto = require("crypto");
  3. var preprocess = require("./preprocess.js");
  4. var sessions = {};
  5. function templatify(str, args, ctx) {
  6. str = preprocess(str, {
  7. session: ctx.session,
  8. template: function(key) {
  9. return ctx.template(key);
  10. }
  11. });
  12. if (args == undefined)
  13. return str;
  14. for (var i in args) {
  15. str = str.split("{{"+i+"}}").join(args[i]);
  16. }
  17. return str;
  18. }
  19. module.exports = function(options) {
  20. this.req = options.req;
  21. this.res = options.res;
  22. this.templates = options.templates;
  23. this.views = options.views;
  24. this.db = options.db;
  25. this.conf = options.conf;
  26. //Handle cookies
  27. this.cookies = {};
  28. this.req.headers.cookie = this.req.headers.cookie || "";
  29. this.req.headers.cookie.split(/;\s*/).forEach(function(elem) {
  30. var pair = elem.split("=");
  31. this.cookies[pair[0]] = decodeURIComponent(pair[1]);
  32. }.bind(this));
  33. //Handle sessions
  34. if (sessions[this.cookies.session]) {
  35. this.session = sessions[this.cookies.session];
  36. } else {
  37. var key;
  38. do {
  39. key = crypto.randomBytes(64).toString("hex");
  40. } while (sessions[key]);
  41. sessions[key] = {};
  42. this.res.setHeader("Set-Cookie", "session="+key);
  43. //Delete session after a while
  44. setTimeout(function() {
  45. delete sessions[key];
  46. }, this.conf.session_timeout);
  47. this.session = sessions[key];
  48. }
  49. }
  50. module.exports.prototype = {
  51. end: function(str) {
  52. if (this.statusCode)
  53. this.res.writeHead(this.statusCode);
  54. this.res.end(str);
  55. },
  56. succeed: function(obj) {
  57. obj = obj || {};
  58. obj.success = true;
  59. this.res.setHeader("Content-Type", "application/json");
  60. this.end(JSON.stringify(obj));
  61. },
  62. fail: function(err) {
  63. console.log("Sending error to client:");
  64. console.trace(err);
  65. this.res.setHeader("Content-Type", "application/json");
  66. obj = {};
  67. obj.success = false;
  68. obj.error = err.toString();
  69. this.end(JSON.stringify(obj));
  70. },
  71. template: function(name, args) {
  72. var str = this.templates[name];
  73. if (!str)
  74. throw new Error("No such template: "+name);
  75. return templatify(str, args, this);
  76. },
  77. view: function(name, args) {
  78. var str = this.views[name];
  79. if (!str)
  80. throw new Error("No such view: "+name);
  81. return templatify(str, args, this);
  82. },
  83. getPostData: function(cb) {
  84. if (this.postData)
  85. return cb(null, this.postData.data, this.postData.files);
  86. if (this.req.method.toUpperCase() != "POST")
  87. return cb(new Error("Expected POST request, got "+this.req.method));
  88. var form = new formidable.IncomingForm();
  89. form.parse(this.req, function(err, data, files) {
  90. if (err) return cb(err);
  91. this.postData = {
  92. data: data,
  93. files: files
  94. }
  95. cb(null, data, files);
  96. }.bind(this));
  97. },
  98. setStatus: function(code) {
  99. this.statusCode = code;
  100. }
  101. }