export.js 4.1 KB

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