MCPcopy Index your code
hub / github.com/apache/answer / UploadAvatarFile

Method UploadAvatarFile

internal/service/uploader/upload.go:103–138  ·  view source on GitHub ↗

UploadAvatarFile upload avatar file

(ctx *gin.Context, userID string)

Source from the content-addressed store, hash-verified

101
102// UploadAvatarFile upload avatar file
103func (us *uploaderService) UploadAvatarFile(ctx *gin.Context, userID string) (url string, err error) {
104 url, err = us.tryToUploadByPlugin(ctx, plugin.UserAvatar)
105 if err != nil {
106 return "", err
107 }
108 if len(url) > 0 {
109 return url, nil
110 }
111
112 siteAdvanced, err := us.siteInfoService.GetSiteAdvanced(ctx)
113 if err != nil {
114 return "", err
115 }
116
117 ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, siteAdvanced.GetMaxImageSize())
118 file, fileHeader, err := ctx.Request.FormFile("file")
119 if err != nil {
120 return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
121 }
122 defer func() {
123 _ = file.Close()
124 }()
125 fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
126 if _, ok := plugin.DefaultFileTypeCheckMapping[plugin.UserAvatar][fileExt]; !ok {
127 return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
128 }
129
130 newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
131 avatarFilePath := path.Join(constant.AvatarSubPath, newFilename)
132 url, err = us.uploadImageFile(ctx, fileHeader, avatarFilePath)
133 if err != nil {
134 return "", err
135 }
136 us.fileRecordService.AddFileRecord(ctx, userID, avatarFilePath, url, string(plugin.UserAvatar))
137 return url, nil
138}
139
140func (us *uploaderService) AvatarThumbFile(ctx *gin.Context, fileName string, size int) (url string, err error) {
141 fileSuffix := path.Ext(fileName)

Callers

nothing calls this directly

Calls 7

tryToUploadByPluginMethod · 0.95
uploadImageFileMethod · 0.95
IDStr12Function · 0.92
GetMaxImageSizeMethod · 0.80
GetSiteAdvancedMethod · 0.65
CloseMethod · 0.65
AddFileRecordMethod · 0.65

Tested by

no test coverage detected