-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
280 lines (273 loc) · 8.03 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"code.videolan.org/videolan/CrashDragon/config"
"code.videolan.org/videolan/CrashDragon/database"
"code.videolan.org/videolan/CrashDragon/processor"
"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
uuid "github.com/satori/go.uuid"
)
// PostReportCrashID allows you to change the crash id of a crashreport
func PostReportCrashID(c *gin.Context) {
var Report database.Report
database.Db.First(&Report, "id = ?", c.Param("id"))
id, err := uuid.FromString(c.PostForm("crashid"))
if err != nil {
c.AbortWithStatus(http.StatusPreconditionFailed)
return
}
Report.CrashID = id
database.Db.Save(&Report)
c.Redirect(http.StatusMovedPermanently, "/reports/"+Report.ID.String())
}
// PostReportComment allows you to post a comment to a crashreport
func PostReportComment(c *gin.Context) {
User := c.MustGet("user").(database.User)
var Report database.Report
database.Db.First(&Report, "id = ?", c.Param("id"))
if Report.ID == uuid.Nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
var Comment database.Comment
database.Db.FirstOrInit(&Comment)
Comment.UserID = User.ID
Comment.ID = uuid.NewV4()
unsafe := blackfriday.MarkdownCommon([]byte(c.PostForm("comment")))
Comment.Content = template.HTML(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
if len(strings.TrimSpace(string(Comment.Content))) == 0 {
c.Redirect(http.StatusMovedPermanently, "/reports/"+Report.ID.String())
}
Comment.ReportID = Report.ID
Comment.CrashID = uuid.Nil
database.Db.Create(&Comment)
c.Redirect(http.StatusMovedPermanently, "/reports/"+Report.ID.String()+"#comment-"+Comment.ID.String())
}
// GetReports returns crashreports
func GetReports(c *gin.Context) {
var Reports []database.Report
var List []struct {
ID string
Signature string
Date time.Time
Product string
Version string
Platform string
Reason string
Location string
GitRepo string
File string
Line int
}
query := database.Db
all, prod := GetProductCookie(c)
if !all {
query = query.Where("product_id = ?", prod.ID)
}
if sig := c.Query("signature"); sig != "" {
query = query.Where("signature = ?", sig)
}
if ver := c.Query("version"); ver != "" {
query = query.Where("version = ?", ver)
}
if platform := c.Query("platform"); platform != "" {
platforms := strings.Split(platform, ",")
var filter []string
for _, os := range platforms {
if os == "Mac OS X" {
filter = append(filter, "'Mac OS X'")
} else if os == "Windows" {
filter = append(filter, "'Windows'")
} else if os == "Linux" {
filter = append(filter, "'Linux'")
}
}
if len(filter) > 0 {
whereQuery := strings.Join(filter, ", ")
query = query.Where("os IN (" + whereQuery + ")")
}
}
if reason := c.Query("reason"); reason != "" {
query = query.Where("reason = ?", reason)
}
offset, err := strconv.Atoi(c.DefaultQuery("offset", "0"))
if err != nil {
offset = 0
}
query = query.Where("processed = true")
var count int
query.Model(database.Report{}).Count(&count)
query.Order("created_at DESC").Offset(offset).Limit(50).Preload("Product").Preload("Version").Find(&Reports)
var next int
var prev int
if (offset + 50) >= count {
next = -1
} else {
next = offset + 50
}
prev = offset - 50
for _, Report := range Reports {
var Item struct {
ID string
Signature string
Date time.Time
Product string
Version string
Platform string
Reason string
Location string
GitRepo string
File string
Line int
}
Item.ID = Report.ID.String()
Item.Date = Report.CreatedAt
Item.Product = Report.Product.Name
Item.Version = Report.Version.Name
Item.Platform = Report.Os
Item.Reason = Report.Report.CrashInfo.Type
Item.Signature = Report.Signature
Item.Location = Report.CrashLocation
Item.GitRepo = Report.Version.GitRepo
Item.File = Report.CrashPath
Item.Line = Report.CrashLine
List = append(List, Item)
}
if strings.HasPrefix(c.Request.Header.Get("Accept"), "text/html") {
c.HTML(http.StatusOK, "reports.html", gin.H{
"prods": database.Products,
"title": "Reports",
"items": List,
"nextOffset": next,
"prevOffset": prev,
})
} else {
c.JSON(http.StatusOK, List)
}
}
// GetReport returns details of crashreport
func GetReport(c *gin.Context) {
var Report database.Report
database.Db.Preload("Product").Preload("Version").First(&Report, "id = ?", c.Param("id")).Order("created_at DESC")
database.Db.Model(&Report).Preload("User").Order("created_at ASC").Related(&Report.Comments)
var Item struct {
ID string
CrashID string
Signature string
Date time.Time
Product string
Version string
Platform string
Arch string
Processor string
Reason string
Comment string
Uptime string
File string
Line int
GitRepo string
Location string
ProcessingTime float64
}
Item.ID = Report.ID.String()
Item.CrashID = Report.CrashID.String()
Item.Signature = Report.Signature
Item.Date = Report.CreatedAt
Item.Product = Report.Product.Name
Item.Version = Report.Version.Name
Item.Platform = Report.Os + " " + Report.OsVersion
Item.Arch = Report.Arch
Item.Processor = Report.Report.SystemInfo.CPUInfo + " (" + strconv.Itoa(Report.Report.SystemInfo.CPUCount) + " cores)"
Item.Reason = Report.Report.CrashInfo.Type
Item.Comment = Report.Comment
h := (Report.ProcessUptime / 3600000) % 24
m := (Report.ProcessUptime / 60000) % 60
s := (Report.ProcessUptime / 1000) % 60
Item.Uptime = fmt.Sprintf("%02d:%02d:%02d", h, m, s)
Item.GitRepo = Report.Version.GitRepo
Item.File = Report.CrashPath
Item.Line = Report.CrashLine
Item.Location = Report.CrashLocation
Item.ProcessingTime = Report.ProcessingTime
result, _ := c.Cookie("result")
if result != "" {
c.SetCookie("result", "", 1, "/", "", false, false)
}
if strings.HasPrefix(c.Request.Header.Get("Accept"), "text/html") {
c.HTML(http.StatusOK, "report.html", gin.H{
"prods": database.Products,
"detailView": true,
"title": "Report",
"item": Item,
"report": Report.Report,
"result": result,
"comments": Report.Comments,
})
} else {
c.JSON(http.StatusOK, Report)
}
}
// GetReportFile returns minidump file of crashreport
func GetReportFile(c *gin.Context) {
var Report database.Report
if err := database.Db.Where("id = ?", c.Param("id")).First(&Report).Error; err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
name := c.Param("name")
switch name {
case "upload_file_minidump":
file := path.Join(config.C.ContentDirectory, "Reports", Report.ID.String()[0:2], Report.ID.String()[0:4], Report.ID.String()+".dmp")
f, err := os.Open(file)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Header("Content-Disposition", "attachment; filename=\""+Report.ID.String()+".dmp\"")
c.Data(http.StatusOK, "application/octet-stream", data)
return
case "processed_json":
c.Header("Content-Disposition", "attachment; filename=\""+Report.ID.String()+".json\"")
c.Data(http.StatusOK, "application/json", []byte(Report.ReportContentJSON))
return
case "processed_txt":
file := path.Join(config.C.ContentDirectory, "TXT", Report.ID.String()[0:2], Report.ID.String()[0:4], Report.ID.String()+".txt")
f, err := os.Open(file)
if os.IsNotExist(err) {
processor.ProcessText(&Report)
f, err = os.Open(file)
}
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Data(http.StatusOK, "text/plain", data)
return
default:
c.AbortWithError(http.StatusBadRequest, errors.New(name+" is a unknwon file"))
return
}
}