| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // 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/>. | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | package media_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"path" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/suite" | 
					
						
							|  |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2022-08-31 17:31:21 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/media" | 
					
						
							| 
									
										
										
										
											2023-08-07 19:38:11 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util" | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/testrig" | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type GetFileTestSuite struct { | 
					
						
							|  |  |  | 	MediaStandardTestSuite | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *GetFileTestSuite) TestGetRemoteFileCached() { | 
					
						
							|  |  |  | 	ctx := context.Background() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"] | 
					
						
							|  |  |  | 	fileName := path.Base(testAttachment.File.Path) | 
					
						
							|  |  |  | 	requestingAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	content, errWithCode := suite.mediaProcessor.GetFile(ctx, requestingAccount, &apimodel.GetContentRequestForm{ | 
					
						
							|  |  |  | 		AccountID: testAttachment.AccountID, | 
					
						
							|  |  |  | 		MediaType: string(media.TypeAttachment), | 
					
						
							|  |  |  | 		MediaSize: string(media.SizeOriginal), | 
					
						
							|  |  |  | 		FileName:  fileName, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.NoError(errWithCode) | 
					
						
							|  |  |  | 	suite.NotNil(content) | 
					
						
							|  |  |  | 	b, err := io.ReadAll(content.Content) | 
					
						
							|  |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if closer, ok := content.Content.(io.Closer); ok { | 
					
						
							|  |  |  | 		suite.NoError(closer.Close()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].Data, b) | 
					
						
							|  |  |  | 	suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].ContentType, content.ContentType) | 
					
						
							|  |  |  | 	suite.EqualValues(len(suite.testRemoteAttachments[testAttachment.RemoteURL].Data), content.ContentLength) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *GetFileTestSuite) TestGetRemoteFileUncached() { | 
					
						
							|  |  |  | 	ctx := context.Background() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// uncache the file from local | 
					
						
							|  |  |  | 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"] | 
					
						
							| 
									
										
										
										
											2023-08-07 19:38:11 +02:00
										 |  |  | 	testAttachment.Cached = util.Ptr(false) | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 	err := suite.db.UpdateByID(ctx, testAttachment, testAttachment.ID, "cached") | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	err = suite.storage.Delete(ctx, testAttachment.File.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	err = suite.storage.Delete(ctx, testAttachment.Thumbnail.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// now fetch it | 
					
						
							|  |  |  | 	fileName := path.Base(testAttachment.File.Path) | 
					
						
							|  |  |  | 	requestingAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	content, errWithCode := suite.mediaProcessor.GetFile(ctx, requestingAccount, &apimodel.GetContentRequestForm{ | 
					
						
							|  |  |  | 		AccountID: testAttachment.AccountID, | 
					
						
							|  |  |  | 		MediaType: string(media.TypeAttachment), | 
					
						
							|  |  |  | 		MediaSize: string(media.SizeOriginal), | 
					
						
							|  |  |  | 		FileName:  fileName, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	suite.NoError(errWithCode) | 
					
						
							|  |  |  | 	suite.NotNil(content) | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	b, err := io.ReadAll(content.Content) | 
					
						
							|  |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-11-03 15:03:12 +01:00
										 |  |  | 	suite.NoError(content.Content.Close()) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].Data, b) | 
					
						
							|  |  |  | 	suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].ContentType, content.ContentType) | 
					
						
							|  |  |  | 	suite.EqualValues(len(suite.testRemoteAttachments[testAttachment.RemoteURL].Data), content.ContentLength) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the attachment should be updated in the database | 
					
						
							| 
									
										
										
										
											2022-08-31 17:31:21 +02:00
										 |  |  | 	var dbAttachment *gtsmodel.MediaAttachment | 
					
						
							|  |  |  | 	if !testrig.WaitFor(func() bool { | 
					
						
							|  |  |  | 		dbAttachment, err = suite.db.GetAttachmentByID(ctx, testAttachment.ID) | 
					
						
							|  |  |  | 		return dbAttachment != nil | 
					
						
							|  |  |  | 	}) { | 
					
						
							|  |  |  | 		suite.FailNow("timed out waiting for updated attachment") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 	suite.True(*dbAttachment.Cached) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// the file should be back in storage at the same path as before | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	refreshedBytes, err := suite.storage.Get(ctx, dbAttachment.File.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 	suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].Data, refreshedBytes) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *GetFileTestSuite) TestGetRemoteFileUncachedInterrupted() { | 
					
						
							|  |  |  | 	ctx := context.Background() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// uncache the file from local | 
					
						
							|  |  |  | 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"] | 
					
						
							| 
									
										
										
										
											2023-08-07 19:38:11 +02:00
										 |  |  | 	testAttachment.Cached = util.Ptr(false) | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 	err := suite.db.UpdateByID(ctx, testAttachment, testAttachment.ID, "cached") | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	err = suite.storage.Delete(ctx, testAttachment.File.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	err = suite.storage.Delete(ctx, testAttachment.Thumbnail.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// now fetch it | 
					
						
							|  |  |  | 	fileName := path.Base(testAttachment.File.Path) | 
					
						
							|  |  |  | 	requestingAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	content, errWithCode := suite.mediaProcessor.GetFile(ctx, requestingAccount, &apimodel.GetContentRequestForm{ | 
					
						
							|  |  |  | 		AccountID: testAttachment.AccountID, | 
					
						
							|  |  |  | 		MediaType: string(media.TypeAttachment), | 
					
						
							|  |  |  | 		MediaSize: string(media.SizeOriginal), | 
					
						
							|  |  |  | 		FileName:  fileName, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	suite.NoError(errWithCode) | 
					
						
							|  |  |  | 	suite.NotNil(content) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	_, err = io.CopyN(io.Discard, content.Content, 1024) | 
					
						
							|  |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	err = content.Content.Close() | 
					
						
							|  |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// the attachment should still be updated in the database even though the caller hung up | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	var dbAttachment *gtsmodel.MediaAttachment | 
					
						
							| 
									
										
										
										
											2022-08-31 17:31:21 +02:00
										 |  |  | 	if !testrig.WaitFor(func() bool { | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 		dbAttachment, _ = suite.db.GetAttachmentByID(ctx, testAttachment.ID) | 
					
						
							| 
									
										
										
										
											2022-08-31 17:31:21 +02:00
										 |  |  | 		return *dbAttachment.Cached | 
					
						
							|  |  |  | 	}) { | 
					
						
							|  |  |  | 		suite.FailNow("timed out waiting for attachment to be updated") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// the file should be back in storage at the same path as before | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	refreshedBytes, err := suite.storage.Get(ctx, dbAttachment.File.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 	suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].Data, refreshedBytes) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *GetFileTestSuite) TestGetRemoteFileThumbnailUncached() { | 
					
						
							|  |  |  | 	ctx := context.Background() | 
					
						
							|  |  |  | 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// fetch the existing thumbnail bytes from storage first | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	thumbnailBytes, err := suite.storage.Get(ctx, testAttachment.Thumbnail.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// uncache the file from local | 
					
						
							| 
									
										
										
										
											2023-08-07 19:38:11 +02:00
										 |  |  | 	testAttachment.Cached = util.Ptr(false) | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 	err = suite.db.UpdateByID(ctx, testAttachment, testAttachment.ID, "cached") | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	err = suite.storage.Delete(ctx, testAttachment.File.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	err = suite.storage.Delete(ctx, testAttachment.Thumbnail.Path) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// now fetch the thumbnail | 
					
						
							|  |  |  | 	fileName := path.Base(testAttachment.File.Path) | 
					
						
							|  |  |  | 	requestingAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	content, errWithCode := suite.mediaProcessor.GetFile(ctx, requestingAccount, &apimodel.GetContentRequestForm{ | 
					
						
							|  |  |  | 		AccountID: testAttachment.AccountID, | 
					
						
							|  |  |  | 		MediaType: string(media.TypeAttachment), | 
					
						
							|  |  |  | 		MediaSize: string(media.SizeSmall), | 
					
						
							|  |  |  | 		FileName:  fileName, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	suite.NoError(errWithCode) | 
					
						
							|  |  |  | 	suite.NotNil(content) | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	b, err := io.ReadAll(content.Content) | 
					
						
							|  |  |  | 	suite.NoError(err) | 
					
						
							| 
									
										
										
										
											2022-11-03 15:03:12 +01:00
										 |  |  | 	suite.NoError(content.Content.Close()) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(thumbnailBytes, b) | 
					
						
							| 
									
										
										
										
											2024-08-08 17:12:13 +00:00
										 |  |  | 	suite.Equal("image/jpeg", content.ContentType) | 
					
						
							| 
									
										
										
										
											2022-03-07 11:08:26 +01:00
										 |  |  | 	suite.EqualValues(testAttachment.Thumbnail.FileSize, content.ContentLength) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetFileTestSuite(t *testing.T) { | 
					
						
							|  |  |  | 	suite.Run(t, &GetFileTestSuite{}) | 
					
						
							|  |  |  | } |