MCPcopy
hub / github.com/apache/answer / UploadFile

Method UploadFile

internal/controller/upload_controller.go:66–96  ·  view source on GitHub ↗

UploadFile upload file @Summary upload file @Description upload file @Tags Upload @Accept multipart/form-data @Security ApiKeyAuth @Param source formData string true "identify the source of the file upload" Enums(post, post_attachment, avatar, branding) @Param file formData file true "file" @Success

(ctx *gin.Context)

Source from the content-addressed store, hash-verified

64// @Success 200 {object} handler.RespBody{data=string}
65// @Router /answer/api/v1/file [post]
66func (uc *UploadController) UploadFile(ctx *gin.Context) {
67 var (
68 url string
69 err error
70 )
71
72 source := ctx.PostForm("source")
73 userID := middleware.GetLoginUserIDFromContext(ctx)
74 switch source {
75 case fileFromAvatar:
76 url, err = uc.uploaderService.UploadAvatarFile(ctx, userID)
77 case fileFromPost:
78 url, err = uc.uploaderService.UploadPostFile(ctx, userID)
79 case fileFromBranding:
80 if !middleware.GetIsAdminFromContext(ctx) {
81 handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
82 return
83 }
84 url, err = uc.uploaderService.UploadBrandingFile(ctx, userID)
85 case fileFromPostAttachment:
86 url, err = uc.uploaderService.UploadPostAttachment(ctx, userID)
87 default:
88 handler.HandleResponse(ctx, errors.BadRequest(reason.UploadFileSourceUnsupported), nil)
89 return
90 }
91 if err != nil {
92 handler.HandleResponse(ctx, err, nil)
93 return
94 }
95 handler.HandleResponse(ctx, err, url)
96}
97
98// PostRender render post content
99// @Summary render post content

Callers

nothing calls this directly

Calls 7

GetIsAdminFromContextFunction · 0.92
HandleResponseFunction · 0.92
UploadAvatarFileMethod · 0.65
UploadPostFileMethod · 0.65
UploadBrandingFileMethod · 0.65
UploadPostAttachmentMethod · 0.65

Tested by

no test coverage detected