-
-
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
Add API for changing Avatars #25369
Changes from 1 commit
85bd24f
252b0a2
200dad8
b53c579
3e54a01
b474de6
59c8ada
c589738
3dca8f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 org | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/typesniffer" | ||
"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 | ||
// - 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 | ||
} | ||
|
||
if int64(len(content)) > setting.Avatar.MaxFileSize { | ||
ctx.Error(http.StatusBadRequest, "AvatarTooBig", fmt.Errorf("The avatar is to big")) | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also a height/width check (4000x4000 default). Can we maybe combine these checks so we don't have to duplicate them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All "pre-check" code for uploaded avatar can be removed. The UploadAvatar calls ProcessAvatarImage, which already handle type-check, size-limit, resizing, etc, correctly. |
||
|
||
st := typesniffer.DetectContentType(content) | ||
if !(st.IsImage() && !st.IsSvgImage()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we actually support SVG avatars on UI avatar upload, so it should not accept them here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code was just copied from the Web UI |
||
ctx.Error(http.StatusBadRequest, "NotAnImage", fmt.Errorf("The avatar is not an image")) | ||
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 | ||
// 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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/typesniffer" | ||
"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 | ||
} | ||
|
||
if int64(len(content)) > setting.Avatar.MaxFileSize { | ||
ctx.Error(http.StatusBadRequest, "AvatarTooBig", fmt.Errorf("The avatar is to big")) | ||
return | ||
} | ||
|
||
st := typesniffer.DetectContentType(content) | ||
if !(st.IsImage() && !st.IsSvgImage()) { | ||
ctx.Error(http.StatusBadRequest, "NotAnImage", fmt.Errorf("The avatar is not an image")) | ||
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package user | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/typesniffer" | ||
"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 | ||
} | ||
|
||
if int64(len(content)) > setting.Avatar.MaxFileSize { | ||
ctx.Error(http.StatusBadRequest, "AvatarTooBig", fmt.Errorf("The avatar is to big")) | ||
return | ||
} | ||
|
||
st := typesniffer.DetectContentType(content) | ||
if !(st.IsImage() && !st.IsSvgImage()) { | ||
ctx.Error(http.StatusBadRequest, "NotAnImage", fmt.Errorf("The avatar is not an image")) | ||
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) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.