Procházet zdrojové kódy

作答题:图片格式和内容检查

Michael Wang před 6 roky
rodič
revize
0acaa3316b
1 změnil soubory, kde provedl 71 přidání a 4 odebrání
  1. 71 4
      src/features/OnlineExam/Examing/UploadPhotos.vue

+ 71 - 4
src/features/OnlineExam/Examing/UploadPhotos.vue

@@ -27,18 +27,18 @@
     <Upload
       ref="upload"
       v-show="this.uploadList.length < 6"
+      :accept="this.format.map(v => 'image/' + v).join()"
       :data="headers"
       :show-upload-list="false"
       :default-file-list="defaultList2"
       :on-success="handleSuccess"
-      :format="['jpg', 'jpeg', 'png']"
+      :format="format"
       :max-size="5 * 1024"
       :on-format-error="handleFormatError"
       :on-exceeded-size="handleMaxSize"
       :before-upload="handleBeforeUpload"
-      multiple
-      type="drag"
       :action="this.uploadUrl"
+      type="drag"
       style="display: inline-block;width:100px;"
     >
       <div style="width: 100px;height:100px;line-height: 100px;">
@@ -77,7 +77,8 @@ export default {
       defaultList2: [...this.defaultList],
       uploadList: [],
       uploadUrl: "",
-      headers: {}
+      headers: {},
+      format: ["jpg", "jpeg", "png"]
     };
   },
   methods: {
@@ -108,7 +109,73 @@ export default {
         desc: file.name + `超过${MAX_UPLOAD_SIZE}M.`
       });
     },
+    fileFormatCheck(file, resolve, reject) {
+      function getMimetype(signature) {
+        switch (signature) {
+          case "89504E47":
+            return "image/png";
+          case "47494638":
+            return "image/gif";
+          case "25504446":
+            return "application/pdf";
+          case "FFD8FFDB":
+          case "FFD8FFE0":
+          case "FFD8FFE1":
+            return "image/jpeg";
+          case "504B0304":
+            return "application/zip";
+          case "504B34":
+            return "application/zip";
+          default:
+            return "Unknown filetype";
+        }
+      }
+
+      const filereader = new FileReader();
+      let uploads = [];
+      filereader.onloadend = evt => {
+        if (evt.target.readyState === FileReader.DONE) {
+          const uint = new Uint8Array(evt.target.result);
+          let bytes = [];
+          uint.forEach(byte => {
+            bytes.push(byte.toString(16));
+          });
+          const hex = bytes.join("").toUpperCase();
+          uploads.push({
+            filename: file.name,
+            filetype: file.type ? file.type : "Unknown/Extension missing",
+            binaryFileType: getMimetype(hex),
+            hex: hex
+          });
+
+          if (
+            ["image/png", "image/gif", "image/jpeg"].includes(getMimetype(hex))
+          ) {
+            resolve();
+          } else {
+            console.log("binary file type check: not zip or pdf");
+            this.$Notice.warning({
+              title: "文件损坏",
+              desc: file.name + " 文件无法以 " + "png/jpg/jpeg" + " 格式读取。"
+            });
+            this.loadingStatus = false;
+            reject("作答文件损坏");
+          }
+        }
+      };
+      const blob = file.slice(0, 4);
+      filereader.readAsArrayBuffer(blob);
+    },
     async handleBeforeUpload(file) {
+      const result = await new Promise((resolve, reject) =>
+        this.fileFormatCheck(file, resolve, reject)
+      );
+      if (result) {
+        // this.$Notice.warning({
+        //   title: `最多上传${MAX_UPLOADS_NUM}张照片。`
+        // });
+        return Promise.resolve(false);
+      }
       const MAX_UPLOADS_NUM = 6;
       const check = this.uploadList.length < MAX_UPLOADS_NUM;
       if (!check) {