Task Attachments #104
|
@ -18,6 +18,8 @@ package files
|
|||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/web"
|
||||
"github.com/spf13/afero"
|
||||
"mime/multipart"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -36,7 +38,7 @@ type File struct {
|
|||
InsertedUnix int64 `xorm:"created" json:"inserted_unix"`
|
||||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||
|
||||
File *multipart.FileHeader `xorm:"-" json:"-"`
|
||||
File *afero.File `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// TableName is the table name for the files table
|
||||
|
@ -44,18 +46,24 @@ func (File) TableName() string {
|
|||
return "files"
|
||||
}
|
||||
|
||||
// GetFileByID returns a file by its ID
|
||||
func (f *File) GetFileByID() (err error) {
|
||||
func (f *File) getFileName() string {
|
||||
return config.FilesBasePath.GetString() + "/" + strconv.FormatInt(f.ID, 10)
|
||||
}
|
||||
|
||||
// LoadFileByID returns a file by its ID
|
||||
func (f *File) LoadFileByID() (err error) {
|
||||
*f.File, err = afs.Open(f.getFileName())
|
||||
return
|
||||
}
|
||||
|
||||
// Create creates a new file from an FileHeader
|
||||
func Create(f *multipart.FileHeader) (file *File, err error) {
|
||||
// NewAttachment creates a new file from an FileHeader
|
||||
func Create(f *multipart.FileHeader, a web.Auth) (file *File, err error) {
|
||||
|
||||
// We first insert the file into the db to get it's ID
|
||||
file = &File{
|
||||
Name: f.Filename,
|
||||
Size: f.Size,
|
||||
Name: f.Filename,
|
||||
Size: f.Size,
|
||||
CreatedByID: a.GetID(),
|
||||
}
|
||||
|
||||
_, err = x.Insert(file)
|
||||
|
@ -71,11 +79,17 @@ func Create(f *multipart.FileHeader) (file *File, err error) {
|
|||
defer ff.Close()
|
||||
|
||||
// Save the file to storage with its new ID as path
|
||||
err = afs.WriteReader(config.FilesBasePath.GetString()+"/"+strconv.FormatInt(file.ID, 10), ff)
|
||||
err = afs.WriteReader(file.getFileName(), ff)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete removes a file from the DB and the file system
|
||||
func (f *File) Delete() (err error) {
|
||||
_, err = x.Where("id = ?", f.ID).Delete(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = afs.Remove(f.getFileName())
|
||||
return
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"github.com/go-xorm/xorm"
|
||||
"src.techknowlogick.com/xormigrate"
|
||||
)
|
||||
|
@ -30,8 +29,6 @@ type taskAttachment20191010131430 struct {
|
|||
|
||||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||
|
||||
File *files.File `xorm:"extends" json:"file"`
|
||||
|
||||
Created int64 `xorm:"created"`
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ package models
|
|||
import (
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/web"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
// TaskAttachment is the definition of a task attachment
|
||||
|
@ -43,11 +44,11 @@ func (TaskAttachment) TableName() string {
|
|||
return "task_attachments"
|
||||
}
|
||||
|
||||
// Create creates a new task attachment
|
||||
func (ta *TaskAttachment) Create(a web.Auth) error {
|
||||
// NewAttachment creates a new task attachment
|
||||
func (ta *TaskAttachment) NewAttachment(fileheader *multipart.FileHeader, a web.Auth) error {
|
||||
|
||||
// Store the file
|
||||
file, err := files.Create(ta.File.File)
|
||||
file, err := files.Create(fileheader, a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -81,7 +82,6 @@ func (ta *TaskAttachment) ReadOne() (err error) {
|
|||
|
||||
// Get the file
|
||||
ta.File.ID = ta.FileID
|
||||
err = ta.File.GetFileByID()
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -98,5 +98,18 @@ func (ta *TaskAttachment) ReadAll(s string, a web.Auth, page int) (interface{},
|
|||
|
||||
// Delete removes an attachment
|
||||
func (ta *TaskAttachment) Delete() error {
|
||||
panic("implement me")
|
||||
// Load the attachment
|
||||
err := ta.ReadOne()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete it
|
||||
_, err = x.Where("task_id = ? AND id = ?", ta.TaskID, ta.ID).Delete(ta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the underlying file
|
||||
return ta.File.Delete()
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/models"
|
||||
"code.vikunja.io/web/handler"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
@ -56,9 +55,8 @@ func UploadTaskAttachment(c echo.Context) error {
|
|||
// We create a new attachment object here to have a clean start
|
||||
ta := models.TaskAttachment{
|
||||
TaskID: taskAttachment.TaskID,
|
||||
File: &files.File{File: file},
|
||||
}
|
||||
err = ta.Create(user)
|
||||
err = ta.NewAttachment(file, user)
|
||||
if err != nil {
|
||||
return handler.HandleHTTPError(err, c)
|
||||
}
|
||||
|
@ -95,6 +93,7 @@ func GetTaskAttachment(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Send the file to the client
|
||||
taskAttachment.File.LoadFileByID()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue