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

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     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  	repo_service "code.gitea.io/gitea/services/repository"
    14  )
    15  
    16  // UpdateVatar updates the Avatar of an Repo
    17  func UpdateAvatar(ctx *context.APIContext) {
    18  	// swagger:operation POST /repos/{owner}/{repo}/avatar repository repoUpdateAvatar
    19  	// ---
    20  	// summary: Update avatar
    21  	// produces:
    22  	// - application/json
    23  	// parameters:
    24  	// - name: owner
    25  	//   in: path
    26  	//   description: owner of the repo
    27  	//   type: string
    28  	//   required: true
    29  	// - name: repo
    30  	//   in: path
    31  	//   description: name of the repo
    32  	//   type: string
    33  	//   required: true
    34  	// - name: body
    35  	//   in: body
    36  	//   schema:
    37  	//     "$ref": "#/definitions/UpdateRepoAvatarOption"
    38  	// responses:
    39  	//   "204":
    40  	//     "$ref": "#/responses/empty"
    41  	//   "404":
    42  	//     "$ref": "#/responses/notFound"
    43  	form := web.GetForm(ctx).(*api.UpdateRepoAvatarOption)
    44  
    45  	content, err := base64.StdEncoding.DecodeString(form.Image)
    46  	if err != nil {
    47  		ctx.Error(http.StatusBadRequest, "DecodeImage", err)
    48  		return
    49  	}
    50  
    51  	err = repo_service.UploadAvatar(ctx, ctx.Repo.Repository, content)
    52  	if err != nil {
    53  		ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
    54  	}
    55  
    56  	ctx.Status(http.StatusNoContent)
    57  }
    58  
    59  // UpdateAvatar deletes the Avatar of an Repo
    60  func DeleteAvatar(ctx *context.APIContext) {
    61  	// swagger:operation DELETE /repos/{owner}/{repo}/avatar repository repoDeleteAvatar
    62  	// ---
    63  	// summary: Delete avatar
    64  	// produces:
    65  	// - application/json
    66  	// parameters:
    67  	// - name: owner
    68  	//   in: path
    69  	//   description: owner of the repo
    70  	//   type: string
    71  	//   required: true
    72  	// - name: repo
    73  	//   in: path
    74  	//   description: name of the repo
    75  	//   type: string
    76  	//   required: true
    77  	// responses:
    78  	//   "204":
    79  	//     "$ref": "#/responses/empty"
    80  	//   "404":
    81  	//     "$ref": "#/responses/notFound"
    82  	err := repo_service.DeleteAvatar(ctx, ctx.Repo.Repository)
    83  	if err != nil {
    84  		ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
    85  	}
    86  
    87  	ctx.Status(http.StatusNoContent)
    88  }