From 5b6f7b001f62636431e832812589faf22f0ad417 Mon Sep 17 00:00:00 2001 From: "Helmut K. C. Tessarek" Date: Wed, 24 May 2023 18:10:05 -0400 Subject: [PATCH] add API endpoint to remove a app's image --- api/application.go | 24 ++++++++++++++++++++++++ router/router.go | 2 ++ 2 files changed, 26 insertions(+) diff --git a/api/application.go b/api/application.go index 142809cc..de484501 100644 --- a/api/application.go +++ b/api/application.go @@ -358,6 +358,30 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) { }) } +func (a *ApplicationAPI) RemoveApplicationImage(ctx *gin.Context) { + withID(ctx, "id", func(id uint) { + app, err := a.DB.GetApplicationByID(id) + if success := successOrAbort(ctx, 500, err); !success { + return + } + if app != nil && app.UserID == auth.GetUserID(ctx) { + if app.Image == "" { + ctx.AbortWithError(400, fmt.Errorf("app with id %d does not have a customized image", id)) + return + } + + app.Image = "" + if success := successOrAbort(ctx, 500, a.DB.UpdateApplication(app)); !success { + return + } + os.Remove(a.ImageDir + app.Image) + ctx.JSON(200, withResolvedImage(app)) + } else { + ctx.AbortWithError(404, fmt.Errorf("app with id %d doesn't exists", id)) + } + }) +} + func withResolvedImage(app *model.Application) *model.Application { if app.Image == "" { app.Image = "static/defaultapp.png" diff --git a/router/router.go b/router/router.go index 56b6bf45..d2a77b27 100644 --- a/router/router.go +++ b/router/router.go @@ -119,6 +119,8 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co app.POST("/:id/image", applicationHandler.UploadApplicationImage) + app.DELETE("/:id/image", applicationHandler.RemoveApplicationImage) + app.PUT("/:id", applicationHandler.UpdateApplication) app.DELETE("/:id", applicationHandler.DeleteApplication)