code.gitea.io/gitea@v1.22.3/routers/api/v1/user/avatar.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package user 5 6 import ( 7 "encoding/base64" 8 "net/http" 9 10 api "code.gitea.io/gitea/modules/structs" 11 "code.gitea.io/gitea/modules/web" 12 "code.gitea.io/gitea/services/context" 13 user_service "code.gitea.io/gitea/services/user" 14 ) 15 16 // UpdateAvatar updates the Avatar of an User 17 func UpdateAvatar(ctx *context.APIContext) { 18 // swagger:operation POST /user/avatar user userUpdateAvatar 19 // --- 20 // summary: Update Avatar 21 // produces: 22 // - application/json 23 // parameters: 24 // - name: body 25 // in: body 26 // schema: 27 // "$ref": "#/definitions/UpdateUserAvatarOption" 28 // responses: 29 // "204": 30 // "$ref": "#/responses/empty" 31 form := web.GetForm(ctx).(*api.UpdateUserAvatarOption) 32 33 content, err := base64.StdEncoding.DecodeString(form.Image) 34 if err != nil { 35 ctx.Error(http.StatusBadRequest, "DecodeImage", err) 36 return 37 } 38 39 err = user_service.UploadAvatar(ctx, ctx.Doer, content) 40 if err != nil { 41 ctx.Error(http.StatusInternalServerError, "UploadAvatar", err) 42 return 43 } 44 45 ctx.Status(http.StatusNoContent) 46 } 47 48 // DeleteAvatar deletes the Avatar of an User 49 func DeleteAvatar(ctx *context.APIContext) { 50 // swagger:operation DELETE /user/avatar user userDeleteAvatar 51 // --- 52 // summary: Delete Avatar 53 // produces: 54 // - application/json 55 // responses: 56 // "204": 57 // "$ref": "#/responses/empty" 58 err := user_service.DeleteAvatar(ctx, ctx.Doer) 59 if err != nil { 60 ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err) 61 return 62 } 63 64 ctx.Status(http.StatusNoContent) 65 }