浏览代码

feature: 增加参数校验

chenqinghe 4 年前
父节点
当前提交
d6e423dcf3
共有 2 个文件被更改,包括 36 次插入6 次删除
  1. 10
    0
      cmd/webpprof/main.go
  2. 26
    6
      web/src/components/Upload.vue

+ 10
- 0
cmd/webpprof/main.go 查看文件

@@ -3,6 +3,7 @@ package main
3 3
 import (
4 4
 	"crypto/md5"
5 5
 	"encoding/hex"
6
+	"errors"
6 7
 	"fmt"
7 8
 	"io/ioutil"
8 9
 	"net/http"
@@ -37,6 +38,10 @@ func mapping(c *gin.Context) {
37 38
 	filename := fmt.Sprintf("%s.pprof", hash)
38 39
 	f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
39 40
 	if err != nil {
41
+		if os.IsNotExist(err) {
42
+			c.AbortWithStatus(http.StatusNotFound)
43
+			return
44
+		}
40 45
 		c.AbortWithError(http.StatusInternalServerError, err)
41 46
 		return
42 47
 	}
@@ -94,6 +99,11 @@ func upload(c *gin.Context) {
94 99
 		return
95 100
 	}
96 101
 
102
+	if _, err := profile.ParseData(fdata); err != nil {
103
+		c.AbortWithError(http.StatusBadRequest, errors.New("unrecongnized profile format"))
104
+		return
105
+	}
106
+
97 107
 	fileDst := fmt.Sprintf("%s.pprof", rhash)
98 108
 	file, err := os.Create(fileDst)
99 109
 	if err != nil {

+ 26
- 6
web/src/components/Upload.vue 查看文件

@@ -4,11 +4,13 @@
4 4
         class="upload-demo"
5 5
         drag
6 6
         action="/upload"
7
-        :on-success="uploadSuccess"
7
+        :on-progress="handleUploadProgress"
8
+        :on-success="handleUploadSuccess"
9
+        :on-error="handleUploadError"
8 10
     >
9 11
       <i class="el-icon-upload"></i>
10 12
       <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
11
-      <div class="el-upload__tip" slot="tip">上传文件不超过500kb</div>
13
+      <div class="el-upload__tip" slot="tip">拖动文件至此即可生成火焰图</div>
12 14
     </el-upload>
13 15
   </div>
14 16
 </template>
@@ -16,10 +18,28 @@
16 18
 <script>
17 19
 export default {
18 20
   name: "Upload",
19
-  methods:{
20
-    uploadSuccess(response,file,fileList){
21
-      console.log(response,file,fileList)
22
-      window.location.href="http://localhost:8090/pprof/"+response.data+"/dot"
21
+  data() {
22
+    return {
23
+      src: 'http://localhost:8090/pprof/bd5001ffcea7949ff66b7368b1912aa7/dot',
24
+      loading: null
25
+    }
26
+  },
27
+  methods: {
28
+    handleUploadSuccess(response) {
29
+      this.loading.close()
30
+      window.location.href = "http://localhost:8090/pprof/" + response.data + "/dot"
31
+    },
32
+    handleUploadError() {
33
+      this.loading.close()
34
+      this.$message.error("文件上传失败或文件格式错误")
35
+    },
36
+    handleUploadProgress() {
37
+      this.loading = this.$loading({
38
+        lock: true,
39
+        text: 'Loading',
40
+        spinner: 'el-icon-loading',
41
+        background: 'rgba(0, 0, 0, 0.7)'
42
+      });
23 43
     }
24 44
   }
25 45
 }