2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2019-10-16 22:52:29 +02:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2019-10-16 22:52:29 +02:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-10-16 22:52:29 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-10-16 22:52:29 +02:00
|
|
|
|
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"net/http"
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
"code.vikunja.io/api/pkg/models"
|
2020-11-21 17:38:58 +01:00
|
|
|
auth2 "code.vikunja.io/api/pkg/modules/auth"
|
2019-10-16 22:52:29 +02:00
|
|
|
"code.vikunja.io/web/handler"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UploadTaskAttachment handles everything needed for the upload of a task attachment
|
|
|
|
// @Summary Upload a task attachment
|
|
|
|
// @Description Upload a task attachment. You can pass multiple files with the files form param.
|
|
|
|
// @tags task
|
|
|
|
// @Accept mpfd
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "Task ID"
|
|
|
|
// @Param files formData string true "The file, as multipart form file. You can pass multiple."
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Success 200 {object} models.Message "Attachments were uploaded successfully."
|
|
|
|
// @Failure 403 {object} models.Message "No access to the task."
|
|
|
|
// @Failure 404 {object} models.Message "The task does not exist."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /tasks/{id}/attachments [put]
|
|
|
|
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
|
2020-11-21 17:38:58 +01:00
|
|
|
auth, err := auth2.GetAuthFromClaims(c)
|
2019-10-16 22:52:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
2020-05-12 15:51:48 +02:00
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
can, err := taskAttachment.CanCreate(s, auth)
|
2019-10-16 22:52:29 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-10-16 22:52:29 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
if !can {
|
|
|
|
return echo.ErrForbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multipart form
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-10-16 22:52:29 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
Errors []*echo.HTTPError `json:"errors"`
|
|
|
|
Success []*models.TaskAttachment `json:"success"`
|
|
|
|
}
|
|
|
|
r := &result{}
|
|
|
|
fileHeaders := form.File["files"]
|
|
|
|
for _, file := range fileHeaders {
|
|
|
|
// We create a new attachment object here to have a clean start
|
|
|
|
ta := &models.TaskAttachment{
|
|
|
|
TaskID: taskAttachment.TaskID,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
r.Errors = append(r.Errors, handler.HandleHTTPError(err, c))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
err = ta.NewAttachment(s, f, file.Filename, uint64(file.Size), auth)
|
2019-10-16 22:52:29 +02:00
|
|
|
if err != nil {
|
|
|
|
r.Errors = append(r.Errors, handler.HandleHTTPError(err, c))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.Success = append(r.Success, ta)
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
if err := s.Commit(); err != nil {
|
|
|
|
_ = s.Rollback()
|
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
return c.JSON(http.StatusOK, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTaskAttachment returns a task attachment to download for the user
|
|
|
|
// @Summary Get one attachment.
|
|
|
|
// @Description Get one attachment for download. **Returns json on error.**
|
|
|
|
// @tags task
|
|
|
|
// @Produce octet-stream
|
|
|
|
// @Param id path int true "Task ID"
|
|
|
|
// @Param attachmentID path int true "Attachment ID"
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Success 200 {} string "The attachment file."
|
|
|
|
// @Failure 403 {object} models.Message "No access to this task."
|
|
|
|
// @Failure 404 {object} models.Message "The task does not exist."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /tasks/{id}/attachments/{attachmentID} [get]
|
|
|
|
func GetTaskAttachment(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
|
2020-11-21 17:38:58 +01:00
|
|
|
auth, err := auth2.GetAuthFromClaims(c)
|
2019-10-16 22:52:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
can, _, err := taskAttachment.CanRead(s, auth)
|
2019-10-16 22:52:29 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-10-16 22:52:29 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
if !can {
|
|
|
|
return echo.ErrForbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the attachment incl file
|
2020-12-23 16:32:28 +01:00
|
|
|
err = taskAttachment.ReadOne(s)
|
2019-10-16 22:52:29 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-10-16 22:52:29 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an send the file to the client
|
|
|
|
err = taskAttachment.File.LoadFileByID()
|
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Commit(); err != nil {
|
|
|
|
_ = s.Rollback()
|
2019-10-16 22:52:29 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(c.Response(), c.Request(), taskAttachment.File.Name, taskAttachment.File.Created, taskAttachment.File.File)
|
|
|
|
return nil
|
|
|
|
}
|