2022-03-07 13:08:26 +03:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 14:43:00 +03:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2022-03-07 13:08:26 +03:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package media_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2022-11-05 14:10:19 +03:00
|
|
|
"codeberg.org/gruf/go-store/v2/storage"
|
2022-03-07 13:08:26 +03:00
|
|
|
"github.com/stretchr/testify/suite"
|
2022-12-12 14:22:19 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-03-07 13:08:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type PruneRemoteTestSuite struct {
|
|
|
|
MediaStandardTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PruneRemoteTestSuite) TestPruneRemote() {
|
2022-12-12 14:22:19 +03:00
|
|
|
testStatusAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
suite.True(*testStatusAttachment.Cached)
|
|
|
|
|
|
|
|
testHeader := suite.testAttachments["remote_account_3_header"]
|
|
|
|
suite.True(*testHeader.Cached)
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-05-15 17:45:04 +03:00
|
|
|
totalPruned, err := suite.manager.PruneAllRemote(context.Background(), 1)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.NoError(err)
|
2023-01-23 15:39:01 +03:00
|
|
|
suite.Equal(2, totalPruned)
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
prunedAttachment, err := suite.db.GetAttachmentByID(context.Background(), testStatusAttachment.ID)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.NoError(err)
|
2022-12-12 14:22:19 +03:00
|
|
|
suite.False(*prunedAttachment.Cached)
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
prunedAttachment, err = suite.db.GetAttachmentByID(context.Background(), testHeader.ID)
|
|
|
|
suite.NoError(err)
|
2022-08-15 13:35:05 +03:00
|
|
|
suite.False(*prunedAttachment.Cached)
|
2022-03-07 13:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PruneRemoteTestSuite) TestPruneRemoteTwice() {
|
2022-05-15 17:45:04 +03:00
|
|
|
totalPruned, err := suite.manager.PruneAllRemote(context.Background(), 1)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.NoError(err)
|
2023-01-23 15:39:01 +03:00
|
|
|
suite.Equal(2, totalPruned)
|
2022-03-07 13:08:26 +03:00
|
|
|
|
|
|
|
// final prune should prune nothing, since the first prune already happened
|
2022-05-15 17:45:04 +03:00
|
|
|
totalPrunedAgain, err := suite.manager.PruneAllRemote(context.Background(), 1)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(0, totalPrunedAgain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PruneRemoteTestSuite) TestPruneAndRecache() {
|
|
|
|
ctx := context.Background()
|
2022-12-12 14:22:19 +03:00
|
|
|
testStatusAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
testHeader := suite.testAttachments["remote_account_3_header"]
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-05-15 17:45:04 +03:00
|
|
|
totalPruned, err := suite.manager.PruneAllRemote(ctx, 1)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.NoError(err)
|
2023-01-23 15:39:01 +03:00
|
|
|
suite.Equal(2, totalPruned)
|
2022-03-07 13:08:26 +03:00
|
|
|
|
|
|
|
// media should no longer be stored
|
2022-12-12 14:22:19 +03:00
|
|
|
_, err = suite.storage.Get(ctx, testStatusAttachment.File.Path)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.ErrorIs(err, storage.ErrNotFound)
|
2022-12-12 14:22:19 +03:00
|
|
|
_, err = suite.storage.Get(ctx, testStatusAttachment.Thumbnail.Path)
|
|
|
|
suite.ErrorIs(err, storage.ErrNotFound)
|
|
|
|
_, err = suite.storage.Get(ctx, testHeader.File.Path)
|
|
|
|
suite.ErrorIs(err, storage.ErrNotFound)
|
|
|
|
_, err = suite.storage.Get(ctx, testHeader.Thumbnail.Path)
|
2022-03-07 13:08:26 +03:00
|
|
|
suite.ErrorIs(err, storage.ErrNotFound)
|
|
|
|
|
|
|
|
// now recache the image....
|
2022-11-03 17:03:12 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, int64, error) {
|
2022-03-07 13:08:26 +03:00
|
|
|
// load bytes from a test image
|
2023-01-11 14:13:13 +03:00
|
|
|
b, err := os.ReadFile("../../testrig/media/thoughtsofdog-original.jpg")
|
2022-03-07 13:08:26 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-03 17:03:12 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), int64(len(b)), nil
|
2022-03-07 13:08:26 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
for _, original := range []*gtsmodel.MediaAttachment{
|
|
|
|
testStatusAttachment,
|
|
|
|
testHeader,
|
|
|
|
} {
|
|
|
|
processingRecache, err := suite.manager.RecacheMedia(ctx, data, nil, original.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// synchronously load the recached attachment
|
|
|
|
recachedAttachment, err := processingRecache.LoadAttachment(ctx)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(recachedAttachment)
|
|
|
|
|
|
|
|
// recachedAttachment should be basically the same as the old attachment
|
|
|
|
suite.True(*recachedAttachment.Cached)
|
|
|
|
suite.Equal(original.ID, recachedAttachment.ID)
|
|
|
|
suite.Equal(original.File.Path, recachedAttachment.File.Path) // file should be stored in the same place
|
|
|
|
suite.Equal(original.Thumbnail.Path, recachedAttachment.Thumbnail.Path) // as should the thumbnail
|
|
|
|
suite.EqualValues(original.FileMeta, recachedAttachment.FileMeta) // and the filemeta should be the same
|
|
|
|
|
|
|
|
// recached files should be back in storage
|
|
|
|
_, err = suite.storage.Get(ctx, recachedAttachment.File.Path)
|
|
|
|
suite.NoError(err)
|
|
|
|
_, err = suite.storage.Get(ctx, recachedAttachment.Thumbnail.Path)
|
|
|
|
suite.NoError(err)
|
|
|
|
}
|
2022-03-07 13:08:26 +03:00
|
|
|
}
|
|
|
|
|
2022-05-08 20:49:45 +03:00
|
|
|
func (suite *PruneRemoteTestSuite) TestPruneOneNonExistent() {
|
|
|
|
ctx := context.Background()
|
2022-12-12 14:22:19 +03:00
|
|
|
testStatusAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
2022-05-08 20:49:45 +03:00
|
|
|
|
|
|
|
// Delete this attachment cached on disk
|
2022-12-12 14:22:19 +03:00
|
|
|
media, err := suite.db.GetAttachmentByID(ctx, testStatusAttachment.ID)
|
2022-05-08 20:49:45 +03:00
|
|
|
suite.NoError(err)
|
2022-08-15 13:35:05 +03:00
|
|
|
suite.True(*media.Cached)
|
2022-07-03 13:08:30 +03:00
|
|
|
err = suite.storage.Delete(ctx, media.File.Path)
|
2022-05-08 20:49:45 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// Now attempt to prune remote for item with db entry no file
|
2022-05-15 17:45:04 +03:00
|
|
|
totalPruned, err := suite.manager.PruneAllRemote(ctx, 1)
|
2022-05-08 20:49:45 +03:00
|
|
|
suite.NoError(err)
|
2023-01-23 15:39:01 +03:00
|
|
|
suite.Equal(2, totalPruned)
|
2022-05-08 20:49:45 +03:00
|
|
|
}
|
|
|
|
|
2022-03-07 13:08:26 +03:00
|
|
|
func TestPruneRemoteTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &PruneRemoteTestSuite{})
|
|
|
|
}
|