From 9e7d022a06779a03e3eaaadad6cc33423f46892b Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Thu, 30 Jun 2022 12:22:10 +0200 Subject: [feature] Cleanup unattached local media (#680) * add localUnattached db function * add parseOlderThan util function * add pruneunusedlocalattachments to media manager * add unusedlocal pruning to schedule + admin call * set number of days to keep as a const * fix test --- internal/media/pruneunusedlocal_test.go | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 internal/media/pruneunusedlocal_test.go (limited to 'internal/media/pruneunusedlocal_test.go') diff --git a/internal/media/pruneunusedlocal_test.go b/internal/media/pruneunusedlocal_test.go new file mode 100644 index 000000000..5f6bfbfba --- /dev/null +++ b/internal/media/pruneunusedlocal_test.go @@ -0,0 +1,75 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package media_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +type PruneUnusedLocalTestSuite struct { + MediaStandardTestSuite +} + +func (suite *PruneUnusedLocalTestSuite) TestPruneUnusedLocal() { + testAttachment := suite.testAttachments["local_account_1_unattached_1"] + suite.True(testAttachment.Cached) + + totalPruned, err := suite.manager.PruneUnusedLocalAttachments(context.Background()) + suite.NoError(err) + suite.Equal(1, totalPruned) + + _, err = suite.db.GetAttachmentByID(context.Background(), testAttachment.ID) + suite.ErrorIs(err, db.ErrNoEntries) +} + +func (suite *PruneUnusedLocalTestSuite) TestPruneRemoteTwice() { + totalPruned, err := suite.manager.PruneUnusedLocalAttachments(context.Background()) + suite.NoError(err) + suite.Equal(1, totalPruned) + + // final prune should prune nothing, since the first prune already happened + totalPrunedAgain, err := suite.manager.PruneUnusedLocalAttachments(context.Background()) + suite.NoError(err) + suite.Equal(0, totalPrunedAgain) +} + +func (suite *PruneUnusedLocalTestSuite) TestPruneOneNonExistent() { + ctx := context.Background() + testAttachment := suite.testAttachments["local_account_1_unattached_1"] + + // Delete this attachment cached on disk + media, err := suite.db.GetAttachmentByID(ctx, testAttachment.ID) + suite.NoError(err) + suite.True(media.Cached) + err = suite.storage.Delete(media.File.Path) + suite.NoError(err) + + // Now attempt to prune for item with db entry no file + totalPruned, err := suite.manager.PruneUnusedLocalAttachments(ctx) + suite.NoError(err) + suite.Equal(1, totalPruned) +} + +func TestPruneUnusedLocalTestSuite(t *testing.T) { + suite.Run(t, &PruneUnusedLocalTestSuite{}) +} -- cgit v1.2.3