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

gridfs generateUrl return file path(no api)

yusueketk 7 лет назад
Родитель
Сommit
fc707f4fed
3 измененных файлов с 40 добавлено и 38 удалено
  1. 31 30
      src/server/routes/attachment.js
  2. 1 1
      src/server/routes/index.js
  3. 8 7
      src/server/service/file-uploader/gridfs.js

+ 31 - 30
src/server/routes/attachment.js

@@ -68,40 +68,41 @@ module.exports = function(crowi, app) {
    * @apiName get
    * @apiName get
    * @apiGroup Attachment
    * @apiGroup Attachment
    *
    *
-   * @apiParam {String} filePath
+   * @apiParam {String} id
    */
    */
-  api.get = function(req, res) {
-    const filePath = req.params.filePath;
+  api.get = async function(req, res) {
+    const id = req.params.id;
 
 
-    AttachmentFile.find({filename: filePath}, async function(err, file) {
-      if (err) {
-        throw new Error(err);
-      }
-      const id = file[0].id;
-      const contentType = file[0].contentType;
-      const readStream = new Promise((resolve, reject) => {
-        let buf;
-        const stream = AttachmentFile.readById(id);
-        stream.on('error', function(error) {
-          reject(error);
-        });
-        stream.on('data', function(data) {
-          if (buf) {
-            buf = Buffer.concat([buf, data]);
-          }
-          else {
-            buf = data;
-          }
-        });
-        stream.on('close', function() {
-          debug('GridFS readstream closed');
-          resolve(buf);
-        });
+    // AttachmentFile.find({filename: filePath}, async function(err, file) {
+    // if (err) {
+    //   throw new Error(err);
+    // }
+    // const id = file[0].id;
+    // const contentType = file[0].contentType;
+    const contentType = 'image/jpeg';
+    const readStream = new Promise((resolve, reject) => {
+      let buf;
+      const stream = AttachmentFile.readById(id);
+      stream.on('error', function(error) {
+        reject(error);
+      });
+      stream.on('data', function(data) {
+        if (buf) {
+          buf = Buffer.concat([buf, data]);
+        }
+        else {
+          buf = data;
+        }
+      });
+      stream.on('close', function() {
+        debug('GridFS readstream closed');
+        resolve(buf);
       });
       });
-      const data = await readStream;
-      res.set('Content-Type', contentType);
-      return res.send(ApiResponse.success(data));
     });
     });
+    const data = await readStream;
+    res.set('Content-Type', contentType);
+    return res.send(ApiResponse.success(data));
+    // });
   };
   };
 
 
   /**
   /**

+ 1 - 1
src/server/routes/index.js

@@ -176,7 +176,7 @@ module.exports = function(crowi, app) {
   app.get( '/:id([0-9a-z]{24})'       , loginRequired(crowi, app, false) , page.api.redirector);
   app.get( '/:id([0-9a-z]{24})'       , loginRequired(crowi, app, false) , page.api.redirector);
   app.get( '/_r/:id([0-9a-z]{24})'    , loginRequired(crowi, app, false) , page.api.redirector); // alias
   app.get( '/_r/:id([0-9a-z]{24})'    , loginRequired(crowi, app, false) , page.api.redirector); // alias
   app.get( '/download/:id([0-9a-z]{24})' , loginRequired(crowi, app, false) , attachment.api.download);
   app.get( '/download/:id([0-9a-z]{24})' , loginRequired(crowi, app, false) , attachment.api.download);
-  app.get( '/files/:id([0-9a-z]{24})'  , loginRequired(crowi, app, false), attachment.api.get);
+  app.get('/files/:id([0-9a-z]{24})'  , loginRequired(crowi, app, false), attachment.api.get);
 
 
   app.get( '/_search'                 , loginRequired(crowi, app, false) , search.searchPage);
   app.get( '/_search'                 , loginRequired(crowi, app, false) , search.searchPage);
   app.get( '/_api/search'             , accessTokenParser , loginRequired(crowi, app, false) , search.api.search);
   app.get( '/_api/search'             , accessTokenParser , loginRequired(crowi, app, false) , search.api.search);

+ 8 - 7
src/server/service/file-uploader/gridfs.js

@@ -86,13 +86,14 @@ module.exports = function(crowi) {
   };
   };
 
 
 
 
-  lib.generateUrl = function(filePath) {
-    // var config = crowi.getConfig();
-    // var baseUrl = (config.crowi['app:siteUrl:fixed'] || '');
-
-    // const url = `${baseUrl}/_api/attachments.getMongo?filePath=${filePath}`;
-    // return url;
-    return `/files/${filePath}`;
+  lib.generateUrl = async function(filePath) {
+    await AttachmentFile.find({filename: filePath},
+      function(err, file) {
+        if (err) {
+          throw new Error(err);
+        }
+        return `/files/${file[0].id}`;
+      });
   };
   };
 
 
   return lib;
   return lib;