/
pipeline.go
481 lines (414 loc) · 18.3 KB
/
pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package tokenprocessing
import (
"context"
"errors"
"net/http"
"cloud.google.com/go/storage"
"github.com/everFinance/goar"
shell "github.com/ipfs/go-ipfs-api"
"github.com/jackc/pgtype"
"github.com/sirupsen/logrus"
db "github.com/mikeydub/go-gallery/db/gen/coredb"
"github.com/mikeydub/go-gallery/platform"
"github.com/mikeydub/go-gallery/service/logger"
"github.com/mikeydub/go-gallery/service/media"
"github.com/mikeydub/go-gallery/service/persist"
"github.com/mikeydub/go-gallery/service/rpc"
"github.com/mikeydub/go-gallery/service/tokenmanage"
"github.com/mikeydub/go-gallery/util"
)
type tokenProcessor struct {
queries *db.Queries
httpClient *http.Client
metadataFinder *MetadataFinder
ipfsClient *shell.Shell
arweaveClient *goar.Client
stg *storage.Client
tokenBucket string
}
func NewTokenProcessor(queries *db.Queries, httpClient *http.Client, metadataFinder *MetadataFinder, ipfsClient *shell.Shell, arweaveClient *goar.Client, stg *storage.Client, tokenBucket string) *tokenProcessor {
return &tokenProcessor{
queries: queries,
metadataFinder: metadataFinder,
httpClient: httpClient,
ipfsClient: ipfsClient,
arweaveClient: arweaveClient,
stg: stg,
tokenBucket: tokenBucket,
}
}
type tokenProcessingJob struct {
tp *tokenProcessor
id persist.DBID
token persist.TokenIdentifiers
contract persist.ContractIdentifiers
cause persist.ProcessingCause
pipelineMetadata *persist.PipelineMetadata
// profileImageKey is an optional key in the metadata that the pipeline should also process as a profile image.
// The pipeline only looks at the root level of the metadata for the key and will also not fail if the key is missing
// or if processing media for the key fails.
profileImageKey string
// refreshMetadata is an optional flag that indicates that the pipeline should check for new metadata when enabled
refreshMetadata bool
// startingMetadata is starting metadata to use to process media from. If empty or refreshMetadata is set, then the pipeline will try to get new metadata.
startingMetadata persist.TokenMetadata
// isSpamJob indicates that the job is processing a spam token. It's currently used to exclude events from Sentry.
isSpamJob bool
// isFxhash indicates that the job is processing a fxhash token.
isFxhash bool
// requireImage indicates that the pipeline should return an error if an image URL is present but an image wasn't cached.
requireImage bool
// requireFxHashSigned indicates that the pipeline should return an error if the token is FxHash but it isn't signed yet.
requireFxHashSigned bool
//fxHashIsSignedF is called to determine if a token is signed by Fxhash. It's currently used to determine if the token should be retried at a later time if it is not signed yet.
fxHashIsSignedF func(persist.TokenMetadata) bool
// imgKeywords are fields in the token's metadata that the pipeline should treat as images. If imgKeywords is empty, the chain's default keywords are used instead.
imgKeywords []string
// animKeywords are fields in the token's metadata that the pipeline should treat as animations. If animKeywords is empty, the chain's default keywords are used instead.
animKeywords []string
// placeHolderImageURL is an image URL that is downloaded from if processing from metadata fails
placeHolderImageURL string
}
type PipelineOption func(*tokenProcessingJob)
type pOpts struct{}
var PipelineOpts pOpts
func (pOpts) WithRefreshMetadata() PipelineOption {
return func(j *tokenProcessingJob) {
j.refreshMetadata = true
}
}
func (pOpts) WithStartingMetadata(t persist.TokenMetadata) PipelineOption {
return func(j *tokenProcessingJob) {
j.startingMetadata = t
}
}
func (pOpts) WithIsSpamJob(isSpamJob bool) PipelineOption {
return func(j *tokenProcessingJob) {
j.isSpamJob = isSpamJob
}
}
func (pOpts) WithRequireImage() PipelineOption {
return func(j *tokenProcessingJob) {
j.requireImage = true
}
}
func (pOpts) WithRequireProhibitionimage(td db.TokenDefinition, c db.Contract) PipelineOption {
return func(j *tokenProcessingJob) {
if platform.IsProhibition(c.Chain, c.Address) {
j.requireImage = true
j.refreshMetadata = true
}
}
}
func (pOpts) WithRequireFxHashSigned(td db.TokenDefinition, c db.Contract) PipelineOption {
return func(j *tokenProcessingJob) {
if td.IsFxhash {
j.isFxhash = true
j.requireFxHashSigned = true
j.fxHashIsSignedF = func(m persist.TokenMetadata) bool { return platform.IsFxhashSigned(td, c, m) }
j.requireImage = true
j.refreshMetadata = true
}
}
}
func (pOpts) WithRequireHighlightImage(td db.TokenDefinition, c db.Contract) PipelineOption {
return func(j *tokenProcessingJob) {
if isHighlight(td) {
j.requireImage = true
j.refreshMetadata = true
}
}
}
func (pOpts) WithKeywords(td db.TokenDefinition) PipelineOption {
return func(j *tokenProcessingJob) {
j.imgKeywords, j.animKeywords = platform.KeywordsFor(td)
}
}
func (pOpts) WithPlaceholderImageURL(u string) PipelineOption {
return func(j *tokenProcessingJob) {
j.placeHolderImageURL = u
}
}
func (pOpts) WithEnsProfileImage(c db.Contract) PipelineOption {
return func(j *tokenProcessingJob) {
if platform.IsEns(c.Chain, c.Address) {
j.profileImageKey = "profile_image"
}
}
}
type ErrImageResultRequired struct{ Err error }
func (e ErrImageResultRequired) Unwrap() error { return e.Err }
func (e ErrImageResultRequired) Error() string {
msg := "failed to process required image"
if e.Err != nil {
msg += ": " + e.Err.Error()
}
return msg
}
// ErrRequiredSignedToken indicates that the token isn't signed
var ErrRequiredSignedToken = errors.New("token isn't signed")
func wrapWithBadTokenErr(err error) error {
if errors.Is(err, media.ErrNoMediaURLs) || util.ErrorIs[errInvalidMedia](err) || util.ErrorIs[errNoDataFromReader](err) || errors.Is(err, ErrRequiredSignedToken) {
err = tokenmanage.ErrBadToken{Err: err}
}
return err
}
func (tpj *tokenProcessingJob) run(ctx context.Context) (persist.Media, persist.TokenMetadata, error) {
ctx = logger.NewContextWithFields(ctx, logrus.Fields{"runID": tpj.id})
logger.For(ctx).Infof("starting pipeline for token=%s", tpj.token)
var (
imgURL media.ImageURL
pfpURL media.ImageURL
animURL media.AnimationURL
metadata persist.TokenMetadata
tokenMedia persist.Media = persist.Media{MediaType: persist.MediaTypeUnknown}
err error
)
if !tpj.refreshMetadata {
imgURL, pfpURL, animURL, err = tpj.urlsToDownload(ctx, tpj.startingMetadata)
if err != nil && !errors.Is(err, media.ErrNoMediaURLs) {
return tokenMedia, tpj.startingMetadata, wrapWithBadTokenErr(err)
}
}
func() {
metadataCallback, ctx := persist.TrackStepStatus(ctx, &tpj.pipelineMetadata.MetadataRetrieval, "MetadataRetrieval")
defer metadataCallback()
metadata, err = tpj.tp.metadataFinder.GetMetadata(ctx, tpj.token)
if err != nil {
return
}
// try to get URLs from new metadata
imgURL, pfpURL, animURL, err = tpj.urlsToDownload(ctx, metadata)
if err == nil || (err != nil && !errors.Is(err, media.ErrNoMediaURLs)) {
return
}
// use the starting metadata
metadata = tpj.startingMetadata
imgURL, pfpURL, animURL, err = tpj.urlsToDownload(ctx, metadata)
}()
if err != nil {
return tokenMedia, metadata, wrapWithBadTokenErr(err)
}
tokenMedia, err = tpj.cacheMediaFromURLs(ctx, imgURL, pfpURL, animURL, metadata,
tpj.requireImage && imgURL != "",
tpj.requireFxHashSigned,
)
return tokenMedia, metadata, err
}
func (tpj *tokenProcessingJob) createErrFromResults(animResult cacheResult, imgResult cacheResult, metadata persist.TokenMetadata, requireImg, requireSigned bool) error {
if requireImg && !imgResult.IsSuccess() {
return ErrImageResultRequired{Err: wrapWithBadTokenErr(imgResult.err)}
}
if requireSigned && !tpj.fxHashIsSignedF(metadata) {
return wrapWithBadTokenErr(ErrRequiredSignedToken)
}
if animResult.IsSuccess() || imgResult.IsSuccess() {
return nil
}
if imgResult.err != nil {
return wrapWithBadTokenErr(imgResult.err)
}
return wrapWithBadTokenErr(animResult.err)
}
func (tpj *tokenProcessingJob) urlsToDownload(ctx context.Context, metadata persist.TokenMetadata) (imgURL media.ImageURL, pfpURL media.ImageURL, animURL media.AnimationURL, err error) {
pfpURL = findProfileImageURL(metadata, tpj.profileImageKey)
imgURL, animURL, err = findImageAndAnimationURLs(ctx, metadata, tpj.imgKeywords, tpj.animKeywords, tpj.pipelineMetadata)
imgURL = media.ImageURL(rpc.RewriteURIToHTTP(string(imgURL), tpj.isFxhash))
pfpURL = media.ImageURL(rpc.RewriteURIToHTTP(string(pfpURL), tpj.isFxhash))
animURL = media.AnimationURL(rpc.RewriteURIToHTTP(string(animURL), tpj.isFxhash))
return imgURL, pfpURL, animURL, err
}
func (tpj *tokenProcessingJob) cacheFromURL(ctx context.Context, tids persist.TokenIdentifiers, defaultObjectType objectType, mediaURL string, subMeta *cachePipelineMetadata) chan cacheResult {
resultCh := make(chan cacheResult)
go func() {
cachedObjects, err := cacheObjectsFromURL(ctx, tids, mediaURL, defaultObjectType, tpj.tp.httpClient, tpj.tp.ipfsClient, tpj.tp.arweaveClient, tpj.tp.stg, tpj.tp.tokenBucket, subMeta)
resultCh <- cacheResult{cachedObjects, err}
}()
return resultCh
}
func (tpj *tokenProcessingJob) cacheMediaFromOriginalURLs(ctx context.Context, imgURL media.ImageURL, pfpURL media.ImageURL, animURL media.AnimationURL) (imgResult, pfpResult, animResult cacheResult) {
imgRunMetadata := &cachePipelineMetadata{
ContentHeaderValueRetrieval: &tpj.pipelineMetadata.ImageContentHeaderValueRetrieval,
ReaderRetrieval: &tpj.pipelineMetadata.ImageReaderRetrieval,
DetermineMediaTypeWithReader: &tpj.pipelineMetadata.ImageDetermineMediaTypeWithReader,
AnimationGzip: &tpj.pipelineMetadata.ImageAnimationGzip,
SVGRasterize: &tpj.pipelineMetadata.ImageSVGRasterize,
StoreGCP: &tpj.pipelineMetadata.ImageStoreGCP,
ThumbnailGCP: &tpj.pipelineMetadata.ImageThumbnailGCP,
LiveRenderGCP: &tpj.pipelineMetadata.ImageLiveRenderGCP,
}
pfpRunMetadata := &cachePipelineMetadata{
ContentHeaderValueRetrieval: &tpj.pipelineMetadata.ProfileImageContentHeaderValueRetrieval,
ReaderRetrieval: &tpj.pipelineMetadata.ProfileImageReaderRetrieval,
DetermineMediaTypeWithReader: &tpj.pipelineMetadata.ProfileImageDetermineMediaTypeWithReader,
AnimationGzip: &tpj.pipelineMetadata.ProfileImageAnimationGzip,
SVGRasterize: &tpj.pipelineMetadata.ProfileImageSVGRasterize,
StoreGCP: &tpj.pipelineMetadata.ProfileImageStoreGCP,
ThumbnailGCP: &tpj.pipelineMetadata.ProfileImageThumbnailGCP,
LiveRenderGCP: &tpj.pipelineMetadata.ProfileImageLiveRenderGCP,
}
animRunMetadata := &cachePipelineMetadata{
ContentHeaderValueRetrieval: &tpj.pipelineMetadata.AnimationContentHeaderValueRetrieval,
ReaderRetrieval: &tpj.pipelineMetadata.AnimationReaderRetrieval,
DetermineMediaTypeWithReader: &tpj.pipelineMetadata.AnimationDetermineMediaTypeWithReader,
AnimationGzip: &tpj.pipelineMetadata.AnimationAnimationGzip,
SVGRasterize: &tpj.pipelineMetadata.AnimationSVGRasterize,
StoreGCP: &tpj.pipelineMetadata.AnimationStoreGCP,
ThumbnailGCP: &tpj.pipelineMetadata.AnimationThumbnailGCP,
LiveRenderGCP: &tpj.pipelineMetadata.AnimationLiveRenderGCP,
}
return tpj.cacheMediaSources(ctx, imgURL, pfpURL, animURL, imgRunMetadata, pfpRunMetadata, animRunMetadata)
}
func (tpj *tokenProcessingJob) cacheMediaFromPlaceholder(ctx context.Context) (imgResult, animResult cacheResult) {
if tpj.placeHolderImageURL == "" {
return cacheResult{}, cacheResult{}
}
imgRunMetadata := &cachePipelineMetadata{
ContentHeaderValueRetrieval: &tpj.pipelineMetadata.AlternateImageContentHeaderValueRetrieval,
ReaderRetrieval: &tpj.pipelineMetadata.AlternateImageReaderRetrieval,
DetermineMediaTypeWithReader: &tpj.pipelineMetadata.AlternateImageDetermineMediaTypeWithReader,
AnimationGzip: &tpj.pipelineMetadata.AlternateImageAnimationGzip,
SVGRasterize: &tpj.pipelineMetadata.AlternateImageSVGRasterize,
StoreGCP: &tpj.pipelineMetadata.AlternateImageStoreGCP,
ThumbnailGCP: &tpj.pipelineMetadata.AlternateImageThumbnailGCP,
LiveRenderGCP: &tpj.pipelineMetadata.AlternateImageLiveRenderGCP,
}
imgResult, _, animResult = tpj.cacheMediaSources(ctx, media.ImageURL(tpj.placeHolderImageURL), "", "", imgRunMetadata, nil, nil)
return imgResult, animResult
}
func (tpj *tokenProcessingJob) cacheMediaSources(
ctx context.Context,
imgURL media.ImageURL,
pfpURL media.ImageURL,
animURL media.AnimationURL,
imgRunMetadata *cachePipelineMetadata,
pfpRunMetadata *cachePipelineMetadata,
animRunMetadata *cachePipelineMetadata,
) (imgResult, pfpResult, animResult cacheResult) {
var imgCh, pfpCh, animCh chan cacheResult
if imgURL != "" {
imgCh = tpj.cacheFromURL(ctx, tpj.token, objectTypeImage, string(imgURL), imgRunMetadata)
}
if pfpURL != "" {
pfpCh = tpj.cacheFromURL(ctx, tpj.token, objectTypeProfileImage, string(pfpURL), pfpRunMetadata)
}
if animURL != "" {
animCh = tpj.cacheFromURL(ctx, tpj.token, objectTypeAnimation, string(animURL), animRunMetadata)
}
if imgCh != nil {
imgResult = <-imgCh
}
if pfpCh != nil {
pfpResult = <-pfpCh
if pfpResult.err != nil {
logger.For(ctx).Errorf("error caching profile image source: %s", pfpResult.err)
}
}
if animCh != nil {
animResult = <-animCh
}
return imgResult, pfpResult, animResult
}
func (tpj *tokenProcessingJob) cacheMediaFromURLs(ctx context.Context, imgURL, pfpURL media.ImageURL, animURL media.AnimationURL, metadata persist.TokenMetadata, requireImg, requireSigned bool) (m persist.Media, err error) {
ctx = logger.NewContextWithFields(ctx, logrus.Fields{
"imgURL": imgURL,
"pfpURL": pfpURL,
"animURL": animURL,
"requireImg": requireImg,
"requireSigned": requireSigned,
})
traceCallback, ctx := persist.TrackStepStatus(ctx, &tpj.pipelineMetadata.CreateMedia, "CreateMedia")
defer traceCallback()
imgResult, pfpResult, animResult := tpj.cacheMediaFromOriginalURLs(ctx, imgURL, pfpURL, animURL)
if (!requireImg && animResult.IsSuccess()) || imgResult.IsSuccess() {
err = tpj.createErrFromResults(animResult, imgResult, metadata, requireImg, requireSigned)
return createMediaFromResults(ctx, tpj, animResult, imgResult, pfpResult), err
}
// If there is a placeholder URL available, use that instead.
placeHolderImgResult, placeHolderAnimResult := tpj.cacheMediaFromPlaceholder(ctx)
if !imgResult.IsSuccess() && placeHolderImgResult.IsSuccess() {
imgResult = placeHolderImgResult
}
if !animResult.IsSuccess() && placeHolderAnimResult.IsSuccess() {
animResult = placeHolderAnimResult
}
if animResult.IsSuccess() || imgResult.IsSuccess() {
err = tpj.createErrFromResults(animResult, imgResult, metadata, requireImg, requireSigned)
return createMediaFromResults(ctx, tpj, animResult, imgResult, pfpResult), err
}
traceCallback, ctx = persist.TrackStepStatus(ctx, &tpj.pipelineMetadata.NothingCachedWithErrors, "NothingCachedWithErrors")
defer traceCallback()
// At this point we don't have a way to make media so we return an error
err = tpj.createErrFromResults(animResult, imgResult, metadata, requireImg, requireSigned)
return mustCreateMediaFromErr(ctx, err, tpj), err
}
func (tpj *tokenProcessingJob) createMediaFromCachedObjects(ctx context.Context, objects []cachedMediaObject) persist.Media {
traceCallback, ctx := persist.TrackStepStatus(ctx, &tpj.pipelineMetadata.CreateMediaFromCachedObjects, "CreateMediaFromCachedObjects")
defer traceCallback()
in := map[objectType]cachedMediaObject{}
for _, obj := range objects {
cur, ok := in[obj.ObjectType]
if !ok {
in[obj.ObjectType] = obj
continue
}
if obj.MediaType.IsMorePriorityThan(cur.MediaType) {
in[obj.ObjectType] = obj
}
}
return createMediaFromCachedObjects(ctx, tpj.tp.tokenBucket, in)
}
func (tpj *tokenProcessingJob) createRawMedia(ctx context.Context, mediaType persist.MediaType, animURL, imgURL string, objects []cachedMediaObject) persist.Media {
traceCallback, ctx := persist.TrackStepStatus(ctx, &tpj.pipelineMetadata.CreateRawMedia, "CreateRawMedia")
defer traceCallback()
return createRawMedia(ctx, persist.NewTokenIdentifiers(tpj.contract.ContractAddress, tpj.token.TokenID, tpj.token.Chain), mediaType, tpj.tp.tokenBucket, animURL, imgURL, objects)
}
func (tpj *tokenProcessingJob) activeStatus(ctx context.Context, media persist.Media) bool {
traceCallback, _ := persist.TrackStepStatus(ctx, &tpj.pipelineMetadata.MediaResultComparison, "MediaResultComparison")
defer traceCallback()
return media.IsServable()
}
func toJSONB(v any) (pgtype.JSONB, error) {
var j pgtype.JSONB
err := j.Set(v)
return j, err
}
func (tpj *tokenProcessingJob) saveResult(ctx context.Context, media persist.Media, metadata persist.TokenMetadata) (db.TokenMedia, error) {
newMedia, err := toJSONB(media)
if err != nil {
return db.TokenMedia{}, err
}
newMetadata, err := toJSONB(metadata)
if err != nil {
return db.TokenMedia{}, err
}
name, description := findNameAndDescription(metadata)
params := db.InsertTokenPipelineResultsParams{
ProcessingJobID: tpj.id,
PipelineMetadata: *tpj.pipelineMetadata,
ProcessingCause: tpj.cause,
ProcessorVersion: "",
RetiringMediaID: persist.GenerateID(),
Chain: tpj.token.Chain,
ContractAddress: tpj.contract.ContractAddress,
TokenID: tpj.token.TokenID,
DecimalTokenID: tpj.token.TokenID.ToDecimalTokenID(),
NewMediaIsActive: tpj.activeStatus(ctx, media),
NewMediaID: persist.GenerateID(),
NewMedia: newMedia,
NewMetadata: newMetadata,
NewName: util.ToNullString(name, true),
NewDescription: util.ToNullString(description, true),
}
params.TokenProperties = persist.TokenProperties{
HasMetadata: len(metadata) > 0,
HasPrimaryMedia: media.MediaType.IsValid() && media.MediaURL != "",
HasThumbnail: media.ThumbnailURL != "",
HasLiveRender: media.LivePreviewURL != "",
HasDimensions: media.Dimensions.Valid(),
HasName: params.NewName.String != "",
HasDescription: params.NewDescription.String != "",
}
r, err := tpj.tp.queries.InsertTokenPipelineResults(ctx, params)
return r.TokenMedia, err
}