fix(background): when list background is removed, delete file from file system and DB
continuous-integration/drone/pr Build is failing Details

Currently, if a list background is removed, the background file stays in disk and in the DB forever.

Fix this by deleting the file during background removal, similar to how it's done when a new background is uploaded: [source](726a517bec/pkg/modules/background/upload/upload.go (L56-L62))

Test Plan:
A) Before these changes
A.1) Create a new list
A.2) Set a new background to the list
A.3) Check file is in disk and DB:
```
$ ls files/
1

$ sqlite3 vikunja.db 
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> select * from files;
1|20220410_180833.jpg||2822966|2023-01-25 21:25:28|1
sqlite> select * from lists;
1|Teste||||1|1|0|1|LeGlCmIqR*of_4R+WCoe4;s.oLjZ|65536.0|2023-01-25 21:25:21|2023-01-25 21:25:28
```

A.4) Remove background from list
A.5) Check file is still in disk and DB:
```
$ ls files/
1

$ sqlite3 vikunja.db 
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> select * from files;
1|20220410_180833.jpg||2822966|2023-01-25 21:25:28|1
sqlite> select * from lists;
1|Teste||||1|1|0|0||65536.0|2023-01-25 21:25:21|2023-01-25 21:32:39
```

B) After these changes
B.1) Set background again
B.2) Check files in disk and DB:
```
$ ls files/
1 2

$ sqlite3 vikunja.db 
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> select * from files;
1|20220410_180833.jpg||2822966|2023-01-25 21:25:28|1
2|20220410_180833.jpg||2822966|2023-01-25 21:34:33|1
sqlite> select * from lists;
1|Teste||||1|1|0|2|LeGlCmIqR*of_4R+WCoe4;s.oLjZ|65536.0|2023-01-25 21:25:21|2023-01-25 21:34:33
```

B.3) Remove background image
B.4) Check files and DB again, verify image 2 has been deleted:
```
$ ls files/
1

$ sqlite3 vikunja.db 
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> select * from files;
1|20220410_180833.jpg||2822966|2023-01-25 21:25:28|1
sqlite> select * from lists;
1|Teste||||1|1|0|0||65536.0|2023-01-25 21:25:21|2023-01-25 21:36:13
```
This commit is contained in:
testinho.testador 2023-01-25 21:37:29 +00:00
parent 682123a9c9
commit 01c9d2edb9
1 changed files with 8 additions and 0 deletions

View File

@ -330,6 +330,14 @@ func RemoveListBackground(c echo.Context) error {
if err != nil {
return err
}
// Remove the old background if one exists
if list.BackgroundFileID != 0 {
file := files.File{ID: list.BackgroundFileID}
if err := file.Delete(); err != nil {
return err
}
}
list.BackgroundFileID = 0
list.BackgroundInformation = nil