code.gitea.io/gitea@v1.21.7/routers/api/v1/org/avatar.go (about)

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