A simple file sharing site with an easy to use API and online panel.
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.

91 lines
2.6KB

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