yusuketk 5 лет назад
Родитель
Сommit
020cb9dd84

+ 10 - 4
src/client/js/components/Page/PageShareManagement.jsx

@@ -28,18 +28,24 @@ const PageShareManagement = (props) => {
   }
 
   async function exportPageHundler(type) {
+    const { pageId, revisionId } = pageContainer.state;
     try {
-      const { pageId, revisionId } = pageContainer.state;
       const responseType = type === 'pdf' ? 'arraybuffer' : 'json';
       // const responseType = 'stream';
-      const markdown = await appContainer.apiv3Get('/page/export', { revisionId, type, responseType });
+      const data = await appContainer.apiv3Get('/page/export',
+        {
+          pageId,
+          revisionId,
+          type,
+          responseType,
+        });
       const blob = new Blob(
-        [markdown],
+        [data],
         { type: 'text/plain' },
       );
       const link = document.createElement('a');
       link.href = window.URL.createObjectURL(blob);
-      link.download = `${revisionId}.${type}`;
+      link.download = `${pageId}.${type}`;
       link.click();
     }
     catch (err) {

+ 3 - 5
src/server/routes/apiv3/page.js

@@ -199,7 +199,7 @@ module.exports = (crowi) => {
   */
   router.get('/export', validator.export, async(req, res) => {
     try {
-      const { pageId = null, revisionId = null } = req.query;
+      const { type, pageId = null, revisionId = null } = req.query;
 
       if (pageId == null) {
         return res.apiv3Err(new ErrorV3('Should provided pageId or both pageId and revisionId.'));
@@ -215,7 +215,6 @@ module.exports = (crowi) => {
         return res.apiv3Err(new ErrorV3(`Haven't the right to see the page ${pageId}.`), 403);
       }
 
-
       let revisionIdForFind;
       if (revisionId == null) {
         const Page = crowi.model('Page');
@@ -226,15 +225,14 @@ module.exports = (crowi) => {
         revisionIdForFind = revisionId;
       }
 
-      
       const Revision = crowi.model('Revision');
       const revision = await Revision.findById(revisionIdForFind);
 
       const markdown = revision.body;
-	  return await exportService.createExportStream(res, markdown, type);
+      return await exportService.createExportStream(res, revisionId, markdown, type);
     }
     catch (err) {
-      logger.error('Failed to get markdown', err);
+      logger.error('Failed to get page', err);
       return res.apiv3Err(err, 500);
     }
   });

+ 5 - 5
src/server/service/export.js

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