|
@@ -10,6 +10,22 @@ module.exports = (crowi) => {
|
|
|
const Page = crowi.model('Page');
|
|
const Page = crowi.model('Page');
|
|
|
const Attachment = crowi.model('Attachment');
|
|
const Attachment = crowi.model('Attachment');
|
|
|
|
|
|
|
|
|
|
+ const { PageQueryBuilder } = Page;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * generate RegExp instance by the 'expression' arg
|
|
|
|
|
+ * @param {string} expression
|
|
|
|
|
+ * @return {RegExp}
|
|
|
|
|
+ */
|
|
|
|
|
+ function generateRegexp(expression) {
|
|
|
|
|
+ // https://regex101.com/r/uOrwqt/2
|
|
|
|
|
+ const matches = expression.match(/^\/(.+)\/(.*)?$/);
|
|
|
|
|
+
|
|
|
|
|
+ return (matches != null)
|
|
|
|
|
+ ? new RegExp(matches[1], matches[2])
|
|
|
|
|
+ : new RegExp(expression);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* return an Attachment model
|
|
* return an Attachment model
|
|
|
*/
|
|
*/
|
|
@@ -64,15 +80,56 @@ module.exports = (crowi) => {
|
|
|
router.get('/refs', async(req, res) => {
|
|
router.get('/refs', async(req, res) => {
|
|
|
const user = req.user;
|
|
const user = req.user;
|
|
|
const { prefix, pagePath } = req.query;
|
|
const { prefix, pagePath } = req.query;
|
|
|
- // eslint-disable-next-line no-unused-vars
|
|
|
|
|
const options = JSON.parse(req.query.options);
|
|
const options = JSON.parse(req.query.options);
|
|
|
|
|
|
|
|
|
|
+ // check either 'prefix' or 'pagePath ' is specified
|
|
|
if (prefix == null && pagePath == null) {
|
|
if (prefix == null && pagePath == null) {
|
|
|
res.status(400).send('either the param \'prefix\' or \'pagePath\' must be set.');
|
|
res.status(400).send('either the param \'prefix\' or \'pagePath\' must be set.');
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- res.status(200).send({ attachments: [] });
|
|
|
|
|
|
|
+ // check regex
|
|
|
|
|
+ let regex;
|
|
|
|
|
+ const regexOptionValue = options.regexp || options.regex;
|
|
|
|
|
+ if (regexOptionValue != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ regex = generateRegexp(regexOptionValue);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ res.status(400).send(`the 'regex=${options.regex}' option is invalid as RegExp.`);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const builder = new PageQueryBuilder(Page.find());
|
|
|
|
|
+ builder.addConditionToListWithDescendants(prefix || pagePath)
|
|
|
|
|
+ .addConditionToExcludeTrashed()
|
|
|
|
|
+ .addConditionToExcludeRedirect();
|
|
|
|
|
+
|
|
|
|
|
+ Page.addConditionToFilteringByViewerForList(builder, user, false);
|
|
|
|
|
+
|
|
|
|
|
+ const results = await builder.query.select('id').exec();
|
|
|
|
|
+ const pageIds = results.map(result => result.id);
|
|
|
|
|
+
|
|
|
|
|
+ logger.debug('retrieve attachments for pages:', pageIds);
|
|
|
|
|
+
|
|
|
|
|
+ // create query to find
|
|
|
|
|
+ let query = Attachment
|
|
|
|
|
+ .find({
|
|
|
|
|
+ page: { $in: pageIds },
|
|
|
|
|
+ });
|
|
|
|
|
+ // add regex condition
|
|
|
|
|
+ if (regex != null) {
|
|
|
|
|
+ query = query.and({
|
|
|
|
|
+ originalName: { $regex: regex },
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const attachments = await query
|
|
|
|
|
+ .populate({ path: 'creator', select: User.USER_PUBLIC_FIELDS, populate: User.IMAGE_POPULATION })
|
|
|
|
|
+ .exec();
|
|
|
|
|
+
|
|
|
|
|
+ res.status(200).send({ attachments });
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
return router;
|
|
return router;
|