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

+ 1 - 3
src/server/models/attachment.js

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

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

@@ -59,6 +59,9 @@ module.exports = function(crowi, app) {
    * @apiParam {String} pageId, fileId
    */
   api.get = async function(req, res) {
+    if (process.env.FILE_UPLOAD != 'gridfs') {
+      return res.status(404);
+    }
     const pageId = req.params.pageId;
     const fileId = req.params.fileId;
     const filePath = `attachment/${pageId}/${fileId}`;
@@ -67,27 +70,6 @@ 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
@@ -178,7 +160,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); // [mongoid]
+          return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
         }).then(function(data) {
           var fileUrl = data.fileUrl;
 

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

@@ -176,8 +176,7 @@ 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( '/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( '/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);

+ 2 - 18
src/server/service/file-uploader/gridfs.js

@@ -40,8 +40,7 @@ module.exports = function(crowi) {
           if (error) {
             reject(error);
           }
-          resolve(createdFile._id); // [mongoid]
-          // resolve();
+          resolve();
         });
     });
   };
@@ -100,10 +99,6 @@ module.exports = function(crowi) {
     };
   };
 
-  lib.getFileDataById = async function(id) {
-    return await lib.readFileData(id);
-  };
-
   lib.getFile = function(filePath) {
     return new Promise((resolve, reject) => {
       AttachmentFile.findOne({
@@ -140,18 +135,7 @@ module.exports = function(crowi) {
   };
 
   lib.generateUrl = function(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 `/${filePath}`;
   };
 
   return lib;