chore(files): use absolute file path to retrieve and save files

This commit is contained in:
kolaente 2024-09-05 15:03:32 +02:00
parent 22e594e253
commit c2b116de70
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 12 additions and 7 deletions

View File

@ -20,6 +20,7 @@ import (
"errors"
"io"
"os"
"path/filepath"
"strconv"
"time"
@ -52,17 +53,21 @@ type File struct {
}
// TableName is the table name for the files table
func (File) TableName() string {
func (*File) TableName() string {
return "files"
}
func (f *File) getFileName() string {
return config.FilesBasePath.GetString() + "/" + strconv.FormatInt(f.ID, 10)
func (f *File) getAbsoluteFilePath() string {
return filepath.Join(
config.ServiceRootpath.GetString(),
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())
f.File, err = afs.Open(f.getAbsoluteFilePath())
return
}
@ -137,7 +142,7 @@ func (f *File) Delete() (err error) {
return ErrFileDoesNotExist{FileID: f.ID}
}
err = afs.Remove(f.getFileName())
err = afs.Remove(f.getAbsoluteFilePath())
if err != nil {
var perr *os.PathError
if errors.As(err, &perr) {
@ -155,7 +160,7 @@ func (f *File) Delete() (err error) {
// Save saves a file to storage
func (f *File) Save(fcontent io.Reader) (err error) {
err = afs.WriteReader(f.getFileName(), fcontent)
err = afs.WriteReader(f.getAbsoluteFilePath(), fcontent)
if err != nil {
return
}

View File

@ -174,7 +174,7 @@ func GetTaskAttachment(c echo.Context) error {
}
}
// Open an send the file to the client
// Open and send the file to the client
err = taskAttachment.File.LoadFileByID()
if err != nil {
_ = s.Rollback()