capes.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var helpers = require("../helpers");
  2. var cache = require("../cache");
  3. // GET cape request
  4. module.exports = function(req, callback) {
  5. var userId = (req.url.path_list[1] || "").split(".")[0];
  6. var def = req.url.searchParams.get('default');
  7. var rid = req.id;
  8. // check for extra paths
  9. if (req.url.path_list.length > 2) {
  10. callback({
  11. status: -2,
  12. body: "Invalid Path",
  13. code: 404
  14. });
  15. return;
  16. }
  17. // strip dashes
  18. userId = userId.replace(/-/g, "");
  19. if (!helpers.id_valid(userId)) {
  20. callback({
  21. status: -2,
  22. body: "Invalid UUID"
  23. });
  24. return;
  25. }
  26. try {
  27. helpers.get_cape(rid, userId, function(err, hash, status, image) {
  28. if (err) {
  29. if (err.code === "ENOENT") {
  30. // no such file
  31. cache.remove_hash(rid, userId);
  32. }
  33. }
  34. callback({
  35. status: status,
  36. body: image,
  37. type: image ? "image/png" : undefined,
  38. redirect: image ? undefined : def,
  39. hash: hash,
  40. err: err
  41. });
  42. });
  43. } catch(e) {
  44. callback({
  45. status: -1,
  46. err: e
  47. });
  48. }
  49. };