Started adding the actual file updload function

This commit is contained in:
kolaente 2019-10-08 22:28:04 +02:00
parent c98bad9f86
commit cfe401028b
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 162 additions and 3 deletions

View File

@ -0,0 +1,62 @@
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2019 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 models
import (
"code.vikunja.io/web"
"mime/multipart"
)
type TaskAttachment struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TaskID int64 `xorm:"int(11) not null" json:"task_id" param:"task"`
FileID int64 `xorm:"int(11) not null" json:"-"`
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
CreatedBy *User `xorm:"-" json:"created_by"`
File *multipart.File `xorm:"-" json:"-"`
Created int64 `xorm:"created"`
}
func (ta *TaskAttachment) Create(web.Auth) error {
// Store the file
// Add an entry to the db
// remove the uploaded file if adding it to the db fails
panic("implement me")
}
func (ta *TaskAttachment) ReadOne() error {
panic("implement me")
}
func (ta *TaskAttachment) ReadAll(string, web.Auth, int) (interface{}, error) {
panic("implement me")
}
func (ta *TaskAttachment) Update() error {
panic("implement me")
}
func (ta *TaskAttachment) Delete() error {
panic("implement me")
}

View File

@ -0,0 +1,44 @@
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2019 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 models
import "code.vikunja.io/web"
func (ta *TaskAttachment) IsAdmin(web.Auth) (bool, error) {
panic("implement me")
}
func (ta *TaskAttachment) CanWrite(web.Auth) (bool, error) {
panic("implement me")
}
func (ta *TaskAttachment) CanRead(web.Auth) (bool, error) {
panic("implement me")
}
func (ta *TaskAttachment) CanDelete(web.Auth) (bool, error) {
panic("implement me")
}
func (ta *TaskAttachment) CanUpdate(web.Auth) (bool, error) {
panic("implement me")
}
func (ta *TaskAttachment) CanCreate(a web.Auth) (bool, error) {
t := Task{ID: ta.TaskID}
return t.CanCreate(a)
}

View File

@ -16,6 +16,57 @@
package v1
//func UploadTaskAttachment(c echo.Context) error {
// c.FormFile()
//}
import (
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/web/handler"
"github.com/labstack/echo/v4"
"net/http"
)
func UploadTaskAttachment(c echo.Context) error {
var taskAttachment models.TaskAttachment
if err := c.Bind(&taskAttachment); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No task ID provided")
}
// Rights check
user, err := models.GetCurrentUser(c)
if err != nil {
return handler.HandleHTTPError(err, c)
}
can, err := taskAttachment.CanCreate(user)
if err != nil {
return handler.HandleHTTPError(err, c)
}
if !can {
return echo.ErrForbidden
}
// Multipart form
form, err := c.MultipartForm()
if err != nil {
return handler.HandleHTTPError(err, c)
}
files := form.File["files"]
for _, file := range files {
src, err := file.Open()
if err != nil {
return echo.ErrInternalServerError
}
defer src.Close()
// We create a new attachment object here to have a clean start
ta := models.TaskAttachment{
TaskID: taskAttachment.TaskID,
File: &src,
}
err = ta.Create(user)
if err != nil {
return handler.HandleHTTPError(err, c)
}
}
return c.NoContent(http.StatusNoContent)
}

View File

@ -266,6 +266,8 @@ func registerAPIRoutes(a *echo.Group) {
a.PUT("/tasks/:task/relations", taskRelationHandler.CreateWeb)
a.DELETE("/tasks/:task/relations", taskRelationHandler.DeleteWeb)
a.PUT("/tasks/:task/attachments", apiv1.UploadTaskAttachment)
labelHandler := &handler.WebHandler{
EmptyStruct: func() handler.CObject {
return &models.Label{}