pub trait Uploader: Prepare {
    fn upload<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
        &'life0 self,
        sugar_config: &'life1 SugarConfig,
        cache: &'life2 mut Cache,
        data_type: DataType,
        assets: &'life3 mut Vec<AssetInfo>,
        progress: &'life4 ProgressBar,
        interrupted: Arc<AtomicBool>
    ) -> Pin<Box<dyn Future<Output = Result<Vec<UploadError>>> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        'life2: 'async_trait,
        'life3: 'async_trait,
        'life4: 'async_trait,
        Self: 'async_trait
; }
Expand description

Types that can upload assets (files).

This trait should be implemented directly by upload methods that require full control on how the upload is performed. For methods that support parallel uploads (threading), consider implementing ParallelUploader instead.

Required Methods

Returns a vector UploadError with the errors (if any) after uploading all assets to the storage.

This function will be called to upload each type of asset separately.

Arguments
  • sugar_config - The current sugar configuration
  • cache - Asset cache object (mutable)
  • data_type - Type of the asset being uploaded
  • assets - Vector of assets to upload (mutable)
  • progress - Reference to the progress bar to provide feedback to the console
  • interrupted - Reference to the shared interruption handler flag to receive notifications
Examples

Implementations are expected to use the interrupted to control when the user aborts the upload process. In general, this would involve using it as a control of a loop:

while !interrupted.load(Ordering::SeqCst) {
    // continue with the upload
}

After uploading an asset, its information need to be updated in the cache and the cache synced to the file system. Syncing the cache to the file system might be slow for large collections, therefore it should be done as frequent as practical to avoid slowing down the upload process and, at the same time, minimizing the chances of information loss in case the user aborts the upload.

...
// once an asset has been upload

let id = asset_info.asset_id.clone();
let uri = "URI of the asset after upload";
// cache item to update
let item = cache.items.get_mut(&id).unwrap();

match data_type {
    DataType::Image => item.image_link = uri,
    DataType::Metadata => item.metadata_link = uri,
    DataType::Animation => item.animation_link = Some(uri),
}
// updates the progress bar
progress.inc(1);

...

// after several uploads
cache.sync_file()?;

Implementors

Default implementation of the trait ‘Uploader’ for all ‘ParallelUploader’.