2023-03-12 15:00:57 +00: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/>.
2021-07-05 12:23:03 +01:00
package media
import (
2021-08-25 14:34:33 +01:00
"context"
2021-07-05 12:23:03 +01:00
"fmt"
2022-03-07 10:08:26 +00:00
"io"
"net/url"
2021-07-05 12:23:03 +01:00
"strings"
2023-11-10 18:29:26 +00:00
"time"
2021-07-05 12:23:03 +01:00
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
2023-04-28 16:45:21 +01:00
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
2021-07-05 12:23:03 +01:00
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/media"
2023-11-10 18:29:26 +00:00
"github.com/superseriousbusiness/gotosocial/internal/storage"
2022-10-13 14:16:24 +01:00
"github.com/superseriousbusiness/gotosocial/internal/uris"
2021-07-05 12:23:03 +01:00
)
2023-11-10 18:29:26 +00:00
// GetFile retrieves a file from storage and streams it back
// to the caller via an io.reader embedded in *apimodel.Content.
func ( p * Processor ) GetFile (
ctx context . Context ,
requestingAccount * gtsmodel . Account ,
form * apimodel . GetContentRequestForm ,
) ( * apimodel . Content , gtserror . WithCode ) {
2021-07-05 12:23:03 +01:00
// parse the form fields
2023-02-22 15:05:26 +00:00
mediaSize , err := parseSize ( form . MediaSize )
2021-07-05 12:23:03 +01:00
if err != nil {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "media size %s not valid" , form . MediaSize ) )
}
2023-02-22 15:05:26 +00:00
mediaType , err := parseType ( form . MediaType )
2021-07-05 12:23:03 +01:00
if err != nil {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "media type %s not valid" , form . MediaType ) )
}
spl := strings . Split ( form . FileName , "." )
if len ( spl ) != 2 || spl [ 0 ] == "" || spl [ 1 ] == "" {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "file name %s not parseable" , form . FileName ) )
}
wantedMediaID := spl [ 0 ]
2022-10-13 14:16:24 +01:00
owningAccountID := form . AccountID
2021-07-05 12:23:03 +01:00
// get the account that owns the media and make sure it's not suspended
2023-03-01 18:26:53 +00:00
owningAccount , err := p . state . DB . GetAccountByID ( ctx , owningAccountID )
2021-08-25 14:34:33 +01:00
if err != nil {
2022-10-13 14:16:24 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "account with id %s could not be selected from the db: %s" , owningAccountID , err ) )
2021-07-05 12:23:03 +01:00
}
2022-10-13 14:16:24 +01:00
if ! owningAccount . SuspendedAt . IsZero ( ) {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "account with id %s is suspended" , owningAccountID ) )
2021-07-05 12:23:03 +01:00
}
// make sure the requesting account and the media account don't block each other
2022-10-13 14:16:24 +01:00
if requestingAccount != nil {
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
blocked , err := p . state . DB . IsEitherBlocked ( ctx , requestingAccount . ID , owningAccountID )
2021-07-05 12:23:03 +01:00
if err != nil {
2022-10-13 14:16:24 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "block status could not be established between accounts %s and %s: %s" , owningAccountID , requestingAccount . ID , err ) )
2021-07-05 12:23:03 +01:00
}
if blocked {
2022-10-13 14:16:24 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "block exists between accounts %s and %s" , owningAccountID , requestingAccount . ID ) )
2021-07-05 12:23:03 +01:00
}
}
// the way we store emojis is a little different from the way we store other attachments,
// so we need to take different steps depending on the media type being requested
switch mediaType {
2021-12-20 14:19:53 +00:00
case media . TypeEmoji :
2022-10-13 14:16:24 +01:00
return p . getEmojiContent ( ctx , wantedMediaID , owningAccountID , mediaSize )
2021-12-20 14:19:53 +00:00
case media . TypeAttachment , media . TypeHeader , media . TypeAvatar :
2022-10-13 14:16:24 +01:00
return p . getAttachmentContent ( ctx , requestingAccount , wantedMediaID , owningAccountID , mediaSize )
2022-03-07 10:08:26 +00:00
default :
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "media type %s not recognized" , mediaType ) )
}
}
2023-02-22 15:05:26 +00:00
/ *
UTIL FUNCTIONS
* /
func parseType ( s string ) ( media . Type , error ) {
switch s {
case string ( media . TypeAttachment ) :
return media . TypeAttachment , nil
case string ( media . TypeHeader ) :
return media . TypeHeader , nil
case string ( media . TypeAvatar ) :
return media . TypeAvatar , nil
case string ( media . TypeEmoji ) :
return media . TypeEmoji , nil
}
return "" , fmt . Errorf ( "%s not a recognized media.Type" , s )
}
func parseSize ( s string ) ( media . Size , error ) {
switch s {
case string ( media . SizeSmall ) :
return media . SizeSmall , nil
case string ( media . SizeOriginal ) :
return media . SizeOriginal , nil
case string ( media . SizeStatic ) :
return media . SizeStatic , nil
}
return "" , fmt . Errorf ( "%s not a recognized media.Size" , s )
}
func ( p * Processor ) getAttachmentContent ( ctx context . Context , requestingAccount * gtsmodel . Account , wantedMediaID string , owningAccountID string , mediaSize media . Size ) ( * apimodel . Content , gtserror . WithCode ) {
2022-03-07 10:08:26 +00:00
// retrieve attachment from the database and do basic checks on it
2023-03-01 18:26:53 +00:00
a , err := p . state . DB . GetAttachmentByID ( ctx , wantedMediaID )
2022-03-07 10:08:26 +00:00
if err != nil {
2023-11-10 18:29:26 +00:00
err = gtserror . Newf ( "attachment %s could not be taken from the db: %w" , wantedMediaID , err )
return nil , gtserror . NewErrorNotFound ( err )
2022-03-07 10:08:26 +00:00
}
2022-10-13 14:16:24 +01:00
if a . AccountID != owningAccountID {
2023-11-10 18:29:26 +00:00
err = gtserror . Newf ( "attachment %s is not owned by %s" , wantedMediaID , owningAccountID )
return nil , gtserror . NewErrorNotFound ( err )
}
// If this is an "Unknown" file type, ie., one we
// tried to process and couldn't, or one we refused
// to process because it wasn't supported, then we
// can skip a lot of steps here by simply forwarding
// the request to the remote URL.
if a . Type == gtsmodel . FileTypeUnknown {
remoteURL , err := url . Parse ( a . RemoteURL )
if err != nil {
err = gtserror . Newf ( "error parsing remote URL of 'Unknown'-type attachment for redirection: %w" , err )
return nil , gtserror . NewErrorInternalError ( err )
}
url := & storage . PresignedURL {
URL : remoteURL ,
// We might manage to cache the media
// at some point, so set a low-ish expiry.
Expiry : time . Now ( ) . Add ( 2 * time . Hour ) ,
}
return & apimodel . Content { URL : url } , nil
2022-03-07 10:08:26 +00:00
}
2023-01-11 11:13:13 +00:00
if ! * a . Cached {
// if we don't have it cached, then we can assume two things:
// 1. this is remote media, since local media should never be uncached
// 2. we need to fetch it again using a transport and the media manager
remoteMediaIRI , err := url . Parse ( a . RemoteURL )
if err != nil {
2023-07-24 13:14:13 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error parsing remote media iri %s: %w" , a . RemoteURL , err ) )
2023-01-11 11:13:13 +00:00
}
2022-03-07 10:08:26 +00:00
2023-01-11 11:13:13 +00:00
// use an empty string as requestingUsername to use the instance account, unless the request for this
// media has been http signed, then use the requesting account to make the request to remote server
var requestingUsername string
if requestingAccount != nil {
requestingUsername = requestingAccount . Username
}
2022-03-07 10:08:26 +00:00
2023-01-11 11:13:13 +00:00
// Pour one out for tobi's original streamed recache
// (streaming data both to the client and storage).
// Gone and forever missed <3
//
// [
// the reason it was removed was because a slow
// client connection could hold open a storage
2023-05-28 13:08:35 +01:00
// recache operation -> holding open a media worker.
2023-01-11 11:13:13 +00:00
// ]
2023-07-24 13:14:13 +01:00
dataFn := func ( ctx context . Context ) ( io . ReadCloser , int64 , error ) {
t , err := p . transportController . NewTransportForUsername ( ctx , requestingUsername )
2022-03-07 10:08:26 +00:00
if err != nil {
return nil , 0 , err
}
2023-07-24 13:14:13 +01:00
return t . DereferenceMedia ( gtscontext . SetFastFail ( ctx ) , remoteMediaIRI )
2021-07-05 12:23:03 +01:00
}
2022-03-07 10:08:26 +00:00
2023-01-11 11:13:13 +00:00
// Start recaching this media with the prepared data function.
2023-05-28 13:08:35 +01:00
processingMedia , err := p . mediaManager . PreProcessMediaRecache ( ctx , dataFn , wantedMediaID )
2023-01-11 11:13:13 +00:00
if err != nil {
2023-07-24 13:14:13 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error recaching media: %w" , err ) )
2022-03-07 10:08:26 +00:00
}
2023-01-11 11:13:13 +00:00
// Load attachment and block until complete
a , err = processingMedia . LoadAttachment ( ctx )
if err != nil {
2023-07-24 13:14:13 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error loading recached attachment: %w" , err ) )
2021-07-05 12:23:03 +01:00
}
}
2023-01-16 15:19:17 +00:00
var (
storagePath string
attachmentContent = & apimodel . Content {
ContentUpdated : a . UpdatedAt ,
}
)
2023-01-11 11:13:13 +00:00
// get file information from the attachment depending on the requested media size
switch mediaSize {
case media . SizeOriginal :
attachmentContent . ContentType = a . File . ContentType
attachmentContent . ContentLength = int64 ( a . File . FileSize )
storagePath = a . File . Path
case media . SizeSmall :
attachmentContent . ContentType = a . Thumbnail . ContentType
attachmentContent . ContentLength = int64 ( a . Thumbnail . FileSize )
storagePath = a . Thumbnail . Path
default :
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "media size %s not recognized for attachment" , mediaSize ) )
}
// ... so now we can safely return it
return p . retrieveFromStorage ( ctx , storagePath , attachmentContent )
2022-03-07 10:08:26 +00:00
}
2023-02-22 15:05:26 +00:00
func ( p * Processor ) getEmojiContent ( ctx context . Context , fileName string , owningAccountID string , emojiSize media . Size ) ( * apimodel . Content , gtserror . WithCode ) {
2022-03-07 10:08:26 +00:00
emojiContent := & apimodel . Content { }
var storagePath string
2022-10-13 14:16:24 +01:00
// reconstruct the static emoji image url -- reason
// for using the static URL rather than full size url
// is that static emojis are always encoded as png,
// so this is more reliable than using full size url
2023-11-10 18:29:26 +00:00
imageStaticURL := uris . URIForAttachment (
2023-07-24 13:14:13 +01:00
owningAccountID ,
string ( media . TypeEmoji ) ,
string ( media . SizeStatic ) ,
fileName ,
"png" ,
)
2022-10-13 14:16:24 +01:00
2023-03-01 18:26:53 +00:00
e , err := p . state . DB . GetEmojiByStaticURL ( ctx , imageStaticURL )
2022-09-06 11:42:55 +01:00
if err != nil {
2023-07-24 13:14:13 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "emoji %s could not be taken from the db: %w" , fileName , err ) )
2022-03-07 10:08:26 +00:00
}
2022-08-15 11:35:05 +01:00
if * e . Disabled {
2022-10-13 14:16:24 +01:00
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "emoji %s has been disabled" , fileName ) )
2022-03-07 10:08:26 +00:00
}
2023-07-24 13:14:13 +01:00
if ! * e . Cached {
// if we don't have it cached, then we can assume two things:
// 1. this is remote emoji, since local emoji should never be uncached
// 2. we need to fetch it again using a transport and the media manager
remoteURL , err := url . Parse ( e . ImageRemoteURL )
if err != nil {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error parsing remote emoji iri %s: %w" , e . ImageRemoteURL , err ) )
}
dataFn := func ( ctx context . Context ) ( io . ReadCloser , int64 , error ) {
t , err := p . transportController . NewTransportForUsername ( ctx , "" )
if err != nil {
return nil , 0 , err
}
return t . DereferenceMedia ( gtscontext . SetFastFail ( ctx ) , remoteURL )
}
// Start recaching this emoji with the prepared data function.
processingEmoji , err := p . mediaManager . PreProcessEmojiRecache ( ctx , dataFn , e . ID )
if err != nil {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error recaching emoji: %w" , err ) )
}
// Load attachment and block until complete
e , err = processingEmoji . LoadEmoji ( ctx )
if err != nil {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error loading recached emoji: %w" , err ) )
}
}
2022-03-07 10:08:26 +00:00
switch emojiSize {
case media . SizeOriginal :
emojiContent . ContentType = e . ImageContentType
emojiContent . ContentLength = int64 ( e . ImageFileSize )
storagePath = e . ImagePath
case media . SizeStatic :
emojiContent . ContentType = e . ImageStaticContentType
emojiContent . ContentLength = int64 ( e . ImageStaticFileSize )
storagePath = e . ImageStaticPath
default :
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "media size %s not recognized for emoji" , emojiSize ) )
}
2022-07-03 11:08:30 +01:00
return p . retrieveFromStorage ( ctx , storagePath , emojiContent )
2022-03-07 10:08:26 +00:00
}
2023-02-22 15:05:26 +00:00
func ( p * Processor ) retrieveFromStorage ( ctx context . Context , storagePath string , content * apimodel . Content ) ( * apimodel . Content , gtserror . WithCode ) {
2023-02-12 13:42:28 +00:00
// If running on S3 storage with proxying disabled then
// just fetch a pre-signed URL instead of serving the content.
2023-03-01 18:26:53 +00:00
if url := p . state . Storage . URL ( ctx , storagePath ) ; url != nil {
2022-07-03 11:08:30 +01:00
content . URL = url
return content , nil
}
2023-02-12 13:42:28 +00:00
2023-03-01 18:26:53 +00:00
reader , err := p . state . Storage . GetStream ( ctx , storagePath )
2021-07-05 12:23:03 +01:00
if err != nil {
return nil , gtserror . NewErrorNotFound ( fmt . Errorf ( "error retrieving from storage: %s" , err ) )
}
2022-02-19 10:44:56 +00:00
content . Content = reader
2021-07-05 12:23:03 +01:00
return content , nil
}