-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: map blobstore implements filesystemer #14
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5bc7d1
feat: map blobstore implements filesystemer
alanshaw b8598a4
chore: upgrade ucanto
alanshaw 845bab4
fix: handle request error
alanshaw 0d70890
chore: update ucanto
alanshaw 0cca01f
chore: upgrade ucanto
alanshaw 3e0a71e
chore: mod tidy
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 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 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 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 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 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 |
---|---|---|
|
@@ -28,7 +28,12 @@ func (srv *Server) Serve(mux *http.ServeMux) { | |
|
||
func NewHandler(server server.ServerView) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
res, _ := server.Request(ucanhttp.NewHTTPRequest(r.Body, r.Header)) | ||
res, err := server.Request(ucanhttp.NewHTTPRequest(r.Body, r.Header)) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
log.Errorf("handling UCAN request: %w", err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
for key, vals := range res.Headers() { | ||
for _, v := range vals { | ||
|
@@ -40,7 +45,7 @@ func NewHandler(server server.ServerView) func(http.ResponseWriter, *http.Reques | |
w.WriteHeader(res.Status()) | ||
} | ||
|
||
_, err := io.Copy(w, res.Body()) | ||
_, err = io.Copy(w, res.Body()) | ||
if err != nil { | ||
log.Errorf("sending UCAN response: %w", err) | ||
} | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"os" | ||
"path" | ||
|
||
"github.com/multiformats/go-multibase" | ||
"github.com/multiformats/go-multihash" | ||
"github.com/storacha/storage/pkg/internal/digestutil" | ||
"github.com/storacha/storage/pkg/store" | ||
|
@@ -71,13 +72,9 @@ type FsBlobstore struct { | |
tmpdir string | ||
} | ||
|
||
func (b *FsBlobstore) EncodePath(digest multihash.Multihash) string { | ||
return encodePath(digest) | ||
} | ||
|
||
// FileSystem returns a filesystem interface for reading blobs. | ||
func (b *FsBlobstore) FileSystem() http.FileSystem { | ||
return http.Dir(b.rootdir) | ||
return &fsDir{http.Dir(b.rootdir)} | ||
} | ||
|
||
func (b *FsBlobstore) Get(ctx context.Context, digest multihash.Multihash, opts ...GetOption) (Object, error) { | ||
|
@@ -194,3 +191,21 @@ func NewFsBlobstore(rootdir string, tmpdir string) (*FsBlobstore, error) { | |
} | ||
return &FsBlobstore{rootdir, tmpdir}, nil | ||
} | ||
|
||
type fsDir struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again you may want to look at fs.FS |
||
fs http.FileSystem | ||
} | ||
|
||
var _ http.FileSystem = (*fsDir)(nil) | ||
|
||
func (d *fsDir) Open(path string) (http.File, error) { | ||
_, bytes, err := multibase.Decode(path[1:]) | ||
if err != nil { | ||
return nil, fs.ErrNotExist | ||
} | ||
digest, err := multihash.Cast(bytes) | ||
if err != nil { | ||
return nil, fs.ErrNotExist | ||
} | ||
return d.fs.Open(encodePath(digest)) | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be simpler with ServeFile instead of FileServer.
also go errata: if you just want to use system utils, you may find ServeFileFS and FileServerFS easer cause you can just use some system utils (if this is actually helpful)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes - looks more appropriate. I didn't realize it existed! If I have a moment I'll look into replacing it.