A simple file sharing site with an easy to use API and online panel.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

88 lines
2.5KB

  1. const express = require('express');
  2. const router = express.Router();
  3. const ModelPath = '../../models/';
  4. const Upload = require(ModelPath + 'Upload.js');
  5. const View = require(ModelPath + 'View.js');
  6. const verifyBody = require('../../util/verifyBody');
  7. const authenticate = require('../../util/auth/authenticateRequest');
  8. const uploadProps = [
  9. {name: 'after', type: 'date', optional: true},
  10. {name: 'before', type: 'date', optional: true},
  11. {name: 'limit', type: 'number', min: 1, max: 10000, optional: true}
  12. ];
  13. router.get('/uploads', authenticate('stats.get'), verifyBody(uploadProps), async (req, res) => {
  14. let constraints = {uploader: req.username};
  15. // Set date constraints if specified
  16. if (req.body.after || req.body.before)
  17. constraints.date = {};
  18. if (req.body.after)
  19. constraints.date.$gt = new Date(req.body.after);
  20. if (req.body.before)
  21. constraints.date.$lt = new Date(req.body.before);
  22. // Create query
  23. const query = Upload.find(constraints);
  24. // Limit if specified
  25. if (req.body.limit)
  26. query.limit(req.body.limit);
  27. // Fetch and transform results
  28. let uploads = await query;
  29. uploads = uploads.map(upload => {
  30. return {
  31. date: upload.date,
  32. uid: upload.uid,
  33. key: upload.uploaderKey,
  34. originalName: upload.file.originalName,
  35. size: upload.file.size,
  36. mime: upload.file.mime
  37. }
  38. });
  39. res.status(200).json(uploads);
  40. });
  41. const viewProps = [
  42. {name: 'after', type: 'date', optional: true},
  43. {name: 'before', type: 'date', optional: true},
  44. {name: 'limit', type: 'number', min: 1, max: 10000, optional: true}
  45. ];
  46. router.get('/views', authenticate('stats.get'), verifyBody(viewProps), async (req, res) => {
  47. let constraints = {uploader: req.username};
  48. // Set date constraints if specified
  49. if (req.body.after || req.body.before)
  50. constraints.date = {};
  51. if (req.body.after)
  52. constraints.date.$gt = new Date(req.body.after);
  53. if (req.body.before)
  54. constraints.date.$lt = new Date(req.body.before);
  55. // Create query
  56. const query = View.find(constraints);
  57. // Limit if specified
  58. if (req.body.limit)
  59. query.limit(req.body.limit);
  60. // Fetch and transform results
  61. let views = await query;
  62. views = views.map(view => {
  63. return {
  64. date: view.date,
  65. uid: view.uid,
  66. }
  67. });
  68. res.status(200).json(views);
  69. });
  70. module.exports = router;