Sotaro KARASAWA 9 years ago
parent
commit
843d92b654

+ 2 - 0
lib/crowi/index.js

@@ -27,6 +27,8 @@ function Crowi (rootdir, env)
   this.resourceDir = path.join(this.rootDir, 'resource') + sep;
   this.viewsDir  = path.join(this.libDir, 'views') + sep;
   this.mailDir   = path.join(this.viewsDir, 'mail') + sep;
+  this.tmpDir    = path.join(this.rootDir, 'tmp') + sep;
+  this.cacheDir  = path.join(this.tmpDir, 'cache');
 
   this.assets    = {};
   try {

+ 38 - 0
lib/models/attachment.js

@@ -32,6 +32,24 @@ module.exports = function(crowi) {
     return fileUploader.generateUrl(this.filePath);
   });
 
+  attachmentSchema.statics.findById = function(id) {
+    var Attachment = this;
+
+    return new Promise(function(resolve, reject) {
+      Attachment.findOne({_id: id}, function(err, data) {
+        if (err) {
+          return reject(err);
+        }
+
+        if (data === null) {
+          return reject(new Error('Attachment not found'));
+        }
+        return resolve(data);
+      });
+
+    });
+  };
+
   attachmentSchema.statics.getListByPageId = function(id) {
     var self = this;
 
@@ -104,5 +122,25 @@ module.exports = function(crowi) {
 
   };
 
+  attachmentSchema.statics.createCacheFileName = function(attachment) {
+    return crowi.cacheDir + 'attachment-' + attachment._id;
+  };
+
+  attachmentSchema.statics.findDeliveryFile = function(attachment) {
+    // find local
+    var fs = require('fs');
+    var deliveryFile = {
+      filename: '',
+      options: {
+        headers: {
+          'Content-Type': attachment.fileFormat,
+        },
+      },
+    };
+    var cacheFileName = this.createCacheFileName(attachment);
+    // とちゅう
+    return deliveryFile;
+  };
+
   return mongoose.model('Attachment', attachmentSchema);
 };

+ 16 - 0
lib/routes/attachment.js

@@ -15,6 +15,22 @@ module.exports = function(crowi, app) {
 
   actions.api = api;
 
+  api.redirector = function(req, res){
+    var id = req.params.id;
+
+    Attachment.findById(id)
+    .then(function(data) {
+
+      // TODO: file delivery plugin for cdn
+      var deliveryFile = Attachment.findDeliveryFile(data);
+      return res.sendFile(deliveryFile.filename, deliveryFile.options);
+    }).catch(function(err) {
+
+      // not found
+      return res.sendFile(crowi.publicDir + '/images/file-not-found.png');
+    });
+  };
+
   /**
    * @api {get} /attachments.list Get attachments of the page
    * @apiName ListAttachments

+ 1 - 0
lib/routes/index.js

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

BIN
public/images/file-not-found.png


+ 1 - 0
tmp/cache/.gitignore

@@ -0,0 +1 @@
+*