Просмотр исходного кода

create tmp file in server and download in client side

yusuketk 5 лет назад
Родитель
Сommit
396f724691

+ 13 - 13
src/client/js/components/Page/PageShareManagement.jsx

@@ -27,26 +27,26 @@ const PageShareManagement = (props) => {
     setIsOutsideShareLinkModalShown(false);
   }
 
-  async function getMarkdown() {
-    const { revisionId } = pageContainer.state;
+  async function exportPageHundler(type) {
     try {
-      const res = await appContainer.apiv3Get('/page/export?format=md', { revisionId });
-      return res.data.markdown;
+      const { revisionId } = pageContainer.state;
+      const responseType = type === 'pdf' ? 'arraybuffer' : 'json';
+      // const responseType = 'stream';
+      const markdown = await appContainer.apiv3Get('/page/export', { revisionId, type, responseType });
+      const blob = new Blob(
+        [markdown],
+        { type: 'text/plain' },
+      );
+      const link = document.createElement('a');
+      link.href = window.URL.createObjectURL(blob);
+      link.download = `${revisionId}.${type}`;
+      link.click();
     }
     catch (err) {
       toastError(Error(t('export_bulk.failed_to_export')));
     }
   }
 
-  async function exportPage(markdown, type) {
-    // TODO: GW-3063
-  }
-
-  async function exportPageHundler(type) {
-    const markdown = await getMarkdown();
-    await exportPage(markdown, type);
-  }
-
   function renderModals() {
     return (
       <>

+ 2 - 28
src/server/routes/apiv3/page.js

@@ -7,10 +7,6 @@ const { body } = require('express-validator');
 
 const router = express.Router();
 
-const path = require('path');
-const fs = require('fs');
-const ApiResponse = require('../../util/apiResponse');
-
 // const ErrorV3 = require('../../models/vo/error-apiv3');
 
 /**
@@ -199,7 +195,7 @@ module.exports = (crowi) => {
   */
   router.get('/export', async(req, res) => {
     try {
-      const { format, pageId = null, revisionId = null } = req.query;
+      const { type, pageId = null, revisionId = null } = req.query;
       let markdown;
 
       // TODO: GW-3061
@@ -213,29 +209,7 @@ module.exports = (crowi) => {
         return res.apiv3Err('Should provided pageId or revisionId');
       }
 
-      let fileStream;
-      let filePath;
-      const baseDir = path.join(crowi.tmpDir, 'exports');
-
-      try {
-        // write tmp file
-        if (format === 'md') {
-          filePath = path.join(baseDir, 'revisionId.md');
-          await fs.writeFileSync(filePath, markdown);
-        }
-        else if (format === 'pdf') {
-          filePath = path.join(baseDir, 'revisionId.pdf');
-          await exportService.convertToPdf(markdown, filePath);
-        }
-
-        fileStream = fs.createReadStream(filePath);
-      }
-      catch (e) {
-        logger.error(e);
-        return res.json(ApiResponse.error(e.message));
-      }
-
-      return fileStream.pipe(res);
+      return await exportService.createExportStream(res, markdown, type);
     }
     catch (err) {
       logger.error('Failed to get markdown', err);

+ 28 - 0
src/server/service/export.js

@@ -351,6 +351,34 @@ class ExportService {
     return zipFile;
   }
 
+  async createExportStream(res, markdown, type) {
+    let fileStream;
+    let filePath;
+    const baseDir = path.join(this.crowi.tmpDir, 'exports');
+
+    try {
+      // create tmp file
+      if (type === 'md') {
+        filePath = path.join(baseDir, 'revisionId.md');
+        await fs.writeFileSync(filePath, markdown);
+      }
+      else if (type === 'pdf') {
+        filePath = path.join(baseDir, 'revisionId.pdf');
+        await this.convertToPdf(markdown, filePath);
+      }
+      else {
+        throw new Error('requested file format is invaild');
+      }
+      fileStream = fs.createReadStream(filePath);
+    }
+    catch (e) {
+      logger.error(e);
+      return new Error(e);
+    }
+
+    return fileStream.pipe(res);
+  }
+
   async convertToPdf(md, path) {
     return new Promise((resolve, reject) => {
       markdownpdf().from.string(md).to(path, () => {