|
|
@@ -6,13 +6,60 @@ import { Router } from 'express';
|
|
|
import type { Model, HydratedDocument } from 'mongoose';
|
|
|
import mongoose, { model, Types } from 'mongoose';
|
|
|
|
|
|
+
|
|
|
import loggerFactory from '../../utils/logger';
|
|
|
|
|
|
const logger = loggerFactory('growi:remark-attachment-refs:routes:refs');
|
|
|
|
|
|
|
|
|
-type RequestWithUser = Request & { user: HydratedDocument<IUser> };
|
|
|
+function generateRegexp(expression: string): RegExp {
|
|
|
+ // https://regex101.com/r/uOrwqt/2
|
|
|
+ const matches = expression.match(/^\/(.+)\/(.*)?$/);
|
|
|
+
|
|
|
+ return (matches != null)
|
|
|
+ ? new RegExp(matches[1], matches[2])
|
|
|
+ : new RegExp(expression);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * add depth condition that limit fetched pages
|
|
|
+ *
|
|
|
+ * @param {any} query
|
|
|
+ * @param {any} pagePath
|
|
|
+ * @param {any} optionsDepth
|
|
|
+ * @returns query
|
|
|
+ */
|
|
|
+function addDepthCondition(query, pagePath, optionsDepth) {
|
|
|
+ // when option strings is 'depth=', the option value is true
|
|
|
+ if (optionsDepth == null || optionsDepth === true) {
|
|
|
+ throw new Error('The value of depth option is invalid.');
|
|
|
+ }
|
|
|
+
|
|
|
+ const range = OptionParser.parseRange(optionsDepth);
|
|
|
|
|
|
+ if (range == null) {
|
|
|
+ return query;
|
|
|
+ }
|
|
|
+
|
|
|
+ const start = range.start;
|
|
|
+ const end = range.end;
|
|
|
+
|
|
|
+ if (start < 1 || end < 1) {
|
|
|
+ throw new Error(`specified depth is [${start}:${end}] : start and end are must be larger than 1`);
|
|
|
+ }
|
|
|
+
|
|
|
+ // count slash
|
|
|
+ const slashNum = pagePath.split('/').length - 1;
|
|
|
+ const depthStart = slashNum; // start is not affect to fetch page
|
|
|
+ const depthEnd = slashNum + end - 1;
|
|
|
+
|
|
|
+ return query.and({
|
|
|
+ path: new RegExp(`^(\\/[^\\/]*){${depthStart},${depthEnd}}$`),
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+type RequestWithUser = Request & { user: HydratedDocument<IUser> };
|
|
|
|
|
|
const loginRequiredFallback = (req, res) => {
|
|
|
return res.status(403).send('login required');
|
|
|
@@ -33,52 +80,6 @@ export const routesFactory = (crowi): any => {
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
const { PageQueryBuilder } = Page as any;
|
|
|
|
|
|
- function generateRegexp(expression: string): RegExp {
|
|
|
- // https://regex101.com/r/uOrwqt/2
|
|
|
- const matches = expression.match(/^\/(.+)\/(.*)?$/);
|
|
|
-
|
|
|
- return (matches != null)
|
|
|
- ? new RegExp(matches[1], matches[2])
|
|
|
- : new RegExp(expression);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * add depth condition that limit fetched pages
|
|
|
- *
|
|
|
- * @param {any} query
|
|
|
- * @param {any} pagePath
|
|
|
- * @param {any} optionsDepth
|
|
|
- * @returns query
|
|
|
- */
|
|
|
- function addDepthCondition(query, pagePath, optionsDepth) {
|
|
|
- // when option strings is 'depth=', the option value is true
|
|
|
- if (optionsDepth == null || optionsDepth === true) {
|
|
|
- throw new Error('The value of depth option is invalid.');
|
|
|
- }
|
|
|
-
|
|
|
- const range = OptionParser.parseRange(optionsDepth);
|
|
|
-
|
|
|
- if (range == null) {
|
|
|
- return query;
|
|
|
- }
|
|
|
-
|
|
|
- const start = range.start;
|
|
|
- const end = range.end;
|
|
|
-
|
|
|
- if (start < 1 || end < 1) {
|
|
|
- throw new Error(`specified depth is [${start}:${end}] : start and end are must be larger than 1`);
|
|
|
- }
|
|
|
-
|
|
|
- // count slash
|
|
|
- const slashNum = pagePath.split('/').length - 1;
|
|
|
- const depthStart = slashNum; // start is not affect to fetch page
|
|
|
- const depthEnd = slashNum + end - 1;
|
|
|
-
|
|
|
- return query.and({
|
|
|
- path: new RegExp(`^(\\/[^\\/]*){${depthStart},${depthEnd}}$`),
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* return an Attachment model
|
|
|
*/
|