Simple image host.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

context.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, env) {
  6. env.url = ctx.req.url;
  7. str = preprocess(str, {
  8. session: ctx.session,
  9. arg: args,
  10. env: env,
  11. template: function(key) {
  12. return ctx.template(key);
  13. }
  14. });
  15. return str;
  16. }
  17. module.exports = function(options) {
  18. this.req = options.req;
  19. this.res = options.res;
  20. this.templates = options.templates;
  21. this.views = options.views;
  22. this.db = options.db;
  23. this.conf = options.conf;
  24. this.query = this.req.url.split("?")[1] || "";
  25. if (this.conf.debug)
  26. this.startTime = new Date();
  27. //Handle cookies
  28. this.cookies = {};
  29. this.req.headers.cookie = this.req.headers.cookie || "";
  30. this.req.headers.cookie.split(/;\s*/).forEach(function(elem) {
  31. var pair = elem.split("=");
  32. this.cookies[pair[0]] = decodeURIComponent(pair[1]);
  33. }.bind(this));
  34. //Handle sessions
  35. if (sessions[this.cookies.session]) {
  36. this.session = sessions[this.cookies.session];
  37. } else {
  38. var key;
  39. do {
  40. key = crypto.randomBytes(64).toString("hex");
  41. } while (sessions[key]);
  42. sessions[key] = {};
  43. this.res.setHeader("Set-Cookie", "session="+key);
  44. //Delete session after a while
  45. setTimeout(function() {
  46. delete sessions[key];
  47. }, this.conf.session_timeout);
  48. this.session = sessions[key];
  49. }
  50. }
  51. module.exports.prototype = {
  52. end: function(str) {
  53. if (this.conf.debug) {
  54. var ms = (new Date().getTime() - this.startTime.getTime());
  55. console.log(
  56. ms+" millisecond(s)\t"+
  57. (this.statusCode || 200)+"\t"+
  58. this.req.url
  59. );
  60. } else {
  61. conole.log(this.req.url);
  62. }
  63. if (this.statusCode)
  64. this.res.writeHead(this.statusCode);
  65. this.res.end(str);
  66. },
  67. succeed: function(obj) {
  68. obj = obj || {};
  69. obj.success = true;
  70. this.res.setHeader("Content-Type", "application/json");
  71. this.end(JSON.stringify(obj));
  72. },
  73. fail: function(err) {
  74. console.log("Sending error to client:");
  75. console.trace(err);
  76. this.res.setHeader("Content-Type", "application/json");
  77. obj = {};
  78. obj.success = false;
  79. obj.error = err.toString();
  80. this.end(JSON.stringify(obj));
  81. },
  82. template: function(name, args) {
  83. var str = this.templates[name];
  84. if (!str)
  85. throw new Error("No such template: "+name);
  86. return templatify(str, args, this, {template: name});
  87. },
  88. view: function(name, args) {
  89. var str = this.views[name];
  90. if (!str)
  91. throw new Error("No such view: "+name);
  92. return templatify(str, args, this, {view: name});
  93. },
  94. getPostData: function(cb) {
  95. if (this.postData)
  96. return cb(null, this.postData.data, this.postData.files);
  97. if (this.req.method.toUpperCase() != "POST")
  98. return cb(new Error("Expected POST request, got "+this.req.method));
  99. var form = new formidable.IncomingForm();
  100. form.parse(this.req, function(err, data, files) {
  101. if (err) return cb(err);
  102. this.postData = {
  103. data: data,
  104. files: files
  105. }
  106. cb(null, data, files);
  107. }.bind(this));
  108. },
  109. setStatus: function(code) {
  110. this.statusCode = code;
  111. },
  112. login: function(username, id) {
  113. this.session.loggedIn = true;
  114. this.session.username = username;
  115. this.session.userId = id;
  116. },
  117. logout: function() {
  118. this.session.loggedIn = false;
  119. delete this.session.username;
  120. delete this.session.userId;
  121. },
  122. async: function(n, cb) {
  123. if (typeof n !== "number")
  124. throw new Error("Expected number, got "+typeof n);
  125. if (n < 1)
  126. return cb();
  127. var res = {};
  128. var errs = {};
  129. var errnum = 0;
  130. return function(key, val, err) {
  131. if (key)
  132. res[key] = val;
  133. if (err)
  134. errs[key] = err;
  135. if (n === 1)
  136. cb((errnum ? errs : null), res);
  137. else
  138. n -= 1;
  139. }
  140. }
  141. }