Simple image host.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

context.js 4.1KB

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