Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle application-level/ipfs-based deletions #241

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions models/results/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package results

import (
"errors"
)

var ErrTokenNotFound = errors.New("token not found")

type Error struct {
Message string `json:"errorMessage"`
Type string `json:"errorType"`
Expand Down
12 changes: 4 additions & 8 deletions network/web2/metadata_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ func NewMetadataFetcher(options ...MetadataOption) *MetadataFetcher {
return &m
}

func (m *MetadataFetcher) Payload(_ context.Context, uri string) ([]byte, error) {
func (m *MetadataFetcher) Payload(_ context.Context, uri string) ([]byte, int, error) {

res, err := m.client.Get(uri)
if err != nil {
return nil, fmt.Errorf("could not execute request: %w", err)
return nil, 0, fmt.Errorf("could not execute request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != 200 {
return nil, fmt.Errorf("bad response code (%d)", res.StatusCode)
}

payload, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %w", err)
return nil, res.StatusCode, fmt.Errorf("could not read response body: %w", err)
}

return payload, nil
return payload, res.StatusCode, nil
}
4 changes: 2 additions & 2 deletions service/pipeline/addition_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package pipeline
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/nsqio/go-nsq"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -139,7 +139,7 @@ func (a *AdditionStage) process(payload []byte) error {
if err != nil {
return fmt.Errorf("could not decode execution error: %w", err)
}
if execErr != nil && strings.Contains(execErr.Error(), "URI query for nonexistent token") {
if errors.Is(execErr, results.ErrTokenNotFound) {
return a.delete(payload)
}
if execErr != nil {
Expand Down
35 changes: 34 additions & 1 deletion service/workers/addition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -169,13 +170,20 @@ func (a *AdditionHandler) Handle(ctx context.Context, addition *jobs.Addition) (

// Finally, we check if we have a payload already, or if we need to fetch it remotely.
var payload []byte
var code int
switch {

case strings.HasPrefix(privateURI, protocol.HTTP), strings.HasPrefix(privateURI, protocol.HTTPS):
payload, err = fetchMetadata.Payload(ctx, privateURI)
payload, code, err = fetchMetadata.Payload(ctx, privateURI)
if err != nil {
return nil, fmt.Errorf("could not fetch remote metadata: %w", err)
}
if code == http.StatusInternalServerError && isTokenNotFound(payload) {
return nil, results.ErrTokenNotFound
}
if code == http.StatusNotFound && isIPFSMissingLink(payload) {
return nil, results.ErrTokenNotFound
}
log.Debug().
Str("payload", string(payload)).
Msg("remote payload fetched")
Expand Down Expand Up @@ -264,3 +272,28 @@ func (a *AdditionHandler) Handle(ctx context.Context, addition *jobs.Addition) (

return &result, nil
}

func isTokenNotFound(payload []byte) bool {
var reqErr *results.Error
err := json.Unmarshal(payload, &reqErr)
if err != nil {
return false
}
if reqErr.Error() == "Token not found" {
return true
}
return false
}

func isIPFSMissingLink(payload []byte) bool {
var reqErr *results.Error
err := json.Unmarshal(payload, &reqErr)
if err != nil {
return false
}
if strings.Contains(reqErr.Error(), "URI query for nonexistent token") ||
strings.Contains(reqErr.Error(), "no link named") {
return true
}
return false
}