diff --git a/pkg/config/config.go b/pkg/config/config.go index 1c0dc8d2a..38cd8fe88 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -115,6 +115,7 @@ const ( AvatarGravaterExpiration Key = `avatar.gravatarexpiration` BackgroundsEnabled Key = `backgrounds.enabled` + BackgroundsUploadEnabled Key = `backgrounds.providers.upload.enabled` BackgroundsUnsplashEnabled Key = `backgrounds.providers.unsplash.enabled` BackgroundsUnsplashAccessToken Key = `backgrounds.providers.unsplash.accesstoken` BackgroundsUnsplashApplicationID Key = `backgrounds.providers.unsplash.applicationid` @@ -252,6 +253,7 @@ func InitDefaultConfig() { AvatarGravaterExpiration.setDefault(3600) // List Backgrounds BackgroundsEnabled.setDefault(false) + BackgroundsUploadEnabled.setDefault(false) BackgroundsUnsplashEnabled.setDefault(false) } diff --git a/pkg/modules/background/handler/background.go b/pkg/modules/background/handler/background.go index 58a094cc7..b6ce576fe 100644 --- a/pkg/modules/background/handler/background.go +++ b/pkg/modules/background/handler/background.go @@ -23,6 +23,7 @@ import ( "code.vikunja.io/api/pkg/modules/background" "code.vikunja.io/api/pkg/modules/background/unsplash" v1 "code.vikunja.io/api/pkg/routes/api/v1" + "code.vikunja.io/web" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" "net/http" @@ -62,35 +63,42 @@ func (bp *BackgroundProvider) SearchBackgrounds(c echo.Context) error { return c.JSON(http.StatusOK, result) } -// SetBackground sets an Image as list background -func (bp *BackgroundProvider) SetBackground(c echo.Context) error { - auth, err := v1.GetAuthFromClaims(c) +// This function does all kinds of preparations for setting and uploading a background +func (bp *BackgroundProvider) setBackgroundPreparations(c echo.Context) (list *models.List, auth web.Auth, err error) { + auth, err = v1.GetAuthFromClaims(c) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid auth token: "+err.Error()) + return nil, nil, echo.NewHTTPError(http.StatusBadRequest, "Invalid auth token: "+err.Error()) } - p := bp.Provider() - listID, err := strconv.ParseInt(c.Param("list"), 10, 64) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid list ID: "+err.Error()) + return nil, nil, echo.NewHTTPError(http.StatusBadRequest, "Invalid list ID: "+err.Error()) } // Check if the user has the right to change the list background - list := &models.List{ID: listID} + list = &models.List{ID: listID} can, err := list.CanUpdate(auth) if err != nil { - return handler.HandleHTTPError(err, c) + return } if !can { log.Infof("Tried to update list background of list %d while not having the rights for it (User: %v)", listID, auth) - return echo.NewHTTPError(http.StatusForbidden) + return list, auth, models.ErrGenericForbidden{} } // Load the list - if err := list.GetSimpleByID(); err != nil { + err = list.GetSimpleByID() + return +} + +// SetBackground sets an Image as list background +func (bp *BackgroundProvider) SetBackground(c echo.Context) error { + list, auth, err := bp.setBackgroundPreparations(c) + if err != nil { return handler.HandleHTTPError(err, c) } + p := bp.Provider() + image := &background.Image{} err = c.Bind(image) if err != nil { @@ -104,6 +112,26 @@ func (bp *BackgroundProvider) SetBackground(c echo.Context) error { return c.JSON(http.StatusOK, list) } +// UploadBackground uploads a background and passes the id of the uploaded file as an Image to the Set function of the BackgroundProvider. +func (bp *BackgroundProvider) UploadBackground(c echo.Context) error { + list, auth, err := bp.setBackgroundPreparations(c) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + p := bp.Provider() + + // Get + upload the image + + image := &background.Image{ID: "1"} + + err = p.Set(image, list, auth) + if err != nil { + return handler.HandleHTTPError(err, c) + } + return c.JSON(http.StatusOK, list) +} + // GetListBackground serves a previously set background from a list // It has no knowledge of the provider that was responsible for setting the background. // @Summary Get the list background diff --git a/pkg/modules/background/upload/upload.go b/pkg/modules/background/upload/upload.go new file mode 100644 index 000000000..60b555b67 --- /dev/null +++ b/pkg/modules/background/upload/upload.go @@ -0,0 +1,35 @@ +// 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 . + +package upload + +import ( + "code.vikunja.io/api/pkg/models" + "code.vikunja.io/api/pkg/modules/background" + "code.vikunja.io/web" +) + +type Provider struct { +} + +// Search is only used to implement the interface +func (p *Provider) Search(search string, page int64) (result []*background.Image, err error) { + return +} + +func (p *Provider) Set(image *background.Image, list *models.List, auth web.Auth) (err error) { + +} diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 23a4a4077..73c3cd028 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -50,6 +50,7 @@ import ( "code.vikunja.io/api/pkg/modules/background" backgroundHandler "code.vikunja.io/api/pkg/modules/background/handler" "code.vikunja.io/api/pkg/modules/background/unsplash" + "code.vikunja.io/api/pkg/modules/background/upload" "code.vikunja.io/api/pkg/modules/migration" migrationHandler "code.vikunja.io/api/pkg/modules/migration/handler" "code.vikunja.io/api/pkg/modules/migration/todoist" @@ -454,6 +455,14 @@ func registerAPIRoutes(a *echo.Group) { // List Backgrounds if config.BackgroundsEnabled.GetBool() { a.GET("/lists/:list/background", backgroundHandler.GetListBackground) + if config.BackgroundsUploadEnabled.GetBool() { + uploadBackgroundProvider := &backgroundHandler.BackgroundProvider{ + Provider: func() background.Provider { + return &upload.Provider{} + }, + } + a.POST("/lists/:list/backgrounds/upload", uploadBackgroundProvider.SetBackground) + } if config.BackgroundsUnsplashEnabled.GetBool() { unsplashBackgroundProvider := &backgroundHandler.BackgroundProvider{ Provider: func() background.Provider {