-
Notifications
You must be signed in to change notification settings - Fork 8
feat: implement blob/retrieve capability
#284
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68f7d63
feat: wip implement access grant
alanshaw ee65714
feat: wip grant blob retrieve access
alanshaw 6f4e161
feat: wip threading validation context
alanshaw f4873e9
feat: complete impl and and tests
alanshaw bbad54d
chore: mod tidy
alanshaw afb0092
fix: listen on retrieval path
alanshaw 9ec0bce
feat: implement blob/retrieve capability
alanshaw 338d022
refactor: common handler for blob retrievals
alanshaw 9398236
feat: add common handler
alanshaw 5bda269
refactor: use nil value
alanshaw 186ed4c
Merge branch 'main' into ash/feat/implement-blob-retrieve
alanshaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package spacecontent | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| logging "github.com/ipfs/go-log/v2" | ||
| "github.com/multiformats/go-multihash" | ||
| "github.com/storacha/go-libstoracha/capabilities/space/content" | ||
| "github.com/storacha/go-libstoracha/digestutil" | ||
| "github.com/storacha/go-ucanto/core/invocation" | ||
| "github.com/storacha/go-ucanto/core/result" | ||
| "github.com/storacha/go-ucanto/core/result/failure" | ||
| "github.com/storacha/go-ucanto/server/retrieval" | ||
| "github.com/storacha/piri/pkg/store" | ||
| "github.com/storacha/piri/pkg/store/blobstore" | ||
| ) | ||
|
|
||
| var log = logging.Logger("retrieval/handlers/spacecontent") | ||
|
|
||
| func Retrieve( | ||
| ctx context.Context, | ||
| blobs blobstore.BlobGetter, | ||
| inv invocation.Invocation, | ||
| digest multihash.Multihash, | ||
| byteRange blobstore.Range, | ||
| ) (result.Result[content.RetrieveOk, failure.IPLDBuilderFailure], retrieval.Response, error) { | ||
| digestStr := digestutil.Format(digest) | ||
| start := byteRange.Start | ||
| end := byteRange.End | ||
| rangeStr := fmt.Sprintf("%d-", start) | ||
| if end != nil { | ||
| rangeStr += fmt.Sprintf("%d", end) | ||
| } | ||
|
|
||
| cap := inv.Capabilities()[0] | ||
| log := log.With("iss", inv.Issuer().DID(), "can", cap.Can(), "with", cap.With(), "digest", digestStr, "range", rangeStr) | ||
|
|
||
| var getOpts []blobstore.GetOption | ||
| if start > 0 || end != nil { | ||
| getOpts = append(getOpts, blobstore.WithRange(start, end)) | ||
| } | ||
|
|
||
| blob, err := blobs.Get(ctx, digest, getOpts...) | ||
| if err != nil { | ||
| if errors.Is(err, store.ErrNotFound) { | ||
| log.Debugw("blob not found", "status", http.StatusNotFound) | ||
| notFoundErr := content.NewNotFoundError(fmt.Sprintf("blob not found: %s", digestStr)) | ||
| res := result.Error[content.RetrieveOk, failure.IPLDBuilderFailure](notFoundErr) | ||
| resp := retrieval.NewResponse(http.StatusNotFound, nil, nil) | ||
| return res, resp, nil | ||
| } else if errors.Is(err, blobstore.ErrRangeNotSatisfiable) { | ||
| log.Debugw("range not satisfiable", "status", http.StatusRequestedRangeNotSatisfiable) | ||
| rangeNotSatisfiableErr := content.NewRangeNotSatisfiableError(fmt.Sprintf("range not satisfiable: %d-%d", start, end)) | ||
| res := result.Error[content.RetrieveOk, failure.IPLDBuilderFailure](rangeNotSatisfiableErr) | ||
| resp := retrieval.NewResponse(http.StatusRequestedRangeNotSatisfiable, nil, nil) | ||
| return res, resp, nil | ||
| } | ||
| log.Errorw("getting blob", "error", err) | ||
| return nil, retrieval.Response{}, fmt.Errorf("getting blob: %w", err) | ||
| } | ||
|
|
||
| if end == nil { | ||
| rend := uint64(blob.Size() - 1) | ||
| end = &rend | ||
| } | ||
|
|
||
| res := result.Ok[content.RetrieveOk, failure.IPLDBuilderFailure](content.RetrieveOk{}) | ||
| status := http.StatusOK | ||
| contentLength := *end - start + 1 | ||
| headers := http.Header{} | ||
| headers.Set("Content-Length", fmt.Sprintf("%d", contentLength)) | ||
| headers.Set("Content-Type", "application/octet-stream") | ||
| headers.Set("Cache-Control", "public, max-age=29030400, immutable") | ||
| headers.Set("Etag", fmt.Sprintf(`"%s"`, digestStr)) | ||
| headers.Set("Vary", "Accept-Encoding") | ||
| if contentLength != uint64(blob.Size()) { | ||
| status = http.StatusPartialContent | ||
| headers.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, *end, blob.Size())) | ||
| headers.Add("Vary", "Range") | ||
| } | ||
| log.Debugw("serving bytes", "status", status, "size", contentLength) | ||
| resp := retrieval.NewResponse(status, headers, blob.Body()) | ||
| return res, resp, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package ucan | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/storacha/go-libstoracha/capabilities/blob" | ||
| "github.com/storacha/go-libstoracha/capabilities/space/content" | ||
| "github.com/storacha/go-ucanto/core/invocation" | ||
| "github.com/storacha/go-ucanto/core/receipt/fx" | ||
| "github.com/storacha/go-ucanto/core/result" | ||
| "github.com/storacha/go-ucanto/core/result/failure" | ||
| "github.com/storacha/go-ucanto/principal" | ||
| "github.com/storacha/go-ucanto/server" | ||
| "github.com/storacha/go-ucanto/server/retrieval" | ||
| "github.com/storacha/go-ucanto/ucan" | ||
| "github.com/storacha/piri/pkg/service/retrieval/handlers/spacecontent" | ||
| "github.com/storacha/piri/pkg/store/blobstore" | ||
| ) | ||
|
|
||
| // InvalidResourceErrorName is the name given to an error where the resource did | ||
| // not match the service DID. | ||
| const InvalidResourceErrorName = "InvalidResource" | ||
|
|
||
| type BlobRetrievalService interface { | ||
| ID() principal.Signer | ||
| Blobs() blobstore.BlobGetter | ||
| } | ||
|
|
||
| func BlobRetrieve(service BlobRetrievalService) retrieval.Option { | ||
| return retrieval.WithServiceMethod( | ||
| blob.RetrieveAbility, | ||
| retrieval.Provide( | ||
| blob.Retrieve, | ||
| func(ctx context.Context, cap ucan.Capability[blob.RetrieveCaveats], inv invocation.Invocation, iCtx server.InvocationContext, request retrieval.Request) (result.Result[blob.RetrieveOk, failure.IPLDBuilderFailure], fx.Effects, retrieval.Response, error) { | ||
| if cap.With() != service.ID().DID().String() { | ||
| return result.Error[blob.RetrieveOk, failure.IPLDBuilderFailure](blob.RetrieveError{ | ||
| ErrorName: InvalidResourceErrorName, | ||
| Message: fmt.Sprintf("resource is %s not %s", cap.With(), service.ID().DID()), | ||
| }), nil, retrieval.Response{}, nil | ||
| } | ||
| res, resp, err := spacecontent.Retrieve(ctx, service.Blobs(), inv, cap.Nb().Blob.Digest, blobstore.Range{}) | ||
| if err != nil { | ||
| return nil, nil, retrieval.Response{}, err | ||
| } | ||
| return result.MapOk(res, func(o content.RetrieveOk) blob.RetrieveOk { | ||
| return blob.RetrieveOk{} | ||
| }), nil, resp, nil | ||
| }, | ||
| ), | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.