gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package org
  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. // UpdateAvatarupdates the Avatar of an Organisation
  13. func UpdateAvatar(ctx *context.APIContext) {
  14. // swagger:operation POST /orgs/{org}/avatar organization orgUpdateAvatar
  15. // ---
  16. // summary: Update Avatar
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: org
  21. // in: path
  22. // description: name of the organization
  23. // type: string
  24. // required: true
  25. // - name: body
  26. // in: body
  27. // schema:
  28. // "$ref": "#/definitions/UpdateUserAvatarOption"
  29. // responses:
  30. // "204":
  31. // "$ref": "#/responses/empty"
  32. // "404":
  33. // "$ref": "#/responses/notFound"
  34. form := web.GetForm(ctx).(*api.UpdateUserAvatarOption)
  35. content, err := base64.StdEncoding.DecodeString(form.Image)
  36. if err != nil {
  37. ctx.APIError(http.StatusBadRequest, err)
  38. return
  39. }
  40. err = user_service.UploadAvatar(ctx, ctx.Org.Organization.AsUser(), content)
  41. if err != nil {
  42. ctx.APIErrorInternal(err)
  43. return
  44. }
  45. ctx.Status(http.StatusNoContent)
  46. }
  47. // DeleteAvatar deletes the Avatar of an Organisation
  48. func DeleteAvatar(ctx *context.APIContext) {
  49. // swagger:operation DELETE /orgs/{org}/avatar organization orgDeleteAvatar
  50. // ---
  51. // summary: Delete Avatar
  52. // produces:
  53. // - application/json
  54. // parameters:
  55. // - name: org
  56. // in: path
  57. // description: name of the organization
  58. // type: string
  59. // required: true
  60. // responses:
  61. // "204":
  62. // "$ref": "#/responses/empty"
  63. // "404":
  64. // "$ref": "#/responses/notFound"
  65. err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser())
  66. if err != nil {
  67. ctx.APIErrorInternal(err)
  68. return
  69. }
  70. ctx.Status(http.StatusNoContent)
  71. }