fix(background): when list background is removed, delete file from file system and DB #1372
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch ":main"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
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:
A.4) Remove background from list
A.5) Check file is still in disk and DB:
B) After these changes
B.1) Set background again
B.2) Check files in disk and DB:
B.3) Remove background image
B.4) Check files and DB again, verify image 2 has been deleted:
Good catch! Can you add a test for this?
@ -332,1 +332,4 @@
}
// Remove the old background if one exists
if list.BackgroundFileID != 0 {
Can you move this to a function and use that function in
726a517bec/pkg/modules/background/upload/upload.go (L56-L62)
as well?
Could be a function for the
List
typeNice!
Would it make sense to add a command to the cli to clean up all the files that were already deleted before?
I think so, or perhaps a migration?
I'll resubmit this on github if that's ok, since my local environment is set up that way. (The initial commit was simple enough that I just used the inline editor here, but as we're expanding it'll be faster if I do it over there)
Added some tests, refactored to a function for
List
and sent the new PR on Github:https://github.com/go-vikunja/api/pull/64
It's actually more complicated for us since the CI only runs here. What makes it easier to push your changes to GitHub instead of here?
It's just that I had only cloned the github repo locally, and since the diff was simple enough I "migrated" it here just by using the web UI (cloned the repo and edited the file + create the commit inline). But if it's best for you guys to do it here, I'll try to bring the changes over then.
01c9d2edb9
toe8ab28f145
@ -260,6 +261,40 @@ func TestList_Delete(t *testing.T) {
})
}
func TestList_DeleteBackgroundFileIfExists(t *testing.T) {
Can you add a test where you remove a list with a background and then assert the file was removed?
(check the fixtures for a list with a background)
e8ab28f145
to3101c5c4e1
@ -815,12 +815,28 @@ func (l *List) Delete(s *xorm.Session, a web.Auth) (err error) {
}
}
err = l.DeleteBackgroundFileIfExists()
So, I added this here, but when I test deleting a list from the frontend UI, it seems like the files are not being deleted. I'll look more into it later, but I guess
l
doesn't have all the fields loaded in thisDelete
function? Any pointers to wherever the magic happens that loads aList
object when aDELETE /lists/{id}
call happens?The
CanDelete
Method loads the list since it's the one doing the permission check (and it needs the full list anyway). At this point it should be loaded though, did you check with a debugger (or good ol'fmt.Println
) if the list has theBackgroundFileID
loaded?Yeah, I added a bunch of
log.Infof
here andBackgroundFileID
is always 0.Looks like
CanDelete
loads the list, but never copies the contents back intol
. But I see thatCanRead
does do*l = *originalList
. Should we be doing the same inCanDelete
?That's entirely possible. In that case, can you just load the list in
DeleteBackgroundFileIfExists
with theGetListSimpleByID
method?3101c5c4e1
tobd458fe0b2
@ -795,6 +795,11 @@ func (l *List) Create(s *xorm.Session, a web.Auth) (err error) {
// @Router /lists/{id} [delete]
func (l *List) Delete(s *xorm.Session, a web.Auth) (err error) {
err = l.DeleteBackgroundFileIfExists(s)
Can you move this code block to after when the list itself was deleted? This could otherwise lead to weird errors where the file is gone, but the list isn't (when deleting the list fails but deleting the background succeeded)
I had it that way initially, but then
GetListSimpleByID
fails to load the List because it has been deleted.I guess alternatively we could load the
fullList
here withGetListSimpleByID
, remove the call toGetListSimpleByID
fromDeleteBackgroundFileIfExists
, then down below we callfullList.DeleteBackgroundFileIfExists
?That makes a lot of sense. I Must have missed that.
I think that's the way to go. Can you check if this is required in the other place where
DeleteBackgroundFileIfExists
is called as well?The other places were working as expected per my previous testings, but I just verified in code that ultimately they have the full list loaded by that point.
The two other places are:
/lists/{id}/backgrounds/upload [put]
: already had the file deletion code, but the full list is coming from/lists/{id}/background [delete]
@ -260,0 +265,4 @@
db.LoadAndAssertFixtures(t)
files.InitTestFileFixtures(t)
s := db.NewSession()
list, err := GetListSimpleByID(s, 25)
Here you should not load the full list, instead create the list object like in the test before:
That way it's the same as when the web handler calls the delete method.
bd458fe0b2
tod74fdb35d1
WIP: fix(background): when list background is removed, delete file from file system and DBto fix(background): when list background is removed, delete file from file system and DBThanks!