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

Method UploadPostFile

internal/service/uploader/upload.go:194–230  ·  view source on GitHub ↗
(ctx *gin.Context, userID string)

Source from the content-addressed store, hash-verified

192}
193
194func (us *uploaderService) UploadPostFile(ctx *gin.Context, userID string) (
195 url string, err error) {
196 url, err = us.tryToUploadByPlugin(ctx, plugin.UserPost)
197 if err != nil {
198 return "", err
199 }
200 if len(url) > 0 {
201 return url, nil
202 }
203
204 siteAdvanced, err := us.siteInfoService.GetSiteAdvanced(ctx)
205 if err != nil {
206 return "", err
207 }
208
209 ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, siteAdvanced.GetMaxImageSize())
210 file, fileHeader, err := ctx.Request.FormFile("file")
211 if err != nil {
212 return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
213 }
214 defer func() {
215 _ = file.Close()
216 }()
217 if checker.IsUnAuthorizedExtension(fileHeader.Filename, siteAdvanced.AuthorizedImageExtensions) {
218 return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
219 }
220
221 fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
222 newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
223 avatarFilePath := path.Join(constant.PostSubPath, newFilename)
224 url, err = us.uploadImageFile(ctx, fileHeader, avatarFilePath)
225 if err != nil {
226 return "", err
227 }
228 us.fileRecordService.AddFileRecord(ctx, userID, avatarFilePath, url, string(plugin.UserPost))
229 return url, nil
230}
231
232func (us *uploaderService) UploadPostAttachment(ctx *gin.Context, userID string) (
233 url string, err error) {

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected