MCPcopy
hub / github.com/gotify/server / UploadApplicationImage

Method UploadApplicationImage

api/application.go:325–377  ·  view source on GitHub ↗

UploadApplicationImage uploads an image for an application. swagger:operation POST /application/{id}/image application uploadAppImage Upload an image for an application. --- consumes: - multipart/form-data produces: [application/json] security: [clientTokenAuthorizationHeader: [], clientToken

(ctx *gin.Context)

Source from the content-addressed store, hash-verified

323// schema:
324// $ref: "#/definitions/Error"
325func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {
326 withID(ctx, "id", func(id uint) {
327 app, err := a.DB.GetApplicationByID(id)
328 if success := successOrAbort(ctx, 500, err); !success {
329 return
330 }
331 if app != nil && app.UserID == auth.GetUserID(ctx) {
332 file, err := ctx.FormFile("file")
333 if err == http.ErrMissingFile {
334 ctx.AbortWithError(400, errors.New("file with key 'file' must be present"))
335 return
336 } else if err != nil {
337 ctx.AbortWithError(500, err)
338 return
339 }
340 head := make([]byte, 261)
341 open, _ := file.Open()
342 open.Read(head)
343 if !filetype.IsImage(head) {
344 ctx.AbortWithError(400, errors.New("file must be an image"))
345 return
346 }
347
348 ext := filepath.Ext(file.Filename)
349 if !ValidApplicationImageExt(ext) {
350 ctx.AbortWithError(400, errors.New("invalid file extension"))
351 return
352 }
353
354 name := generateNonExistingImageName(a.ImageDir, func() string {
355 return generateImageName() + ext
356 })
357
358 err = ctx.SaveUploadedFile(file, a.ImageDir+name)
359 if err != nil {
360 ctx.AbortWithError(500, err)
361 return
362 }
363
364 if app.Image != "" {
365 os.Remove(a.ImageDir + app.Image)
366 }
367
368 app.Image = name
369 if success := successOrAbort(ctx, 500, a.DB.UpdateApplication(app)); !success {
370 return
371 }
372 ctx.JSON(200, withResolvedImage(app))
373 } else {
374 ctx.AbortWithError(404, fmt.Errorf("app with id %d doesn't exists", id))
375 }
376 })
377}
378
379// RemoveApplicationImage deletes an image of an application.
380// swagger:operation DELETE /application/{id}/image application removeAppImage

Calls 11

GetUserIDFunction · 0.92
withIDFunction · 0.85
successOrAbortFunction · 0.85
ValidApplicationImageExtFunction · 0.85
withResolvedImageFunction · 0.85
OpenMethod · 0.80
ReadMethod · 0.80
ErrorfMethod · 0.80
GetApplicationByIDMethod · 0.65
UpdateApplicationMethod · 0.65