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

test to link attachment id and mongo file id

yusueketk 7 лет назад
Родитель
Сommit
ad86a59981

+ 4 - 2
src/server/models/attachment.js

@@ -21,7 +21,8 @@ module.exports = function(crowi) {
     originalName: { type: String },
     fileFormat: { type: String, required: true },
     fileSize: { type: Number, default: 0 },
-    createdAt: { type: Date, default: Date.now }
+    createdAt: { type: Date, default: Date.now },
+    originalId: { type: ObjectId, ref: 'User', index: true}
   }, {
     toJSON: {
       virtuals: true
@@ -79,7 +80,7 @@ module.exports = function(crowi) {
     });
   };
 
-  attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize) {
+  attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize, originalId) { // [mongoid]
     var Attachment = this;
 
     return new Promise(function(resolve, reject) {
@@ -93,6 +94,7 @@ module.exports = function(crowi) {
       newAttachment.fileFormat = fileFormat;
       newAttachment.fileSize = fileSize;
       newAttachment.createdAt = Date.now();
+      newAttachment.originalId = originalId; // [mongoid]
 
       newAttachment.save(function(err, data) {
         if (err) {

+ 22 - 1
src/server/routes/attachment.js

@@ -67,6 +67,27 @@ module.exports = function(crowi, app) {
     return res.send(ApiResponse.success(fileData.data));
   };
 
+  /**
+   * @api {get} /attachments.redirector get attachments from mongoDB
+   * @apiName redirector
+   * @apiGroup Attachment
+   *
+   * @apiParam {String} id
+   */
+  api.redirector = async function(req, res, next) {
+    const id = req.params.id;
+    Attachment.findById(id, async function(err, data) {
+      const id = data.id;
+      const contentType = data.fileFormat;
+      const fileData = await fileUploader.getFileDataById(id);
+      const encodedFileName = encodeURIComponent(data.originalName);
+      res.set('Content-Type', contentType);
+      res.set('Content-Disposition', `inline;filename*=UTF-8''${encodedFileName}`);
+      return res.sendFile(ApiResponse.success(fileData.data));
+    });
+    return res.status(404).sendFile(crowi.publicDir + '/images/file-not-found.png');
+  };
+
   /**
    * @api {get} /attachments.list Get attachments of the page
    * @apiName ListAttachments
@@ -157,7 +178,7 @@ module.exports = function(crowi, app) {
           debug('Uploaded data is: ', data);
 
           // TODO size
-          return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize, data);
+          return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize, data); // [mongoid]
         }).then(function(data) {
           var fileUrl = data.fileUrl;
 

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

@@ -176,7 +176,8 @@ module.exports = function(crowi, app) {
   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( '/download/:id([0-9a-z]{24})' , loginRequired(crowi, app, false) , attachment.api.download);
-  app.get( '/attachment/:pageId/:fileId'  , loginRequired(crowi, app, false), attachment.api.get);
+  app.get( '/files/:id([0-9a-z]{24})', loginRequired(crowi, app, false), attachment.api.redirector);
+  // app.get( '/attachment/:pageId/:fileId'  , loginRequired(crowi, app, false), attachment.api.get);
 
   app.get( '/_search'                 , loginRequired(crowi, app, false) , search.searchPage);
   app.get( '/_api/search'             , accessTokenParser , loginRequired(crowi, app, false) , search.api.search);

+ 25 - 5
src/server/service/file-uploader/gridfs.js

@@ -9,6 +9,7 @@ module.exports = function(crowi) {
   var path = require('path');
   var fs = require('fs');
   var lib = {};
+  var Attachment = crowi.model('Attachment');
   var AttachmentFile = {};
 
   // instantiate mongoose-gridfs
@@ -39,7 +40,8 @@ module.exports = function(crowi) {
           if (error) {
             reject(error);
           }
-          resolve();
+          resolve(createdFile._id); // [mongoid]
+          // resolve();
         });
     });
   };
@@ -92,18 +94,25 @@ module.exports = function(crowi) {
     const id = file.id;
     const contentType = file.contentType;
     const data = await lib.readFileData(id);
-    return {data, contentType};
+    return {
+      data,
+      contentType
+    };
+  };
+
+  lib.getFileDataById = async function(id) {
+    return await lib.readFileData(id);
   };
 
   lib.getFile = function(filePath) {
     return new Promise((resolve, reject) => {
-      AttachmentFile.find({
+      AttachmentFile.findOne({
         filename: filePath
       }, async function(err, file) {
         if (err) {
           reject(err);
         }
-        resolve(file[0]);
+        resolve(file);
       });
     });
   };
@@ -131,7 +140,18 @@ module.exports = function(crowi) {
   };
 
   lib.generateUrl = function(filePath) {
-    return `/${filePath}`;
+    // return `/${filePath}`; // [mongoid]
+    return new Promise((resolve, reject) => {
+      Attachment.find({
+        filePath: filePath
+      }, async function(err, file) {
+        if (err) {
+          reject(err);
+        }
+        const id = file._id;
+        resolve(`/files/${id}`);
+      });
+    });
   };
 
   return lib;