(c *context.Context)
| 522 | } |
| 523 | |
| 524 | func UploadFileToServer(c *context.Context) { |
| 525 | file, header, err := c.Req.FormFile("file") |
| 526 | if err != nil { |
| 527 | c.Error(err, "get file") |
| 528 | return |
| 529 | } |
| 530 | defer file.Close() |
| 531 | |
| 532 | buf := make([]byte, 1024) |
| 533 | n, _ := file.Read(buf) |
| 534 | if n > 0 { |
| 535 | buf = buf[:n] |
| 536 | } |
| 537 | fileType := http.DetectContentType(buf) |
| 538 | |
| 539 | if len(conf.Repository.Upload.AllowedTypes) > 0 { |
| 540 | allowed := false |
| 541 | for _, t := range conf.Repository.Upload.AllowedTypes { |
| 542 | t := strings.Trim(t, " ") |
| 543 | if t == "*/*" || t == fileType { |
| 544 | allowed = true |
| 545 | break |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if !allowed { |
| 550 | c.PlainText(http.StatusBadRequest, ErrFileTypeForbidden.Error()) |
| 551 | return |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | upload, err := database.NewUpload(header.Filename, buf, file) |
| 556 | if err != nil { |
| 557 | c.Error(err, "new upload") |
| 558 | return |
| 559 | } |
| 560 | |
| 561 | log.Trace("New file uploaded by user[%d]: %s", c.UserID(), upload.UUID) |
| 562 | c.JSONSuccess(map[string]string{ |
| 563 | "uuid": upload.UUID, |
| 564 | }) |
| 565 | } |
| 566 | |
| 567 | func RemoveUploadFileFromServer(c *context.Context, f form.RemoveUploadFile) { |
| 568 | if f.File == "" { |
nothing calls this directly
no test coverage detected