Browse Source

Save files serverside with filename ObjectID, without filename.

Thanks to g-roliveira, iamabrantes, Floaz, koelle25, scott-dunt, mfilser and xet7 !

Fixes #4416
Lauri Ojansivu 2 years ago
parent
commit
76ac070f9b
2 changed files with 16 additions and 3 deletions
  1. 6 1
      models/attachments.js
  2. 10 2
      server/migrations.js

+ 6 - 1
models/attachments.js

@@ -72,7 +72,12 @@ Attachments = new FilesCollection({
       filenameWithoutExtension = Math.random().toString(36).slice(2);
       filenameWithoutExtension = Math.random().toString(36).slice(2);
       fileId = Math.random().toString(36).slice(2);
       fileId = Math.random().toString(36).slice(2);
     }
     }
-    const ret = fileId + "-original-" + filenameWithoutExtension;
+
+    // OLD:
+    //const ret = fileId + "-original-" + filenameWithoutExtension;
+    // NEW: Save file only with filename of ObjectID, not including filename.
+    // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
+    const ret = fileId;
     // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
     // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
     return ret;
     return ret;
   },
   },

+ 10 - 2
server/migrations.js

@@ -1250,7 +1250,11 @@ Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
   AttachmentsOld.find().forEach(function(fileObj) {
   AttachmentsOld.find().forEach(function(fileObj) {
     const newFileName = fileObj.name();
     const newFileName = fileObj.name();
     const storagePath = Attachments.storagePath({});
     const storagePath = Attachments.storagePath({});
-    const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
+    // OLD:
+    // const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
+    // NEW: Save file only with filename of ObjectID, not including filename.
+    // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
+    const filePath = path.join(storagePath, `${fileObj._id}`);
 
 
     // This is "example" variable, change it to the userId that you might be using.
     // This is "example" variable, change it to the userId that you might be using.
     const userId = fileObj.userId;
     const userId = fileObj.userId;
@@ -1318,7 +1322,11 @@ Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
   AvatarsOld.find().forEach(function(fileObj) {
   AvatarsOld.find().forEach(function(fileObj) {
     const newFileName = fileObj.name();
     const newFileName = fileObj.name();
     const storagePath = Avatars.storagePath({});
     const storagePath = Avatars.storagePath({});
-    const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
+    // OLD:
+    // const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
+    // NEW: Save file only with filename of ObjectID, not including filename.
+    // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
+    const filePath = path.join(storagePath, `${fileObj._id}`);
 
 
     // This is "example" variable, change it to the userId that you might be using.
     // This is "example" variable, change it to the userId that you might be using.
     const userId = fileObj.userId;
     const userId = fileObj.userId;