Browse Source

Attachment filename contains now the filename of the uploaded file

Martin Filser 3 years ago
parent
commit
0fcfd8e168
2 changed files with 26 additions and 11 deletions
  1. 19 11
      client/components/cards/attachments.js
  2. 7 0
      models/attachments.js

+ 19 - 11
client/components/cards/attachments.js

@@ -21,12 +21,16 @@ Template.cardAttachmentsPopup.events({
   'change .js-attach-file'(event) {
   'change .js-attach-file'(event) {
     const card = this;
     const card = this;
     if (event.currentTarget.files && event.currentTarget.files[0]) {
     if (event.currentTarget.files && event.currentTarget.files[0]) {
+      const fileId = Random.id();
+      const config = {
+        file: event.currentTarget.files[0],
+        fileId: fileId,
+        meta: Utils.getCommonAttachmentMetaFrom(card),
+        chunkSize: 'dynamic',
+      };
+      config.meta.fileId = fileId;
       const uploader = Attachments.insert(
       const uploader = Attachments.insert(
-        {
-          file: event.currentTarget.files[0],
-          meta: Utils.getCommonAttachmentMetaFrom(card),
-          chunkSize: 'dynamic',
-        },
+        config,
         false,
         false,
       );
       );
       uploader.on('uploaded', (error, fileRef) => {
       uploader.on('uploaded', (error, fileRef) => {
@@ -92,13 +96,17 @@ Template.previewClipboardImagePopup.events({
     if (pastedResults && pastedResults.file) {
     if (pastedResults && pastedResults.file) {
       const file = pastedResults.file;
       const file = pastedResults.file;
       window.oPasted = pastedResults;
       window.oPasted = pastedResults;
+      const fileId = Random.id();
+      const config = {
+        file,
+        fileId: fileId,
+        meta: Utils.getCommonAttachmentMetaFrom(card),
+        fileName: file.name || file.type.replace('image/', 'clipboard.'),
+        chunkSize: 'dynamic',
+      };
+      config.meta.fileId = fileId;
       const uploader = Attachments.insert(
       const uploader = Attachments.insert(
-        {
-          file,
-          meta: Utils.getCommonAttachmentMetaFrom(card),
-          fileName: file.name || file.type.replace('image/', 'clipboard.'),
-          chunkSize: 'dynamic',
-        },
+        config,
         false,
         false,
       );
       );
       uploader.on('uploaded', (error, fileRef) => {
       uploader.on('uploaded', (error, fileRef) => {

+ 7 - 0
models/attachments.js

@@ -11,6 +11,13 @@ Attachments = new FilesCollection({
   debug: false, // Change to `true` for debugging
   debug: false, // Change to `true` for debugging
   collectionName: 'attachments',
   collectionName: 'attachments',
   allowClientCode: true,
   allowClientCode: true,
+  namingFunction(opts) {
+    const filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
+    const ret = opts.meta.fileId + "-" + filenameWithoutExtension;
+    // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
+    delete opts.meta.fileId;
+    return ret;
+  },
   storagePath() {
   storagePath() {
     if (process.env.WRITABLE_PATH) {
     if (process.env.WRITABLE_PATH) {
       return path.join(process.env.WRITABLE_PATH, 'uploads', 'attachments');
       return path.join(process.env.WRITABLE_PATH, 'uploads', 'attachments');