Просмотр исходного кода

Fix error when tmpfile doesnt include ext in its original name

Sotaro KARASAWA 9 лет назад
Родитель
Сommit
37cf11b11c
1 измененных файлов с 23 добавлено и 1 удалено
  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) {
   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;
     return 'attachment/' + pageId + '/' + generateFileHash(fileName) + ext;
   };
   };