Просмотр исходного кода

support retrieving attachment data with id

Yuki Takei 5 лет назад
Родитель
Сommit
264db3d1ea

+ 4 - 0
packages/growi-plugin-attachment-refs/README.md

@@ -43,11 +43,13 @@ Usage
 ```
 ```
 $ref(file.txt)
 $ref(file.txt)
 $ref(file.txt, page=/somewhere/page)
 $ref(file.txt, page=/somewhere/page)
+$ref(5f17286fd7fbb1104fdbd111, page=/somewhere/page)
 ```
 ```
 
 
 #### Options
 #### Options
 
 
 - **`file`** : File name of reference file (default: the first argument)
 - **`file`** : File name of reference file (default: the first argument)
+- **`id`** : Attachment ID of reference file (default: the first argument)
 - *`page`* : Target page path of reference file (default: current page)
 - *`page`* : Target page path of reference file (default: current page)
 
 
 
 
@@ -74,11 +76,13 @@ $refs(/somewhere/page, regexp=/^file.*\.txt$/)
 
 
 ```
 ```
 $refimg(pict.png, width=50%, alt=Pic)
 $refimg(pict.png, width=50%, alt=Pic)
+$refimg(5f17286fd7fbb1104fdbd111, width=50%, alt=Pic)
 ```
 ```
 
 
 #### Options
 #### Options
 
 
 - **`file`** : File name of reference file (default: the first argument)
 - **`file`** : File name of reference file (default: the first argument)
+- **`id`** : Attachment ID of reference file (default: the first argument)
 - *`width`* : width
 - *`width`* : width
 - *`height`* : height
 - *`height`* : height
 - *`max-width`* : max-width
 - *`max-width`* : max-width

+ 1 - 1
packages/growi-plugin-attachment-refs/src/client/js/components/AttachmentList.jsx

@@ -88,7 +88,7 @@ export default class AttachmentList extends React.Component {
         res = await axios.get('/_api/plugin/ref', {
         res = await axios.get('/_api/plugin/ref', {
           params: {
           params: {
             pagePath: refsContext.pagePath,
             pagePath: refsContext.pagePath,
-            fileName: refsContext.fileName,
+            fileNameOrId: refsContext.fileNameOrId,
             options: refsContext.options,
             options: refsContext.options,
           },
           },
         });
         });

+ 5 - 4
packages/growi-plugin-attachment-refs/src/client/js/util/RefsContext.js

@@ -68,15 +68,16 @@ export default class RefsContext extends TagContext {
    * parse method for 'ref' and 'refimg'
    * parse method for 'ref' and 'refimg'
    */
    */
   parseForSingle(parsedArgs) {
   parseForSingle(parsedArgs) {
-    // determine fileName
+    // determine fileNameOrId
     // order:
     // order:
     //   1: ref(file=..., ...)
     //   1: ref(file=..., ...)
+    //   2: ref(id=..., ...)
     //   2: refs(firstArgs, ...)
     //   2: refs(firstArgs, ...)
-    this.fileName = this.options.file
+    this.fileNameOrId = this.options.file || this.options.id
       || ((parsedArgs.firstArgsValue === true) ? parsedArgs.firstArgsKey : undefined);
       || ((parsedArgs.firstArgsValue === true) ? parsedArgs.firstArgsKey : undefined);
 
 
-    if (this.fileName == null) {
-      throw new Error('fileName is not specified. set first argument or specify \'file\' option');
+    if (this.fileNameOrId == null) {
+      throw new Error('\'file\' or \'id\' is not specified. Set first argument or specify \'file\' or \'id\' option');
     }
     }
 
 
     // determine pagePath
     // determine pagePath

+ 7 - 4
packages/growi-plugin-attachment-refs/src/server/routes/refs.js

@@ -67,7 +67,7 @@ module.exports = (crowi) => {
    */
    */
   router.get('/ref', async(req, res) => {
   router.get('/ref', async(req, res) => {
     const user = req.user;
     const user = req.user;
-    const { pagePath, fileName } = req.query;
+    const { pagePath, fileNameOrId } = req.query;
     // eslint-disable-next-line no-unused-vars
     // eslint-disable-next-line no-unused-vars
     const options = JSON.parse(req.query.options);
     const options = JSON.parse(req.query.options);
 
 
@@ -93,17 +93,20 @@ module.exports = (crowi) => {
     const attachment = await Attachment
     const attachment = await Attachment
       .findOne({
       .findOne({
         page: page._id,
         page: page._id,
-        originalName: fileName,
+        $or: [
+          { _id: fileNameOrId },
+          { originalName: fileNameOrId },
+        ],
       })
       })
       .populate({ path: 'creator', select: User.USER_PUBLIC_FIELDS, populate: creatorPopulateOpt });
       .populate({ path: 'creator', select: User.USER_PUBLIC_FIELDS, populate: creatorPopulateOpt });
 
 
     // not found
     // not found
     if (attachment == null) {
     if (attachment == null) {
-      res.status(404).send(`attachment (fileName: '${fileName}') is not found.`);
+      res.status(404).send(`attachment '${fileNameOrId}' is not found.`);
       return;
       return;
     }
     }
 
 
-    logger.debug(`attachment '${attachment.id}' is found from filename '${fileName}'`);
+    logger.debug(`attachment '${attachment.id}' is found from fileNameOrId '${fileNameOrId}'`);
 
 
     // forbidden
     // forbidden
     const isAccessible = await Page.isAccessiblePageByViewer(attachment.page, user);
     const isAccessible = await Page.isAccessiblePageByViewer(attachment.page, user);