-
Notifications
You must be signed in to change notification settings - Fork 135
Implemented Cascade storage method in upload functionality #476
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
Open
mastercodercat
wants to merge
4
commits into
metaplex-foundation:main
Choose a base branch
from
mastercodercat:cascade-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a86cca0
Implemented Cascade storage method in upload functionality
henry-binary 7fdc4ae
cascade_id added to metadata and cache file and also updated docs abo…
henry-binary 72cef31
fix config documentation and comment
mastercodercat 33a15b2
add sense support
mastercodercat 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| [View code on GitHub](https://github.com/metaplex-foundation/sugar/src/upload/methods/cascade.rs) | ||
|
|
||
| The code in this file is responsible for uploading files to the Pastel's Cascade service. It defines the `CascadeStorageMethod` struct and implements the `Prepare` and `Uploader` traits for it. The main purpose of this code is to handle the process of uploading files to NFT Storage while adhering to the service's limitations, such as file size and request rate limits. | ||
|
|
||
| The `CascadeStorageMethod` struct contains an `Arc<Client>` for making HTTP requests. The `new` method initializes the struct by creating an HTTP client with the necessary headers, including the authentication token. | ||
|
|
||
| The `prepare` method, which is part of the `Prepare` trait implementation, checks if any file in the provided asset pairs exceeds the 100MB file size limit. If any file is too large, an error is returned. | ||
|
|
||
| The `upload` method, which is part of the `Uploader` trait implementation, is responsible for uploading the files to Cascade Protocol. It first groups the files into batches, ensuring that each batch does not exceed the file size and count limits. Then, it iterates through the batches and uploads them using a multipart HTTP request. If the upload is successful, the cache is updated with the new file URLs and active registration id of Cascade, and the progress bar is incremented. If an error occurs during the upload, it is added to a list of errors that is returned at the end of the method. The | ||
|
|
||
| To avoid hitting the rate limit, the code waits for a specified duration (`REQUEST_WAIT`) between uploading batches. Additionally, an `interrupted` flag is used to stop the upload process if needed. | ||
|
|
||
| Here's an example of how this code might be used in the larger project: | ||
|
|
||
| ```rust | ||
| let config_data = ConfigData::load("config.toml")?; | ||
| let cascade_storage_method = CascadeStorageMethod::new(&config_data).await?; | ||
|
|
||
| let sugar_config = SugarConfig::load("sugar_config.toml")?; | ||
| let asset_pairs = load_asset_pairs(&sugar_config)?; | ||
| let asset_indices = get_asset_indices(&asset_pairs)?; | ||
|
|
||
| cascade_storage_method.prepare(&sugar_config, &asset_pairs, asset_indices).await?; | ||
|
|
||
| let mut cache = Cache::load("cache.toml")?; | ||
| let mut assets = prepare_assets(&asset_pairs, &cache)?; | ||
| let progress = ProgressBar::new(assets.len() as u64); | ||
| let interrupted = Arc::new(AtomicBool::new(false)); | ||
|
|
||
| let errors = cascade_storage_method | ||
| .upload(&sugar_config, &mut cache, DataType::Image, &mut assets, &progress, interrupted) | ||
| .await?; | ||
| ``` | ||
|
|
||
| This example demonstrates how to initialize the `CascadeStorageMethod`, prepare the assets for upload, and then upload them using the `upload` method. | ||
| ## Questions: | ||
| 1. **Question**: What is the purpose of the `CascadeStorageMethod` struct and its associated methods? | ||
| **Answer**: The `CascadeStorageMethod` struct is used to handle the interaction with the Cascade Protocol API. It provides methods for initializing a new instance with the necessary authentication, preparing the assets for upload by checking file size limits, and uploading the assets to the Cascade Protocol API. | ||
|
|
||
| 2. **Question**: What are the constants defined at the beginning of the code and what are their purposes? | ||
| **Answer**: The constants defined at the beginning of the code are: | ||
| - `CASCADE_STORAGE_API_URL`: The base URL for the Cascade Protocol API. | ||
| - `REQUEST_WAIT`: The time window (in milliseconds) to wait between requests to avoid rate limits. | ||
| - `FILE_SIZE_LIMIT`: The maximum file size allowed for upload (100 MB). | ||
| - `FILE_COUNT_LIMIT`: The maximum number of files allowed per request. | ||
|
|
||
| 3. **Question**: How does the `upload` method handle uploading assets in batches? | ||
| **Answer**: The `upload` method first groups the assets into batches based on the file size and count limits. It then iterates through each batch, creating a multipart form with the assets, and sends a POST request to the Cascade Protocol API. After each successful upload, the cache is updated, and the progress bar is incremented. If there are more batches to process, the method waits for a specified duration to avoid rate limits before proceeding with the next batch. |
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
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
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.