1
0

fix(files): use absolute path everywhere

(cherry picked from commit 68636f27da707f3ee87ba0e4f1ff100504486608)
This commit is contained in:
kolaente
2024-09-06 12:55:35 +02:00
parent cca02a3f2e
commit 0b9f3070fd
11 changed files with 45 additions and 31 deletions

View File

@ -20,7 +20,6 @@ import (
"os"
"testing"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/keyvalue"
@ -54,9 +53,8 @@ func initFixtures(t *testing.T) {
// InitTestFileFixtures initializes file fixtures
func InitTestFileFixtures(t *testing.T) {
// Init fixture files
filename := config.FilesBasePath.GetString() + "/1"
err := afero.WriteFile(afs, filename, []byte("testfile1"), 0644)
testfile := &File{ID: 1}
err := afero.WriteFile(afs, testfile.getAbsoluteFilePath(), []byte("testfile1"), 0644)
require.NoError(t, err)
}
@ -84,6 +82,6 @@ func InitTests() {
}
// FileStat stats a file. This is an exported function to be able to test this from outide of the package
func FileStat(filename string) (os.FileInfo, error) {
return afs.Stat(filename)
func FileStat(file *File) (os.FileInfo, error) {
return afs.Stat(file.getAbsoluteFilePath())
}

View File

@ -128,11 +128,8 @@ func CreateWithMimeAndSession(s *xorm.Session, f io.Reader, realname string, rea
}
// Delete removes a file from the DB and the file system
func (f *File) Delete() (err error) {
s := db.NewSession()
defer s.Close()
deleted, err := s.Where("id = ?", f.ID).Delete(f)
func (f *File) Delete(s *xorm.Session) (err error) {
deleted, err := s.Where("id = ?", f.ID).Delete(&File{})
if err != nil {
_ = s.Rollback()
return err

View File

@ -21,6 +21,8 @@ import (
"os"
"testing"
"code.vikunja.io/api/pkg/db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -84,15 +86,20 @@ func TestCreate(t *testing.T) {
func TestFile_Delete(t *testing.T) {
t.Run("Normal", func(t *testing.T) {
s := db.NewSession()
defer s.Close()
initFixtures(t)
f := &File{ID: 1}
err := f.Delete()
err := f.Delete(s)
require.NoError(t, err)
})
t.Run("Nonexisting", func(t *testing.T) {
s := db.NewSession()
defer s.Close()
initFixtures(t)
f := &File{ID: 9999}
err := f.Delete()
err := f.Delete(s)
require.Error(t, err)
assert.True(t, IsErrFileDoesNotExist(err))
})