-
Notifications
You must be signed in to change notification settings - Fork 35
webui: Content page #715
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
webui: Content page #715
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package webrpc | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/ipfs/go-cid" | ||
| "github.com/multiformats/go-multicodec" | ||
| "github.com/multiformats/go-multihash" | ||
| "golang.org/x/xerrors" | ||
|
|
||
| "github.com/filecoin-project/go-state-types/abi" | ||
|
|
||
| "github.com/filecoin-project/curio/lib/commcidv2" | ||
| ) | ||
|
|
||
| // ContentInfo represents information about content location | ||
| type ContentInfo struct { | ||
| PieceCID string `json:"piece_cid"` | ||
| Offset uint64 `json:"offset"` | ||
| Size uint64 `json:"size"` | ||
|
|
||
| Err string `json:"err"` | ||
| } | ||
|
|
||
| // FindContentByCID finds content by CID | ||
| func (a *WebRPC) FindContentByCID(ctx context.Context, cs string) ([]ContentInfo, error) { | ||
| cid, err := cid.Parse(cs) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if commcidv2.IsPieceCidV2(cid) || IsCidV1PieceCid(cid) { | ||
| _, pcid2, err := a.maybeUpgradePieceCid(ctx, cid) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("failed to upgrade piece cid: %w", err) | ||
| } | ||
| return []ContentInfo{ | ||
| { | ||
| PieceCID: pcid2.String(), | ||
| Offset: 0, | ||
| Size: 0, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| mh := cid.Hash() | ||
|
|
||
| offsets, err := a.deps.IndexStore.PiecesContainingMultihash(ctx, mh) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("pieces containing multihash %s: %w", mh, err) | ||
| } | ||
|
|
||
| var res []ContentInfo | ||
| for _, offset := range offsets { | ||
| off, err := a.deps.IndexStore.GetOffset(ctx, offset.PieceCidV2, mh) | ||
| if err != nil { | ||
| _, pcid2, err := a.maybeUpgradePieceCid(ctx, offset.PieceCidV2) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("failed to upgrade piece cid: %w", err) | ||
| } | ||
| res = append(res, ContentInfo{ | ||
| PieceCID: pcid2.String(), | ||
| Offset: off, | ||
| Size: uint64(offset.BlockSize), | ||
| Err: err.Error(), | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| _, pcid2, err := a.maybeUpgradePieceCid(ctx, offset.PieceCidV2) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("failed to upgrade piece cid: %w", err) | ||
| } | ||
| res = append(res, ContentInfo{ | ||
| PieceCID: pcid2.String(), | ||
| Offset: off, | ||
| Size: uint64(offset.BlockSize), | ||
| }) | ||
| } | ||
|
|
||
| return res, nil | ||
| } | ||
|
|
||
| func (a *WebRPC) maybeUpgradePieceCid(ctx context.Context, c cid.Cid) (bool, cid.Cid, error) { | ||
| if commcidv2.IsPieceCidV2(c) { | ||
| return true, c, nil | ||
| } | ||
|
|
||
| if !IsCidV1PieceCid(c) { | ||
| return false, c, nil | ||
| } | ||
|
|
||
| // Lookup piece_cid in market_piece_deal (always v1), get raw_size and piece_length | ||
|
|
||
| // raw_size = if mpd.raw_size == 0, then Padded(piece_length).Unpadded() else mpd.raw_size | ||
| var rawSize uint64 | ||
| var pieceLength uint64 | ||
|
|
||
| err := a.deps.DB.QueryRow(ctx, ` | ||
| SELECT COALESCE(raw_size, 0), piece_length | ||
| FROM market_piece_deal | ||
| WHERE piece_cid = $1 | ||
| `, c.String()).Scan(&rawSize, &pieceLength) | ||
| if err != nil { | ||
| return false, c, xerrors.Errorf("failed to lookup piece info: %w", err) | ||
| } | ||
|
|
||
| if rawSize == 0 { | ||
| rawSize = uint64(abi.PaddedPieceSize(pieceLength).Unpadded()) | ||
| } | ||
|
|
||
| pcid2, err := commcidv2.PieceCidV2FromV1(c, rawSize) | ||
| if err != nil { | ||
| return false, c, err | ||
| } | ||
|
|
||
| return true, pcid2, nil | ||
| } | ||
|
|
||
| func IsCidV1PieceCid(c cid.Cid) bool { | ||
| decoded, err := multihash.Decode(c.Hash()) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| filCodec := multicodec.Code(c.Type()) | ||
| filMh := multicodec.Code(decoded.Code) | ||
|
|
||
| // Check if it's a valid Filecoin commitment type | ||
| switch filCodec { | ||
| case multicodec.FilCommitmentUnsealed: | ||
| if filMh != multicodec.Sha2_256Trunc254Padded { | ||
| return false | ||
| } | ||
| /* case multicodec.FilCommitmentSealed: | ||
| if filMh != multicodec.PoseidonBls12_381A2Fc1 { | ||
| return false | ||
| } */ | ||
| default: | ||
| // Neither unsealed nor sealed commitment | ||
| return false | ||
| } | ||
|
|
||
| // Commitments must be exactly 32 bytes | ||
| if len(decoded.Digest) != 32 { | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } |
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,125 @@ | ||
| import { LitElement, html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js'; | ||
| import RPCCall from '/lib/jsonrpc.mjs'; | ||
|
|
||
| class ContentPage extends LitElement { | ||
| static properties = { | ||
| searchCid: { type: String }, | ||
| results: { type: Array }, | ||
| loading: { type: Boolean }, | ||
| error: { type: String } | ||
| }; | ||
|
|
||
| constructor() { | ||
| super(); | ||
| this.searchCid = ''; | ||
| this.results = []; | ||
| this.loading = false; | ||
| this.error = ''; | ||
| } | ||
|
|
||
| handleInput(e) { | ||
| this.searchCid = e.target.value; | ||
| } | ||
|
|
||
| async handleFind() { | ||
| if (!this.searchCid.trim()) { | ||
| this.error = 'Please enter a CID'; | ||
| return; | ||
| } | ||
|
|
||
| this.loading = true; | ||
| this.error = ''; | ||
| this.results = []; | ||
|
|
||
| try { | ||
| const results = await RPCCall('FindContentByCID', [this.searchCid.trim()]); | ||
| this.results = results || []; | ||
| if (this.results.length === 0) { | ||
| this.error = 'No content found for this CID'; | ||
| } | ||
| } catch (err) { | ||
| console.error('Error finding content:', err); | ||
| this.error = `Error: ${err.message || err}`; | ||
| } finally { | ||
| this.loading = false; | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| return html` | ||
| <link | ||
| href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" | ||
| rel="stylesheet" | ||
| integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" | ||
| crossorigin="anonymous" | ||
| /> | ||
| <link rel="stylesheet" href="/ux/main.css" onload="document.body.style.visibility = 'initial'"> | ||
|
|
||
| <div class="container"> | ||
| <h2>Find CID</h2> | ||
| <div class="search-container"> | ||
| <input | ||
| autofocus | ||
| type="text" | ||
| placeholder="Enter CID (baf...)" | ||
| .value="${this.searchCid}" | ||
| @input="${this.handleInput}" | ||
| @keypress="${(e) => e.key === 'Enter' && this.handleFind()}" | ||
| /> | ||
| <button | ||
| class="btn btn-primary" | ||
| @click="${this.handleFind}" | ||
| ?disabled="${this.loading}" | ||
| > | ||
| ${this.loading ? 'Searching...' : 'Find'} | ||
| </button> | ||
| </div> | ||
|
|
||
| ${this.error ? html` | ||
| <div class="alert alert-danger">${this.error}</div> | ||
| ` : ''} | ||
|
|
||
| ${this.results.length > 0 ? html` | ||
| <h3>Results</h3> | ||
| <table class="table table-dark table-striped"> | ||
| <thead> | ||
| <tr> | ||
| <th>Piece CID</th> | ||
| <th>Offset</th> | ||
| <th>Size</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| ${this.results.map(item => html` | ||
| <tr> | ||
| <td><a href="/pages/piece/?id=${item.piece_cid}">${item.piece_cid}</a></td> | ||
| <td>${item.err ? html`<span class="text-danger">${item.err}</span>` : item.offset}</td> | ||
| <td>${this.formatBytes(item.size)}</td> | ||
| </tr> | ||
| `)} | ||
| </tbody> | ||
| </table> | ||
| ` : ''} | ||
| </div> | ||
| `; | ||
| } | ||
|
|
||
| formatBytes(bytes) { | ||
| if (!bytes) return '0 Bytes'; | ||
| const k = 1024; | ||
| const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB']; | ||
| const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
| return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
| } | ||
|
|
||
| static styles = css` | ||
| .search-container { | ||
| display: grid; | ||
| grid-template-columns: 1fr auto; | ||
| grid-column-gap: 0.75rem; | ||
| margin-bottom: 1rem; | ||
| } | ||
| `; | ||
| } | ||
|
|
||
| customElements.define('content-page', ContentPage); |
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,26 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Content</title> | ||
| <script type="module" src="/ux/curio-ux.mjs"></script> | ||
| <script type="module" src="/ux/components/Drawer.mjs"></script> | ||
| <script type="module" src="content.mjs"></script> | ||
| </head> | ||
| <body style="visibility:hidden; background:rgb(11, 22, 34)" data-bs-theme="dark"> | ||
| <curio-ux> | ||
| <div class="page" style="margin-left: 20px; margin-right: 10px"> | ||
| <section class="section"> | ||
| <div class="row"> | ||
| <h1>Content</h1> | ||
| </div> | ||
| <div class="row"> | ||
| <div class="col-md-auto" style="max-width: 95%"> | ||
| <content-page></content-page> | ||
| </div> | ||
| </div> | ||
| </section> | ||
| </div> | ||
| </curio-ux> | ||
| </body> | ||
| </html> | ||
|
|
||
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
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.