From 319d52871d01ac8106e44609e7074875d06e4d5d Mon Sep 17 00:00:00 2001 From: Andriy Biletsky Date: Thu, 22 Aug 2024 04:28:26 +0700 Subject: [PATCH] Implement 'resolve' patching, fix repeating thumbnails --- app/arweave/arweave.go | 35 ++++++++++++++++++++++++++++++++--- app/arweave/arweave_test.go | 24 +++++++++++++++++++++--- app/query/caller.go | 1 + app/query/processors.go | 23 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/app/arweave/arweave.go b/app/arweave/arweave.go index 2f100050..8478532b 100644 --- a/app/arweave/arweave.go +++ b/app/arweave/arweave.go @@ -10,7 +10,7 @@ import ( func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string) (any, error) { var origUrls []string - urlPaths := map[string]string{} + urlPaths := map[string][]string{} jsonData, err := json.Marshal(structure) if err != nil { @@ -22,7 +22,11 @@ func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string) urlPath := fmt.Sprintf("%s.%s.%s", collPath, key.String(), itemPath) url := gjson.GetBytes(jsonData, urlPath).String() origUrls = append(origUrls, url) - urlPaths[url] = urlPath + if slice, exists := urlPaths[url]; exists { + urlPaths[url] = append(slice, urlPath) + } else { + urlPaths[url] = []string{urlPath} + } return true }) @@ -33,7 +37,7 @@ func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string) } for oldURL, newURL := range subsUrls { - if path, exists := urlPaths[oldURL]; exists { + for _, path := range urlPaths[oldURL] { jsonData, _ = sjson.SetBytes(jsonData, path, newURL) } } @@ -42,6 +46,31 @@ func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string) return d, json.Unmarshal(jsonData, &d) } +func ReplaceAssetUrl(baseUrl string, structure any, path string) (any, error) { + jsonData, err := json.Marshal(structure) + if err != nil { + return nil, err + } + + origUrl := gjson.GetBytes(jsonData, path).String() + + resolver := NewAssetResolver(baseUrl) + subsUrls, err := resolver.ResolveUrls([]string{origUrl}) + + if err != nil { + return nil, err + } + if newUrl, ok := subsUrls[origUrl]; ok { + jsonData, err = sjson.SetBytes(jsonData, path, newUrl) + if err != nil { + return nil, err + } + } + + var d any + return d, json.Unmarshal(jsonData, &d) +} + func GetClaimUrl(baseUrl, claim_id string) (string, error) { resolver := NewAssetResolver(baseUrl) r, err := resolver.ResolveClaims([]string{claim_id}) diff --git a/app/arweave/arweave_test.go b/app/arweave/arweave_test.go index b64dca1e..09791c0a 100644 --- a/app/arweave/arweave_test.go +++ b/app/arweave/arweave_test.go @@ -3,6 +3,7 @@ package arweave import ( "encoding/json" "os" + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -10,7 +11,7 @@ import ( "github.com/ybbus/jsonrpc" ) -func TestQueryParamsAsMap(t *testing.T) { +func TestReplaceAssetUrls(t *testing.T) { require := require.New(t) assert := assert.New(t) @@ -18,11 +19,28 @@ func TestQueryParamsAsMap(t *testing.T) { require.NoError(err) var resp jsonrpc.RPCResponse require.NoError(json.Unmarshal(f, &resp)) - // result := ReplaceAssetUrls("http://odycdn.com", resp.Result, "result.items", "signing_channel.value.thumbnail.url") result, err := ReplaceAssetUrls("http://odycdn.com", resp.Result, "items", "value.thumbnail.url") require.NoError(err) out, err := json.MarshalIndent(result, "", " ") require.NoError(err) - assert.Regexp(`http://odycdn.com/explore/\w{64}\?filename=\w{64}\.webp`, string(out)) + re := regexp.MustCompile(`http://odycdn.com/explore/\w{64}\?filename=\w{64}\.webp`) + matches := re.FindAllString(string(out), -1) + assert.Equal(2, len(matches)) +} + +func TestReplaceAssetUrl(t *testing.T) { + require := require.New(t) + assert := assert.New(t) + + f, err := os.ReadFile("../../resolve.json") + require.NoError(err) + var resp jsonrpc.RPCResponse + require.NoError(json.Unmarshal(f, &resp)) + result, err := ReplaceAssetUrl("http://odycdn.com", resp.Result.(map[string]any)["lbry://@MySillyReactions#d1ae6a9097b44691d318a5bfc6dc1240311c75e2"], "value.thumbnail.url") + require.NoError(err) + + out, err := json.MarshalIndent(result, "", " ") + require.NoError(err) + assert.Regexp(`http://odycdn.com/explore/\w{64}\?filename=\w{64}\.jpg`, string(out)) } diff --git a/app/query/caller.go b/app/query/caller.go index 2dd57533..37b2a3e7 100644 --- a/app/query/caller.go +++ b/app/query/caller.go @@ -160,6 +160,7 @@ func (c *Caller) addDefaultHooks() { c.AddPreflightHook(MethodClaimSearch, preflightHookClaimSearch, builtinHookName) c.AddPostflightHook(MethodClaimSearch, postClaimSearchArfleetThumbs, builtinHookName) + c.AddPostflightHook(MethodResolve, postResolveArfleetThumbs, builtinHookName) } func (c *Caller) CloneWithoutHook(endpoint, method, name string) *Caller { diff --git a/app/query/processors.go b/app/query/processors.go index 3fc6507a..1ec436e0 100644 --- a/app/query/processors.go +++ b/app/query/processors.go @@ -482,6 +482,29 @@ func postClaimSearchArfleetThumbs(_ *Caller, ctx context.Context) (*jsonrpc.RPCR return resp, nil } +func postResolveArfleetThumbs(_ *Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) { + logger := zapadapter.NewKV(nil).With("module", "query.preprocessors") + baseUrl := config.GetArfleetCDN() + + resp := GetResponse(ctx) + claims, ok := resp.Result.(map[string]any) + if !ok { + logger.Warn("error processing resolve response", "result", resp.Result) + } + var claimUrl string + var claim any + for k, v := range claims { + claimUrl, claim = k, v + } + pClaim, err := arweave.ReplaceAssetUrl(baseUrl, claim, "value.thumbnail.url") + if err != nil { + logger.Warn("error replacing asset url", "err", err) + return resp, nil + } + resp.Result = map[string]any{claimUrl: pClaim} + return resp, nil +} + func sliceContains[V comparable](cont []V, items ...V) bool { for _, t := range cont { for _, i := range items {