Yuki Takei 11 месяцев назад
Родитель
Сommit
a9a85cf887
1 измененных файлов с 15 добавлено и 5 удалено
  1. 15 5
      apps/app/src/server/routes/apiv3/page/index.ts

+ 15 - 5
apps/app/src/server/routes/apiv3/page/index.ts

@@ -2,7 +2,7 @@ import path from 'path';
 import { type Readable } from 'stream';
 import { pipeline } from 'stream/promises';
 
-import type { IPage } from '@growi/core';
+import type { IPage, IRevision } from '@growi/core';
 import {
   AllSubscriptionStatusType, PageGrant, SubscriptionStatusType,
   getIdForRef,
@@ -157,7 +157,7 @@ module.exports = (crowi) => {
     ],
     export: [
       query('format').isString().isIn(['md', 'pdf']),
-      query('revisionId').isString(),
+      query('revisionId').optional().isMongoId(),
     ],
     archive: [
       body('rootPagePath').isString(),
@@ -844,8 +844,10 @@ module.exports = (crowi) => {
   */
   router.get('/export/:pageId', loginRequiredStrictly, validator.export, async(req, res) => {
     const pageId: string = req.params.pageId;
-    const { format, revisionId = null } = req.query;
-    let revision;
+    const format: 'md' | 'pdf' = req.params.format ?? 'md';
+    const revisionId: string | undefined = req.params.revisionId;
+
+    let revision: HydratedDocument<IRevision> | null;
     let pagePath;
 
     const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
@@ -878,9 +880,17 @@ module.exports = (crowi) => {
     }
 
     try {
-      const revisionIdForFind = revisionId ?? (page.revision != null ? getIdForRef(page.revision) : undefined);
+      const targetId = revisionId ?? (page.revision != null ? getIdForRef(page.revision) : null);
+      if (targetId == null) {
+        throw new Error('revisionId is not specified');
+      }
 
+      const revisionIdForFind = new mongoose.Types.ObjectId(targetId);
       revision = await Revision.findById(revisionIdForFind);
+      if (revision == null) {
+        throw new Error('Revision is not found');
+      }
+
       pagePath = page.path;
 
       // Error if pageId and revison's pageIds do not match