Taichi Masuyama 4 лет назад
Родитель
Сommit
2705e7b79e
2 измененных файлов с 50 добавлено и 46 удалено
  1. 4 4
      packages/app/src/server/routes/index.js
  2. 46 42
      packages/app/src/server/routes/page.js

+ 4 - 4
packages/app/src/server/routes/index.js

@@ -139,8 +139,6 @@ module.exports = function(crowi, app) {
   // my drafts
   // my drafts
   app.get('/me/drafts'                , loginRequiredStrictly, me.drafts.list);
   app.get('/me/drafts'                , loginRequiredStrictly, me.drafts.list);
 
 
-  app.get('/:id([0-9a-z]{24})'       , loginRequired , page.showPage, page.notFound);
-  app.get('/_r/:id([0-9a-z]{24})'    , loginRequired , page.redirector); // alias
   app.get('/attachment/:id([0-9a-z]{24})' , certifySharedFile , loginRequired, attachment.api.get);
   app.get('/attachment/:id([0-9a-z]{24})' , certifySharedFile , loginRequired, attachment.api.get);
   app.get('/attachment/profile/:id([0-9a-z]{24})' , loginRequired, attachment.api.get);
   app.get('/attachment/profile/:id([0-9a-z]{24})' , loginRequired, attachment.api.get);
   app.get('/attachment/:pageId/:fileName', loginRequired, attachment.api.obsoletedGetForMongoDB); // DEPRECATED: remains for backward compatibility for v3.3.x or below
   app.get('/attachment/:pageId/:fileName', loginRequired, attachment.api.obsoletedGetForMongoDB); // DEPRECATED: remains for backward compatibility for v3.3.x or below
@@ -195,7 +193,9 @@ module.exports = function(crowi, app) {
 
 
   app.get('/share/:linkId', page.showSharedPage);
   app.get('/share/:linkId', page.showSharedPage);
 
 
-  app.get('/*/$'                   , loginRequired , page.redirectorWithEndOfSlash, page.notFound);
-  app.get('/*'                     , loginRequired , autoReconnectToSearch, page.redirector, page.notFound);
+  app.get('/:id([0-9a-z]{24})'       , loginRequired , page.showPage);
+
+  app.get('/*/$'                   , loginRequired , page.redirectorWithEndOfSlash);
+  app.get('/*'                     , loginRequired , autoReconnectToSearch, page.redirector);
 
 
 };
 };

+ 46 - 42
packages/app/src/server/routes/page.js

@@ -289,6 +289,47 @@ module.exports = function(crowi, app) {
     return compiledTemplate(definitions);
     return compiledTemplate(definitions);
   }
   }
 
 
+  async function _notFound(req, res) {
+    const path = req.pagePath || getPathFromRequest(req);
+
+    let view;
+    const renderVars = { path };
+
+    if (!isCreatablePage(path)) {
+      view = 'layout-growi/not_creatable';
+    }
+    else if (req.isForbidden) {
+      view = 'layout-growi/forbidden';
+    }
+    else {
+      view = 'layout-growi/not_found';
+
+      // retrieve templates
+      if (req.user != null) {
+        const template = await Page.findTemplate(path);
+        if (template.templateBody) {
+          const body = replacePlaceholdersOfTemplate(template.templateBody, req);
+          const tags = template.templateTags;
+          renderVars.template = body;
+          renderVars.templateTags = tags;
+        }
+      }
+
+      // add scope variables by ancestor page
+      const ancestor = await Page.findAncestorByPathAndViewer(path, req.user);
+      if (ancestor != null) {
+        await ancestor.populate('grantedGroup').execPopulate();
+        addRenderVarsForScope(renderVars, ancestor);
+      }
+    }
+
+    const limit = 50;
+    const offset = parseInt(req.query.offset) || 0;
+    await addRenderVarsForDescendants(renderVars, path, req.user, offset, limit, true);
+
+    return res.render(view, renderVars);
+  }
+
   async function showPageForPresentation(req, res, next) {
   async function showPageForPresentation(req, res, next) {
     const id = req.params.id;
     const id = req.params.id;
     const { revisionId } = req.query;
     const { revisionId } = req.query;
@@ -359,13 +400,13 @@ module.exports = function(crowi, app) {
     if (page == null) {
     if (page == null) {
       // check the page is forbidden or just does not exist.
       // check the page is forbidden or just does not exist.
       req.isForbidden = await Page.count({ _id: id }) > 0;
       req.isForbidden = await Page.count({ _id: id }) > 0;
-      return next();
+      return _notFound(req, res);
     }
     }
 
 
     // empty page
     // empty page
     if (page.isEmpty) {
     if (page.isEmpty) {
       req.pagePath = page.path;
       req.pagePath = page.path;
-      return next(); // to page.notFound
+      return _notFound(req, res);
     }
     }
 
 
     const { path } = page; // this must exist
     const { path } = page; // this must exist
@@ -514,44 +555,7 @@ module.exports = function(crowi, app) {
   /* eslint-enable no-else-return */
   /* eslint-enable no-else-return */
 
 
   actions.notFound = async function(req, res) {
   actions.notFound = async function(req, res) {
-    const path = req.pagePath || getPathFromRequest(req);
-
-    let view;
-    const renderVars = { path };
-
-    if (!isCreatablePage(path)) {
-      view = 'layout-growi/not_creatable';
-    }
-    else if (req.isForbidden) {
-      view = 'layout-growi/forbidden';
-    }
-    else {
-      view = 'layout-growi/not_found';
-
-      // retrieve templates
-      if (req.user != null) {
-        const template = await Page.findTemplate(path);
-        if (template.templateBody) {
-          const body = replacePlaceholdersOfTemplate(template.templateBody, req);
-          const tags = template.templateTags;
-          renderVars.template = body;
-          renderVars.templateTags = tags;
-        }
-      }
-
-      // add scope variables by ancestor page
-      const ancestor = await Page.findAncestorByPathAndViewer(path, req.user);
-      if (ancestor != null) {
-        await ancestor.populate('grantedGroup').execPopulate();
-        addRenderVarsForScope(renderVars, ancestor);
-      }
-    }
-
-    const limit = 50;
-    const offset = parseInt(req.query.offset) || 0;
-    await addRenderVarsForDescendants(renderVars, path, req.user, offset, limit, true);
-
-    return res.render(view, renderVars);
+    return _notFound(req, res);
   };
   };
 
 
   actions.deletedPageListShow = async function(req, res) {
   actions.deletedPageListShow = async function(req, res) {
@@ -598,7 +602,7 @@ module.exports = function(crowi, app) {
 
 
     if (pages.length === 1) {
     if (pages.length === 1) {
       if (pages[0].isEmpty) {
       if (pages[0].isEmpty) {
-        return next();
+        return _notFound(req, res);
       }
       }
 
 
       const url = new URL('https://dummy.origin');
       const url = new URL('https://dummy.origin');
@@ -609,7 +613,7 @@ module.exports = function(crowi, app) {
       return res.safeRedirect(urljoin(url.pathname, url.search));
       return res.safeRedirect(urljoin(url.pathname, url.search));
     }
     }
 
 
-    return next(); // to page.notFound
+    return _notFound(req, res);
   }
   }
 
 
   actions.redirector = async function(req, res, next) {
   actions.redirector = async function(req, res, next) {