MCPcopy Create free account
hub / github.com/gogs/gogs / UploadFileToServer

Function UploadFileToServer

internal/route/repo/editor.go:524–565  ·  view source on GitHub ↗
(c *context.Context)

Source from the content-addressed store, hash-verified

522}
523
524func 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
567func RemoveUploadFileFromServer(c *context.Context, f form.RemoveUploadFile) {
568 if f.File == "" {

Callers

nothing calls this directly

Calls 5

NewUploadFunction · 0.92
PlainTextMethod · 0.80
UserIDMethod · 0.80
JSONSuccessMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected