Sotaro KARASAWA 10 лет назад
Родитель
Сommit
8111835405
4 измененных файлов с 54 добавлено и 9 удалено
  1. 7 8
      lib/models/attachment.js
  2. 4 1
      lib/routes/attachment.js
  3. 28 0
      lib/views/page.html
  4. 15 0
      resource/css/_layout.scss

+ 7 - 8
lib/models/attachment.js

@@ -17,6 +17,7 @@ module.exports = function(crowi) {
     creator: { type: ObjectId, ref: 'User', index: true  },
     filePath: { type: String, required: true },
     fileName: { type: String, required: true },
+    originalName: { type: String },
     fileFormat: { type: String, required: true },
     fileSize: { type: Number, default: 0 },
     createdAt: { type: Date, default: Date.now }
@@ -29,7 +30,8 @@ module.exports = function(crowi) {
 
       self
         .find({page: id})
-        .sort('updatedAt', -1)
+        .sort({'updatedAt': 1})
+        .populate('creator')
         .exec(function(err, data) {
           if (err) {
             return reject(err);
@@ -39,17 +41,13 @@ module.exports = function(crowi) {
             return resolve([]);
           }
 
-          return data.populate(
-            [{path: 'creator', model: 'User'}],
-            function (err, populatedData) {
-              return resolve(populatedData);
-            }
-          );
+          debug(data);
+          return resolve(data);
         });
     });
   };
 
-  attachmentSchema.statics.create = function(pageId, creator, filePath, fileName, fileFormat, fileSize) {
+  attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize) {
     var Attachment = this;
 
     return new Promise(function(resolve, reject) {
@@ -58,6 +56,7 @@ module.exports = function(crowi) {
       newAttachment.page = pageId;
       newAttachment.creator = creator._id;
       newAttachment.filePath = filePath;
+      newAttachment.originalName = originalName;
       newAttachment.fileName = fileName;
       newAttachment.fileFormat = fileFormat;
       newAttachment.fileSize = fileSize;

+ 4 - 1
lib/routes/attachment.js

@@ -6,6 +6,7 @@ module.exports = function(crowi, app) {
     , User = crowi.model('User')
     , Page = crowi.model('Page')
     , Promise = require('bluebird')
+    , config = crowi.getConfig()
     , fs = require('fs')
     , actions = {}
     , api = {};
@@ -20,6 +21,7 @@ module.exports = function(crowi, app) {
       res.json({
         status: true,
         data: {
+          fileBaseUrl: 'https://' + config.crowi['aws:bucket'] +'.s3.amazonaws.com/', // FIXME: ベタ書きよくない
           attachments: attachments
         }
       });
@@ -72,6 +74,7 @@ module.exports = function(crowi, app) {
       id = pageData._id;
 
       var tmpPath = tmpFile.path,
+        originalName = tmpFile.originalname,
         fileName = tmpFile.name,
         fileType = tmpFile.mimetype,
         fileSize = tmpFile.size,
@@ -92,7 +95,7 @@ module.exports = function(crowi, app) {
         debug('Uploaded data is: ', data);
 
         // TODO size
-        Attachment.create(id, req.user, filePath, fileName, fileType, fileSize)
+        Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize)
         .then(function(data) {
           var imageUrl = fileUploader.generateS3FileUrl(data.filePath);
           return res.json({

+ 28 - 0
lib/views/page.html

@@ -143,6 +143,34 @@
 {% endblock %}
 
 {% block content_footer %}
+
+<div class="page-attachments">
+  <p>Attachments</p>
+  <ul>
+  </ul>
+</div>
+<script>
+  (function() {
+    var pageId = $('#content-main').data('page-id');
+    var $pageAttachmentList = $('.page-attachments ul');
+    if (pageId) {
+      $.get('/_api/attachment/page/' + pageId, function(res) {
+        var attachments = res.data.attachments;
+        var urlBase = res.data.fileBaseUrl;
+        if (attachments.length > 0) {
+          $.each(attachments, function(i, file) {
+            console.log(file);
+            $pageAttachmentList.append(
+            '<li><a href="' + urlBase + file.filePath + '">' + (file.originalName || file.fileName) + '</a> <span class="label label-default">' + file.fileFormat + '</span></li>'
+            );
+          })
+        } else {
+          $('.page-attachments').remove();
+        }
+      });
+    }
+  })();
+</script>
 <footer>
   {% if not page %}
   {% else %}

+ 15 - 0
resource/css/_layout.scss

@@ -305,6 +305,21 @@
       }
     }
 
+    .page-attachments {
+      background: #f0f0f0;
+      padding: 10px;
+      font-size: 0.9em;
+      color: #888;
+      margin: 10px 0;
+      border-radius: 5px;
+      p {
+        font-weight: bold;
+      }
+
+      ul {
+      }
+    }
+
     .footer { // {{{
       position: fixed;
       width: 100%;