Przeglądaj źródła

Fix error when tmpfile doesnt include ext in its original name

Sotaro KARASAWA 9 lat temu
rodzic
commit
37cf11b11c
1 zmienionych plików z 23 dodań i 1 usunięć
  1. 23 1
      lib/models/attachment.js

+ 23 - 1
lib/models/attachment.js

@@ -99,8 +99,30 @@ module.exports = function(crowi) {
     });
   };
 
+  attachmentSchema.statics.guessExtByFileType = function (fileType) {
+    let ext = '';
+    const isImage = fileType.match(/^image\/(.+)/i);
+
+    if (isImage) {
+      ext = isImage[1].toLowerCase();
+    }
+
+    return ext;
+  };
+
   attachmentSchema.statics.createAttachmentFilePath = function (pageId, fileName, fileType) {
-    var ext = '.' + fileName.match(/(.*)(?:\.([^.]+$))/)[2] || '';
+    const Attachment = this;
+    let ext = '';
+    const fnExt = fileName.match(/(.*)(?:\.([^.]+$))/);
+
+    if (fnExt) {
+      ext = '.' + fnExt[2];
+    } else {
+      ext = Attachment.guessExtByFileType(fileType);
+      if (ext !== '') {
+        ext = '.' + ext;
+      }
+    }
 
     return 'attachment/' + pageId + '/' + generateFileHash(fileName) + ext;
   };