Add upload avatar provider
This commit is contained in:
parent
b764ba73df
commit
69d935f7f0
|
@ -23,6 +23,7 @@ import (
|
|||
|
||||
type user20200801183357 struct {
|
||||
AvatarProvider string `xorm:"varchar(255) null" json:"-"`
|
||||
AvatarFileID int64 `xorn:"null" json:"-"`
|
||||
}
|
||||
|
||||
func (s user20200801183357) TableName() string {
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
// Vikunja is a to-do list application to facilitate your life.
|
||||
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package upload
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
func (p *Provider) GetAvatar(u *user.User, size int64) (avatar []byte, mimeType string, err error) {
|
||||
f := &files.File{ID: u.AvatarFileID}
|
||||
if err := f.LoadFileByID(); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
avatar, err = ioutil.ReadAll(f.File)
|
||||
return avatar, f.Mime, err
|
||||
}
|
|
@ -17,12 +17,15 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/models"
|
||||
"code.vikunja.io/api/pkg/modules/avatar"
|
||||
"code.vikunja.io/api/pkg/modules/avatar/empty"
|
||||
"code.vikunja.io/api/pkg/modules/avatar/gravatar"
|
||||
"code.vikunja.io/api/pkg/modules/avatar/initials"
|
||||
user2 "code.vikunja.io/api/pkg/user"
|
||||
"code.vikunja.io/api/pkg/modules/avatar/upload"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"code.vikunja.io/web/handler"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
|
@ -45,7 +48,7 @@ func GetAvatar(c echo.Context) error {
|
|||
username := c.Param("username")
|
||||
|
||||
// Get the user
|
||||
user, err := user2.GetUserWithEmail(&user2.User{Username: username})
|
||||
u, err := user.GetUserWithEmail(&user.User{Username: username})
|
||||
if err != nil {
|
||||
log.Errorf("Error getting user for avatar: %v", err)
|
||||
return handler.HandleHTTPError(err, c)
|
||||
|
@ -55,11 +58,13 @@ func GetAvatar(c echo.Context) error {
|
|||
// For now, we only have one avatar provider, in the future there could be multiple which
|
||||
// could be changed based on user settings etc.
|
||||
var avatarProvider avatar.Provider
|
||||
switch user.AvatarProvider {
|
||||
switch u.AvatarProvider {
|
||||
case "gravatar":
|
||||
avatarProvider = &gravatar.Provider{}
|
||||
case "initials":
|
||||
avatarProvider = &initials.Provider{}
|
||||
case "upload":
|
||||
avatarProvider = &upload.Provider{}
|
||||
default:
|
||||
avatarProvider = &empty.Provider{}
|
||||
}
|
||||
|
@ -75,11 +80,47 @@ func GetAvatar(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Get the avatar
|
||||
a, mimeType, err := avatarProvider.GetAvatar(user, sizeInt)
|
||||
a, mimeType, err := avatarProvider.GetAvatar(u, sizeInt)
|
||||
if err != nil {
|
||||
log.Errorf("Error getting avatar for user %d: %v", user.ID, err)
|
||||
log.Errorf("Error getting avatar for user %d: %v", u.ID, err)
|
||||
return handler.HandleHTTPError(err, c)
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK, mimeType, a)
|
||||
}
|
||||
|
||||
func UploadAvatar(c echo.Context) (err error) {
|
||||
|
||||
u, err := user.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return handler.HandleHTTPError(err, c)
|
||||
}
|
||||
|
||||
// Get + upload the image
|
||||
file, err := c.FormFile("avatar")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
f, err := files.Create(src, file.Filename, uint64(file.Size), u)
|
||||
if err != nil {
|
||||
if files.IsErrFileIsTooLarge(err) {
|
||||
return echo.ErrBadRequest
|
||||
}
|
||||
|
||||
return handler.HandleHTTPError(err, c)
|
||||
}
|
||||
|
||||
u.AvatarFileID = f.ID
|
||||
|
||||
if _, err := user.UpdateUser(u); err != nil {
|
||||
return handler.HandleHTTPError(err, c)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models.Message{Message: "Avatar was uploaded successfully."})
|
||||
}
|
||||
|
|
|
@ -256,6 +256,7 @@ func registerAPIRoutes(a *echo.Group) {
|
|||
u.POST("/settings/email", apiv1.UpdateUserEmail)
|
||||
u.GET("/settings/avatar", apiv1.GetUserAvatarProvider)
|
||||
u.POST("/settings/avatar", apiv1.ChangeUserAvatarProvider)
|
||||
u.PUT("/settings/avatar/upload", apiv1.UploadAvatar)
|
||||
|
||||
if config.ServiceEnableTotp.GetBool() {
|
||||
u.GET("/settings/totp", apiv1.UserTOTP)
|
||||
|
|
|
@ -56,6 +56,7 @@ type User struct {
|
|||
EmailConfirmToken string `xorm:"varchar(450) null" json:"-"`
|
||||
|
||||
AvatarProvider string `xorm:"varchar(255) null" json:"-"`
|
||||
AvatarFileID int64 `xorn:"null" json:"-"`
|
||||
|
||||
// A timestamp when this task was created. You cannot change this value.
|
||||
Created time.Time `xorm:"created not null" json:"created"`
|
||||
|
|
Loading…
Reference in New Issue