refetch.go (5292B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package media 19 20 import ( 21 "context" 22 "errors" 23 "fmt" 24 "io" 25 "net/url" 26 27 "github.com/superseriousbusiness/gotosocial/internal/db" 28 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 29 "github.com/superseriousbusiness/gotosocial/internal/log" 30 "github.com/superseriousbusiness/gotosocial/internal/util" 31 ) 32 33 type DereferenceMedia func(ctx context.Context, iri *url.URL) (io.ReadCloser, int64, error) 34 35 // RefetchEmojis iterates through remote emojis (for the given domain, or all if domain is empty string). 36 // 37 // For each emoji, the manager will check whether both the full size and static images are present in storage. 38 // If not, the manager will refetch and reprocess full size and static images for the emoji. 39 // 40 // The provided DereferenceMedia function will be used when it's necessary to refetch something this way. 41 func (m *Manager) RefetchEmojis(ctx context.Context, domain string, dereferenceMedia DereferenceMedia) (int, error) { 42 // normalize domain 43 if domain == "" { 44 domain = db.EmojiAllDomains 45 } 46 47 var ( 48 maxShortcodeDomain string 49 refetchIDs []string 50 ) 51 52 // page through emojis 20 at a time, looking for those with missing images 53 for { 54 // Fetch next block of emojis from database 55 emojis, err := m.state.DB.GetEmojis(ctx, domain, false, true, "", maxShortcodeDomain, "", 20) 56 if err != nil { 57 if !errors.Is(err, db.ErrNoEntries) { 58 // an actual error has occurred 59 log.Errorf(ctx, "error fetching emojis from database: %s", err) 60 } 61 break 62 } 63 64 for _, emoji := range emojis { 65 if emoji.Domain == "" { 66 // never try to refetch local emojis 67 continue 68 } 69 70 if refetch, err := m.emojiRequiresRefetch(ctx, emoji); err != nil { 71 // an error here indicates something is wrong with storage, so we should stop 72 return 0, fmt.Errorf("error checking refetch requirement for emoji %s: %w", util.ShortcodeDomain(emoji), err) 73 } else if !refetch { 74 continue 75 } 76 77 refetchIDs = append(refetchIDs, emoji.ID) 78 } 79 80 // Update next maxShortcodeDomain from last emoji 81 maxShortcodeDomain = util.ShortcodeDomain(emojis[len(emojis)-1]) 82 } 83 84 // bail early if we've got nothing to do 85 toRefetchCount := len(refetchIDs) 86 if toRefetchCount == 0 { 87 log.Debug(ctx, "no remote emojis require a refetch") 88 return 0, nil 89 } 90 log.Debugf(ctx, "%d remote emoji(s) require a refetch, doing that now...", toRefetchCount) 91 92 var totalRefetched int 93 for _, emojiID := range refetchIDs { 94 emoji, err := m.state.DB.GetEmojiByID(ctx, emojiID) 95 if err != nil { 96 // this shouldn't happen--since we know we have the emoji--so return if it does 97 return 0, fmt.Errorf("error getting emoji %s: %w", emojiID, err) 98 } 99 shortcodeDomain := util.ShortcodeDomain(emoji) 100 101 if emoji.ImageRemoteURL == "" { 102 log.Errorf(ctx, "remote emoji %s could not be refreshed because it has no ImageRemoteURL set", shortcodeDomain) 103 continue 104 } 105 106 emojiImageIRI, err := url.Parse(emoji.ImageRemoteURL) 107 if err != nil { 108 log.Errorf(ctx, "remote emoji %s could not be refreshed because its ImageRemoteURL (%s) is not a valid uri: %s", shortcodeDomain, emoji.ImageRemoteURL, err) 109 continue 110 } 111 112 dataFunc := func(ctx context.Context) (reader io.ReadCloser, fileSize int64, err error) { 113 return dereferenceMedia(ctx, emojiImageIRI) 114 } 115 116 processingEmoji, err := m.PreProcessEmoji(ctx, dataFunc, emoji.Shortcode, emoji.ID, emoji.URI, &AdditionalEmojiInfo{ 117 Domain: &emoji.Domain, 118 ImageRemoteURL: &emoji.ImageRemoteURL, 119 ImageStaticRemoteURL: &emoji.ImageStaticRemoteURL, 120 Disabled: emoji.Disabled, 121 VisibleInPicker: emoji.VisibleInPicker, 122 }, true) 123 if err != nil { 124 log.Errorf(ctx, "emoji %s could not be refreshed because of an error during processing: %s", shortcodeDomain, err) 125 continue 126 } 127 128 if _, err := processingEmoji.LoadEmoji(ctx); err != nil { 129 log.Errorf(ctx, "emoji %s could not be refreshed because of an error during loading: %s", shortcodeDomain, err) 130 continue 131 } 132 133 log.Tracef(ctx, "refetched emoji %s successfully from remote", shortcodeDomain) 134 totalRefetched++ 135 } 136 137 return totalRefetched, nil 138 } 139 140 func (m *Manager) emojiRequiresRefetch(ctx context.Context, emoji *gtsmodel.Emoji) (bool, error) { 141 if has, err := m.state.Storage.Has(ctx, emoji.ImagePath); err != nil { 142 return false, err 143 } else if !has { 144 return true, nil 145 } 146 147 if has, err := m.state.Storage.Has(ctx, emoji.ImageStaticPath); err != nil { 148 return false, err 149 } else if !has { 150 return true, nil 151 } 152 153 return false, nil 154 }