gitea源码

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