UploadAvatarFile upload avatar file
(ctx *gin.Context, userID string)
| 101 | |
| 102 | // UploadAvatarFile upload avatar file |
| 103 | func (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 | |
| 140 | func (us *uploaderService) AvatarThumbFile(ctx *gin.Context, fileName string, size int) (url string, err error) { |
| 141 | fileSuffix := path.Ext(fileName) |
nothing calls this directly
no test coverage detected