code.gitea.io/gitea@v1.22.3/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  	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  // 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, ctx.Org.Organization.AsUser(), content)
    47  	if err != nil {
    48  		ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
    49  		return
    50  	}
    51  
    52  	ctx.Status(http.StatusNoContent)
    53  }
    54  
    55  // DeleteAvatar deletes the Avatar of an Organisation
    56  func DeleteAvatar(ctx *context.APIContext) {
    57  	// swagger:operation DELETE /orgs/{org}/avatar organization orgDeleteAvatar
    58  	// ---
    59  	// summary: Delete Avatar
    60  	// produces:
    61  	// - application/json
    62  	// parameters:
    63  	// - name: org
    64  	//   in: path
    65  	//   description: name of the organization
    66  	//   type: string
    67  	//   required: true
    68  	// responses:
    69  	//   "204":
    70  	//     "$ref": "#/responses/empty"
    71  	//   "404":
    72  	//     "$ref": "#/responses/notFound"
    73  	err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser())
    74  	if err != nil {
    75  		ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
    76  		return
    77  	}
    78  
    79  	ctx.Status(http.StatusNoContent)
    80  }