Browse Source

Merge pull request #4650 from mfilser/attachment_using_new_feature_of_meteor-files-2.3.0

Attachment using new feature of Meteor Files 2.3.0
Lauri Ojansivu 2 years ago
parent
commit
8eb4ffea89
3 changed files with 42 additions and 5 deletions
  1. 1 1
      .meteor/packages
  2. 10 4
      models/attachments.js
  3. 31 0
      models/avatars.js

+ 1 - 1
.meteor/packages

@@ -81,7 +81,7 @@ konecty:mongo-counter
 percolate:synced-cron
 cfs:filesystem
 ostrio:cookies
-ostrio:files@2.0.1
+ostrio:files@2.3.0
 rajit:bootstrap3-datepicker-fi
 rajit:bootstrap3-datepicker-ar
 rajit:bootstrap3-datepicker-bg

+ 10 - 4
models/attachments.js

@@ -59,7 +59,12 @@ Attachments = new FilesCollection({
       delete opts.meta.fileId;
     } else if (opts?.file?.name) {
       // Server
-      filenameWithoutExtension = opts.file.name.replace(new RegExp(opts.file.extensionWithDot + "$"), "")
+      if (opts.file.extension) {
+        filenameWithoutExtension = opts.file.name.replace(new RegExp(opts.file.extensionWithDot + "$"), "")
+      } else {
+        // file has no extension, so don't replace anything, otherwise the last character is removed (because extensionWithDot = '.')
+        filenameWithoutExtension = opts.file.name;
+      }
       fileId = opts.fileId;
     }
     else {
@@ -71,6 +76,10 @@ Attachments = new FilesCollection({
     // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
     return ret;
   },
+  sanitize(str, max, replacement) {
+    // keep the original filename
+    return str;
+  },
   storagePath() {
     const ret = fileStoreStrategyFactory.storagePath;
     return ret;
@@ -136,9 +145,6 @@ if (Meteor.isServer) {
 
       const fileObj = Attachments.findOne({_id: fileObjId});
       moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
-
-      // since Meteor-Files 2.1.0 the filename is truncated to 28 characters, so rename the file after upload to the right filename back
-      rename(fileObj, fileObj.name, fileStoreStrategyFactory);
     },
     renameAttachment(fileObjId, newName) {
       check(fileObjId, String);

+ 31 - 0
models/avatars.js

@@ -45,6 +45,37 @@ Avatars = new FilesCollection({
   debug: false, // Change to `true` for debugging
   collectionName: 'avatars',
   allowClientCode: true,
+  namingFunction(opts) {
+    let filenameWithoutExtension = ""
+    let fileId = "";
+    if (opts?.name) {
+      // Client
+      filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
+      fileId = opts.meta.fileId;
+      delete opts.meta.fileId;
+    } else if (opts?.file?.name) {
+      // Server
+      if (opts.file.extension) {
+        filenameWithoutExtension = opts.file.name.replace(new RegExp(opts.file.extensionWithDot + "$"), "")
+      } else {
+        // file has no extension, so don't replace anything, otherwise the last character is removed (because extensionWithDot = '.')
+        filenameWithoutExtension = opts.file.name;
+      }
+      fileId = opts.fileId;
+    }
+    else {
+      // should never reach here
+      filenameWithoutExtension = Math.random().toString(36).slice(2);
+      fileId = Math.random().toString(36).slice(2);
+    }
+    const ret = fileId + "-original-" + filenameWithoutExtension;
+    // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
+    return ret;
+  },
+  sanitize(str, max, replacement) {
+    // keep the original filename
+    return str;
+  },
   storagePath() {
     const ret = fileStoreStrategyFactory.storagePath;
     return ret;