export.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:export'); // eslint-disable-line no-unused-vars
  3. const path = require('path');
  4. const express = require('express');
  5. const router = express.Router();
  6. /**
  7. * @swagger
  8. * tags:
  9. * name: Export
  10. */
  11. module.exports = (crowi) => {
  12. const { growiBridgeService, exportService } = crowi;
  13. /**
  14. * @swagger
  15. *
  16. * /export/status:
  17. * get:
  18. * tags: [Export]
  19. * description: get properties of zip files for export
  20. * produces:
  21. * - application/json
  22. * responses:
  23. * 200:
  24. * description: export cache status
  25. * content:
  26. * application/json:
  27. */
  28. router.get('/status', async(req, res) => {
  29. const zipFileStats = await exportService.getStatus();
  30. // TODO: use res.apiv3
  31. return res.json({ ok: true, zipFileStats });
  32. });
  33. /**
  34. * @swagger
  35. *
  36. * /export:
  37. * post:
  38. * tags: [Export]
  39. * description: generate a zipped json for multiple collections
  40. * produces:
  41. * - application/json
  42. * responses:
  43. * 200:
  44. * description: a zip file is generated
  45. * content:
  46. * application/json:
  47. */
  48. router.post('/', async(req, res) => {
  49. // TODO: add express validator
  50. try {
  51. const { collections } = req.body;
  52. // get model for collection
  53. const models = collections.map(collectionName => exportService.getModelFromCollectionName(collectionName));
  54. const [metaJson, jsonFiles] = await Promise.all([
  55. exportService.createMetaJson(),
  56. exportService.exportMultipleCollectionsToJsons(models),
  57. ]);
  58. // zip json
  59. const configs = jsonFiles.map((jsonFile) => { return { from: jsonFile, as: path.basename(jsonFile) } });
  60. // add meta.json in zip
  61. configs.push({ from: metaJson, as: path.basename(metaJson) });
  62. // exec zip
  63. const zipFile = await exportService.zipFiles(configs);
  64. // get stats for the zip file
  65. const zipFileStat = await growiBridgeService.parseZipFile(zipFile);
  66. // TODO: use res.apiv3
  67. return res.status(200).json({
  68. ok: true,
  69. zipFileStat,
  70. });
  71. }
  72. catch (err) {
  73. // TODO: use ApiV3Error
  74. logger.error(err);
  75. return res.status(500).send({ status: 'ERROR' });
  76. }
  77. });
  78. /**
  79. * @swagger
  80. *
  81. * /export:
  82. * delete:
  83. * tags: [Export]
  84. * description: unlink all json and zip files for exports
  85. * produces:
  86. * - application/json
  87. * parameters:
  88. * - name: fileName
  89. * in: path
  90. * description: file name of zip file
  91. * schema:
  92. * type: string
  93. * responses:
  94. * 200:
  95. * description: the json and zip file are deleted
  96. * content:
  97. * application/json:
  98. */
  99. router.delete('/:fileName', async(req, res) => {
  100. // TODO: add express validator
  101. const { fileName } = req.params;
  102. try {
  103. const zipFile = exportService.getFile(fileName);
  104. exportService.deleteZipFile(zipFile);
  105. // TODO: use res.apiv3
  106. return res.status(200).send({ ok: true });
  107. }
  108. catch (err) {
  109. // TODO: use ApiV3Error
  110. logger.error(err);
  111. return res.status(500).send({ ok: false });
  112. }
  113. });
  114. return router;
  115. };