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

WIP: refactor attachment

ensure to create refs/download link url on the server side
Yuki Takei 7 лет назад
Родитель
Сommit
954823e708

+ 2 - 3
src/client/js/components/PageAttachment/Attachment.js

@@ -35,7 +35,7 @@ export default class Attachment extends React.Component {
 
     const btnDownload = (this.props.isUserLoggedIn)
       ? (
-        <a className="attachment-download" href={`/download/${attachment._id}`}>
+        <a className="attachment-download" href={attachment.downloadPathProxied}>
           <i className="icon-cloud-download"></i>
         </a>)
       : '';
@@ -50,9 +50,8 @@ export default class Attachment extends React.Component {
     return (
       <li>
         <User user={attachment.creator} />
-        <i className={formatIcon}></i>
 
-        <a href={attachment.url}> {attachment.originalName}</a>
+        <a href={attachment.filePathProxied}><i className={formatIcon}></i> {attachment.originalName}</a>
 
         {fileType}
 

+ 7 - 28
src/server/models/attachment.js

@@ -27,12 +27,15 @@ module.exports = function(crowi) {
     createdAt: { type: Date, default: Date.now },
   }, {
     toJSON: {
-      virtuals: true
     },
   });
 
-  attachmentSchema.virtual('fileUrl').get(function() {
-    return `/files/${this._id}`;
+  attachmentSchema.virtual('filePathProxied').get(function() {
+    return `/attachment/${this._id}`;
+  });
+
+  attachmentSchema.virtual('downloadPathProxied').get(function() {
+    return `/download/${this._id}`;
   });
 
   attachmentSchema.virtual('filePathOnStorage').get(function() {
@@ -46,30 +49,6 @@ module.exports = function(crowi) {
     return filePath;
   });
 
-  attachmentSchema.statics.getListByPageId = function(id) {
-    var self = this;
-
-    return new Promise(function(resolve, reject) {
-
-      self
-        .find({page: id})
-        .sort({'updatedAt': 1})
-        .populate('creator')
-        .exec(function(err, data) {
-          if (err) {
-            return reject(err);
-          }
-
-          if (data.length < 1) {
-            return resolve([]);
-          }
-
-          debug(data);
-          return resolve(data);
-        });
-    });
-  };
-
   attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize) {
     var Attachment = this;
 
@@ -129,7 +108,7 @@ module.exports = function(crowi) {
     var Attachment = this;
 
     return new Promise((resolve, reject) => {
-      Attachment.getListByPageId(pageId)
+      Attachment.find({ page: pageId})
       .then((attachments) => {
         for (let attachment of attachments) {
           Attachment.removeAttachment(attachment).then((res) => {

+ 15 - 20
src/server/routes/attachment.js

@@ -115,32 +115,27 @@ module.exports = function(crowi, app) {
    *
    * @apiParam {String} page_id
    */
-  api.list = function(req, res) {
+  api.list = async function(req, res) {
     const id = req.query.page_id || null;
     if (!id) {
       return res.json(ApiResponse.error('Parameters page_id is required.'));
     }
 
-    Attachment.getListByPageId(id)
-    .then(function(attachments) {
-
-      // NOTE: use original fileUrl directly (not proxy) -- 2017.05.08 Yuki Takei
-      // reason:
-      //   1. this is buggy (doesn't work on Win)
-      //   2. ensure backward compatibility of data
-
-      // var config = crowi.getConfig();
-      // var baseUrl = (config.crowi['app:siteUrl:fixed'] || '');
-      return res.json(ApiResponse.success({
-        attachments: attachments.map(at => {
-          const fileUrl = at.fileUrl;
-          at = at.toObject();
-          // at.url = baseUrl + fileUrl;
-          at.url = fileUrl;
-          return at;
-        })
-      }));
+    let attachments = await Attachment.find({page: id})
+      .sort({'updatedAt': 1})
+      .populate('creator', User.USER_PUBLIC_FIELDS);
+
+    // toJSON
+    attachments = attachments.map(attachment => {
+      const json = attachment.toJSON({ virtuals: true});
+
+      // omit unnecessary property
+      json.filePathOnStorage = undefined;
+
+      return json;
     });
+
+    return res.json(ApiResponse.success({ attachments }));
   };
 
   /**