Bläddra i källkod

fix: 粘帖文件格式限制

zhangjie 4 månader sedan
förälder
incheckning
160e958124
2 ändrade filer med 14 tillägg och 5 borttagningar
  1. 9 5
      src/components/vEditor/clipboard.js
  2. 5 0
      src/components/vEditor/utils.js

+ 9 - 5
src/components/vEditor/clipboard.js

@@ -1,4 +1,4 @@
-import { insertTagToEditor } from "./utils";
+import { checkValidImage, insertTagToEditor } from "./utils";
 // import { randomCode } from "../../utils/utils";
 
 let _this = null;
@@ -210,6 +210,11 @@ async function pasteImage(file, attributes) {
     return;
   }
 
+  if (!checkValidImage(file.type)) {
+    _this.$message("只支持png、jpg、jpeg格式图片!");
+    return;
+  }
+
   // 默认转base64;
   const srcBase64 = await fileToBase64(file);
   insertTagToEditor(null, "IMG", srcBase64, attributes);
@@ -251,10 +256,9 @@ export async function pasteHandle(event) {
     return;
   }
 
-  const fileItems = filterItem(
-    clipboard.items,
-    (item) => item.kind == "file" && item.type.match("^image/")
-  );
+  const fileItems = filterItem(clipboard.items, (item) => {
+    return item.kind == "file" && checkValidImage(item.type);
+  });
   if (fileItems.length) {
     console.log("... paste: file ");
     for (let index = 0; index < fileItems.length; index++) {

+ 5 - 0
src/components/vEditor/utils.js

@@ -98,3 +98,8 @@ export function checkFileFormat(filename, format) {
     ? format.some((item) => item.toLocaleLowerCase() === _file_format)
     : true;
 }
+
+export function checkValidImage(imageType) {
+  const dataUriRegex = /^image\/(png|jpg|jpeg)/;
+  return dataUriRegex.test(imageType);
+}