Parcourir la source

判断文件格式

xyxie il y a 6 mois
Parent
commit
443a41077e
1 fichiers modifiés avec 5 ajouts et 2 suppressions
  1. 5 2
      controllers/knowledge/resource.go

+ 5 - 2
controllers/knowledge/resource.go

@@ -1010,8 +1010,11 @@ func (this *ResourceController) ResourceUpload() {
 		br.ErrMsg = "读取文件失败, Err: " + e.Error()
 		return
 	}
+	ext := path.Ext(h.Filename)
+	//fmt.Println("ext", ext)
 	kind, _ := filetype.Match(fileData)
-	if kind.Extension != "pdf" && kind.Extension != "ppt" && kind.Extension != "pptx" && kind.Extension != "docx" && kind.Extension != "doc" {
+	//fmt.Println("kind.Extension", kind.Extension)
+	if ext != ".pdf" && ext != ".ppt" && ext != ".pptx" && ext != ".docx" && ext != ".doc" && kind.Extension != "pdf" && kind.Extension != "ppt" && kind.Extension != "pptx" && kind.Extension != "docx" && kind.Extension != "doc" {
 		br.Msg = "文件格式有误"
 		return
 	}
@@ -1022,7 +1025,7 @@ func (this *ResourceController) ResourceUpload() {
 		br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
 		return
 	}
-	ext := path.Ext(h.Filename)
+
 	ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
 	filePath := uploadDir + "/" + ossFileName
 	if e = this.SaveToFile("File", filePath); e != nil {