Browse Source

Attachment file rename is now possible

- Relates to: #2099
Martin Filser 3 năm trước cách đây
mục cha
commit
26e1c1dc4a

+ 8 - 0
client/components/cards/attachments.jade

@@ -69,6 +69,9 @@ template(name="attachmentActionsPopup")
           else
           else
             | {{_ 'add-cover'}}
             | {{_ 'add-cover'}}
       if currentUser.isBoardAdmin
       if currentUser.isBoardAdmin
+        a.js-rename
+          i.fa.fa-pencil-square-o
+          | {{_ 'rename'}}
         a.js-confirm-delete
         a.js-confirm-delete
           i.fa.fa-close
           i.fa.fa-close
           | {{_ 'delete'}}
           | {{_ 'delete'}}
@@ -85,3 +88,8 @@ template(name="attachmentActionsPopup")
             a.js-move-storage-gridfs
             a.js-move-storage-gridfs
               i.fa.fa-arrow-right
               i.fa.fa-arrow-right
               | {{_ 'attachment-move-storage-gridfs'}}
               | {{_ 'attachment-move-storage-gridfs'}}
+
+template(name="attachmentRenamePopup")
+  input.js-edit-attachment-name(type='text' autofocus value=name dir="auto")
+  .edit-controls.clearfix
+    button.primary.confirm.js-submit-edit-attachment-name(type="submit") {{_ 'save'}}

+ 25 - 0
client/components/cards/attachments.js

@@ -134,6 +134,7 @@ BlazeComponent.extendComponent({
   events() {
   events() {
     return [
     return [
       {
       {
+        'click .js-rename': Popup.open('attachmentRename'),
         'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete', function() {
         'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete', function() {
           Attachments.remove(this._id);
           Attachments.remove(this._id);
           Popup.back(2);
           Popup.back(2);
@@ -158,3 +159,27 @@ BlazeComponent.extendComponent({
     ]
     ]
   }
   }
 }).register('attachmentActionsPopup');
 }).register('attachmentActionsPopup');
+
+BlazeComponent.extendComponent({
+  events() {
+    return [
+      {
+        'keydown input.js-edit-attachment-name'(evt) {
+          // enter = save
+          if (evt.keyCode === 13) {
+            this.find('button[type=submit]').click();
+          }
+        },
+        'click button.js-submit-edit-attachment-name'(event) {
+          // save button pressed
+          event.preventDefault();
+          const name = this.$('.js-edit-attachment-name')[0]
+            .value
+            .trim();
+          Meteor.call('renameAttachment', this.data()._id, name);
+          Popup.back();
+        },
+      }
+    ]
+  }
+}).register('attachmentRenamePopup');

+ 2 - 1
imports/i18n/data/en.i18n.json

@@ -1177,5 +1177,6 @@
   "size": "Size",
   "size": "Size",
   "storage": "Storage",
   "storage": "Storage",
   "action": "Action",
   "action": "Action",
-  "board-title": "Board Title"
+  "board-title": "Board Title",
+  "attachmentRenamePopup-title": "Attachment Rename"
 }
 }

+ 8 - 1
models/attachments.js

@@ -4,7 +4,7 @@ import { createBucket } from './lib/grid/createBucket';
 import fs from 'fs';
 import fs from 'fs';
 import path from 'path';
 import path from 'path';
 import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
 import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
-import FileStoreStrategyFactory, {moveToStorage, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
+import FileStoreStrategyFactory, {moveToStorage, rename, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
 
 
 let attachmentBucket;
 let attachmentBucket;
 let storagePath;
 let storagePath;
@@ -87,6 +87,13 @@ if (Meteor.isServer) {
       const fileObj = Attachments.findOne({_id: fileObjId});
       const fileObj = Attachments.findOne({_id: fileObjId});
       moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
       moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
     },
     },
+    renameAttachment(fileObjId, newName) {
+      check(fileObjId, String);
+      check(newName, String);
+
+      const fileObj = Attachments.findOne({_id: fileObjId});
+      rename(fileObj, newName, fileStoreStrategyFactory);
+    },
   });
   });
 
 
   Meteor.startup(() => {
   Meteor.startup(() => {

+ 28 - 0
models/lib/fileStoreStrategy.js

@@ -114,6 +114,13 @@ class FileStoreStrategy {
   unlink() {
   unlink() {
   }
   }
 
 
+  /** rename the file (physical)
+   * @li at database the filename is updated after this method
+   * @param newFilePath the new file path
+   */
+  rename(newFilePath) {
+  }
+
   /** return the storage name
   /** return the storage name
    * @return the storage name
    * @return the storage name
    */
    */
@@ -287,6 +294,14 @@ export class FileStoreStrategyFilesystem extends FileStoreStrategy {
     fs.unlink(filePath, () => {});
     fs.unlink(filePath, () => {});
   }
   }
 
 
+  /** rename the file (physical)
+   * @li at database the filename is updated after this method
+   * @param newFilePath the new file path
+   */
+  rename(newFilePath) {
+    fs.renameSync(this.fileObj.versions[this.versionName].path, newFilePath);
+  }
+
   /** return the storage name
   /** return the storage name
    * @return the storage name
    * @return the storage name
    */
    */
@@ -389,3 +404,16 @@ export const copyFile = function(fileObj, newCardId, fileStoreStrategyFactory) {
 
 
   readStream.pipe(writeStream);
   readStream.pipe(writeStream);
 };
 };
+
+export const rename = function(fileObj, newName, fileStoreStrategyFactory) {
+  Object.keys(fileObj.versions).forEach(versionName => {
+    const strategy = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName);
+    const newFilePath = strategy.getNewPath(fileStoreStrategyFactory.storagePath, newName);
+    strategy.rename(newFilePath);
+
+    Attachments.update({ _id: fileObj._id }, { $set: {
+      "name": newName,
+      [`versions.${versionName}.path`]: newFilePath,
+    } });
+  });
+};