-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add API for changing Avatars #25369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add API for changing Avatars #25369
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
85bd24f
Add API for changing Avatars
JakobDev 252b0a2
Remove checks from API code
JakobDev 200dad8
Merge branch 'main' into avatarapi
silverwind b53c579
Merge branch 'main' into avatarapi
GiteaBot 3e54a01
Merge branch 'main' into avatarapi
GiteaBot b474de6
Update tests
JakobDev 59c8ada
Fix Swagger
JakobDev c589738
Merge branch 'main' into avatarapi
silverwind 3dca8f9
Merge branch 'main' into avatarapi
silverwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package org | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/web" | ||
user_service "code.gitea.io/gitea/services/user" | ||
) | ||
|
||
// UpdateAvatarupdates the Avatar of an Organisation | ||
func UpdateAvatar(ctx *context.APIContext) { | ||
// swagger:operation POST /orgs/{org}/avatar organization orgUpdateAvatar | ||
// --- | ||
// summary: Update Avatar | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: org | ||
// in: path | ||
// description: name of the organization | ||
// type: string | ||
// required: true | ||
// - name: body | ||
// in: body | ||
// schema: | ||
// "$ref": "#/definitions/UpdateUserAvatarOption" | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
form := web.GetForm(ctx).(*api.UpdateUserAvatarOption) | ||
|
||
content, err := base64.StdEncoding.DecodeString(form.Image) | ||
if err != nil { | ||
ctx.Error(http.StatusBadRequest, "DecodeImage", err) | ||
return | ||
} | ||
|
||
err = user_service.UploadAvatar(ctx.Org.Organization.AsUser(), content) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err) | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} | ||
|
||
// DeleteAvatar deletes the Avatar of an Organisation | ||
func DeleteAvatar(ctx *context.APIContext) { | ||
// swagger:operation DELETE /orgs/{org}/avatar organization orgDeleteAvatar | ||
// --- | ||
// summary: Delete Avatar | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: org | ||
// in: path | ||
// description: name of the organization | ||
// type: string | ||
// required: true | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
err := user_service.DeleteAvatar(ctx.Org.Organization.AsUser()) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err) | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/web" | ||
repo_service "code.gitea.io/gitea/services/repository" | ||
) | ||
|
||
// UpdateVatar updates the Avatar of an Repo | ||
func UpdateAvatar(ctx *context.APIContext) { | ||
// swagger:operation POST /repos/{owner}/{repo}/avatar repository repoUpdateAvatar | ||
// --- | ||
// summary: Update avatar | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: owner | ||
// in: path | ||
// description: owner of the repo | ||
// type: string | ||
// required: true | ||
// - name: repo | ||
// in: path | ||
// description: name of the repo | ||
// type: string | ||
// required: true | ||
// - name: body | ||
// in: body | ||
// schema: | ||
// "$ref": "#/definitions/UpdateRepoAvatarOption" | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
form := web.GetForm(ctx).(*api.UpdateRepoAvatarOption) | ||
|
||
content, err := base64.StdEncoding.DecodeString(form.Image) | ||
if err != nil { | ||
ctx.Error(http.StatusBadRequest, "DecodeImage", err) | ||
return | ||
} | ||
|
||
err = repo_service.UploadAvatar(ctx, ctx.Repo.Repository, content) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err) | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} | ||
|
||
// UpdateAvatar deletes the Avatar of an Repo | ||
func DeleteAvatar(ctx *context.APIContext) { | ||
// swagger:operation DELETE /repos/{owner}/{repo}/avatar repository repoDeleteAvatar | ||
// --- | ||
// summary: Delete avatar | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: owner | ||
// in: path | ||
// description: owner of the repo | ||
// type: string | ||
// required: true | ||
// - name: repo | ||
// in: path | ||
// description: name of the repo | ||
// type: string | ||
// required: true | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
err := repo_service.DeleteAvatar(ctx, ctx.Repo.Repository) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err) | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package user | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/web" | ||
user_service "code.gitea.io/gitea/services/user" | ||
) | ||
|
||
// UpdateAvatar updates the Avatar of an User | ||
func UpdateAvatar(ctx *context.APIContext) { | ||
// swagger:operation POST /user/avatar user userUpdateAvatar | ||
// --- | ||
// summary: Update Avatar | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: body | ||
// in: body | ||
// schema: | ||
// "$ref": "#/definitions/UpdateUserAvatarOption" | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
form := web.GetForm(ctx).(*api.UpdateUserAvatarOption) | ||
|
||
content, err := base64.StdEncoding.DecodeString(form.Image) | ||
if err != nil { | ||
ctx.Error(http.StatusBadRequest, "DecodeImage", err) | ||
return | ||
} | ||
|
||
err = user_service.UploadAvatar(ctx.Doer, content) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err) | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} | ||
|
||
// DeleteAvatar deletes the Avatar of an User | ||
func DeleteAvatar(ctx *context.APIContext) { | ||
// swagger:operation DELETE /user/avatar user userDeleteAvatar | ||
// --- | ||
// summary: Delete Avatar | ||
// produces: | ||
// - application/json | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
err := user_service.DeleteAvatar(ctx.Doer) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err) | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.