Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 4.06 KB

File metadata and controls

48 lines (34 loc) · 4.06 KB

View code on GitHub

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:

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.