1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use std::{
    borrow::Borrow,
    collections::HashSet,
    ffi::OsStr,
    fmt::Write as _,
    fs::OpenOptions,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
};

use console::style;

use crate::{
    cache::{load_cache, Cache},
    common::*,
    config::{get_config_data, SugarConfig},
    upload::*,
    utils::*,
    validate::format::Metadata,
};

pub struct UploadArgs {
    pub assets_dir: String,
    pub config: String,
    pub keypair: Option<String>,
    pub rpc_url: Option<String>,
    pub cache: String,
    pub interrupted: Arc<AtomicBool>,
}

pub struct AssetType {
    pub image: Vec<isize>,
    pub metadata: Vec<isize>,
    pub animation: Vec<isize>,
}

pub async fn process_upload(args: UploadArgs) -> Result<()> {
    let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
    let config_data = get_config_data(&args.config)?;

    // loading assets
    println!(
        "{} {}Loading assets",
        style("[1/4]").bold().dim(),
        ASSETS_EMOJI
    );

    let pb = spinner_with_style();
    pb.enable_steady_tick(120);
    pb.set_message("Reading files...");
    let asset_pairs = get_asset_pairs(&args.assets_dir)?;

    // creates/loads the cache
    let mut cache = load_cache(&args.cache, true)?;
    if asset_pairs.get(&-1).is_none() {
        cache.items.remove("-1");
    }

    // list of indices to upload
    let mut indices = AssetType {
        image: Vec::new(),
        metadata: Vec::new(),
        animation: Vec::new(),
    };

    for (index, pair) in &asset_pairs {
        // checks if we have complete URIs in the metadata file;
        // if true, no upload is necessary and we will use the
        // existing URIs

        let m: Metadata = {
            let m = OpenOptions::new()
                .read(true)
                .open(&pair.metadata)
                .map_err(|e| {
                    anyhow!(
                        "Failed to read metadata file '{}' with error: {}",
                        &pair.metadata,
                        e
                    )
                })?;
            serde_json::from_reader(&m)?
        };

        // retrieve the existring image uri from the metadata
        let existing_image = if is_complete_uri(&m.image) {
            m.image.clone()
        } else {
            String::new()
        };

        // retrieve the existring animation uri from the metadata
        let existing_animation = match m.animation_url {
            Some(ref url) => {
                if is_complete_uri(url) {
                    url.clone()
                } else {
                    String::new()
                }
            }
            None => String::new(),
        };

        match cache.items.get_mut(&index.to_string()) {
            Some(item) => {
                let image_changed = (!item.image_hash.eq(&pair.image_hash)
                    || item.image_link.is_empty())
                    && existing_image.is_empty();

                let animation_changed = (!item.animation_hash.eq(&pair.animation_hash)
                    || (item.animation_link.is_none() && pair.animation.is_some()))
                    && existing_animation.is_empty();

                let metadata_changed =
                    !item.metadata_hash.eq(&pair.metadata_hash) || item.metadata_link.is_empty();

                if image_changed {
                    // triggers the image upload
                    item.image_hash = pair.image_hash.clone();
                    item.image_link = String::new();
                    indices.image.push(*index);
                } else if !existing_image.is_empty() {
                    item.image_hash = pair.image_hash.clone();
                    item.image_link = existing_image;
                }

                if animation_changed {
                    // triggers the animation upload
                    item.animation_hash = pair.animation_hash.clone();
                    item.animation_link = None;
                    indices.animation.push(*index);
                } else if !existing_animation.is_empty() {
                    item.animation_hash = pair.animation_hash.clone();
                    item.animation_link = Some(existing_animation);
                }

                if metadata_changed || image_changed || animation_changed {
                    // triggers the metadata upload
                    item.metadata_hash = pair.metadata_hash.clone();
                    item.metadata_link = String::new();
                    item.on_chain = false;
                    // we need to upload metadata only
                    indices.metadata.push(*index);
                }
            }
            None => {
                let mut item = pair.clone().into_cache_item();

                // check if we need to upload the image
                if existing_image.is_empty() {
                    indices.image.push(*index);
                } else {
                    item.image_hash = pair.image_hash.clone();
                    item.image_link = existing_image;
                }

                // and we might need to upload the animation
                if pair.animation.is_some() {
                    if existing_animation.is_empty() {
                        indices.animation.push(*index);
                    } else {
                        item.animation_hash = pair.animation_hash.clone();
                        item.animation_link = Some(existing_animation);
                    }
                }

                indices.metadata.push(*index);
                cache.items.insert(index.to_string(), item);
            }
        }
        // sanity check: verifies that both symbol and seller-fee-basis-points are the
        // same as the ones in the config file
        let f = File::open(Path::new(&pair.metadata))?;
        match serde_json::from_reader(f) {
            Ok(metadata) => {
                let metadata: Metadata = metadata;
                // symbol check, but only if the asset actually has the value
                if let Some(symbol) = metadata.symbol {
                    if config_data.symbol.ne(&symbol) {
                        return Err(UploadError::MismatchValue(
                            "symbol".to_string(),
                            pair.metadata.clone(),
                            config_data.symbol,
                            symbol,
                        )
                        .into());
                    }
                }
                // seller-fee-basis-points check, but only if the asset actually has the value
                if let Some(seller_fee_basis_points) = metadata.seller_fee_basis_points {
                    if config_data.seller_fee_basis_points != seller_fee_basis_points {
                        return Err(UploadError::MismatchValue(
                            "seller_fee_basis_points".to_string(),
                            pair.metadata.clone(),
                            config_data.seller_fee_basis_points.to_string(),
                            seller_fee_basis_points.to_string(),
                        )
                        .into());
                    }
                }
            }
            Err(err) => {
                let error = anyhow!("Error parsing metadata ({}): {}", pair.metadata, err);
                error!("{:?}", error);
                return Err(error);
            }
        }
    }

    pb.finish_and_clear();

    println!(
        "Found {} asset pair(s), uploading files:",
        asset_pairs.len()
    );
    println!("+--------------------+");
    println!("| images    | {:>6} |", indices.image.len());
    println!("| metadata  | {:>6} |", indices.metadata.len());

    if !indices.animation.is_empty() {
        println!("| animation | {:>6} |", indices.animation.len());
    }

    println!("+--------------------+");

    // this should never happen, since every time we update the image file we
    // need to update the metadata
    if indices.image.len() > indices.metadata.len() {
        return Err(anyhow!(format!(
            "There are more image files ({}) to upload than metadata ({})",
            indices.image.len(),
            indices.metadata.len(),
        )));
    }

    let need_upload =
        !indices.image.is_empty() || !indices.metadata.is_empty() || !indices.animation.is_empty();

    // ready to upload data

    let mut errors = Vec::new();

    if need_upload {
        let total_steps = if indices.animation.is_empty() { 4 } else { 5 };
        println!(
            "\n{} {}Initializing upload",
            style(format!("[2/{}]", total_steps)).bold().dim(),
            COMPUTER_EMOJI
        );

        let pb = spinner_with_style();
        pb.set_message("Connecting...");

        let storage = initialize(&sugar_config, &config_data).await?;

        pb.finish_with_message("Connected");

        storage
            .prepare(
                &sugar_config,
                &asset_pairs,
                vec![
                    (DataType::Image, &indices.image),
                    (DataType::Animation, &indices.animation),
                    (DataType::Metadata, &indices.metadata),
                ],
            )
            .await?;

        // clear the interruption handler value ahead of the upload
        args.interrupted.store(false, Ordering::SeqCst);

        println!(
            "\n{} {}Uploading image files {}",
            style(format!("[3/{}]", total_steps)).bold().dim(),
            UPLOAD_EMOJI,
            if indices.image.is_empty() {
                "(skipping)"
            } else {
                ""
            }
        );

        if !indices.image.is_empty() {
            errors.extend(
                upload_data(
                    &sugar_config,
                    &asset_pairs,
                    &mut cache,
                    &indices.image,
                    DataType::Image,
                    storage.borrow(),
                    args.interrupted.clone(),
                )
                .await?,
            );

            // updates the list of metadata indices since the image upload
            // might fail - removes any index that the image upload failed
            if !indices.metadata.is_empty() {
                for index in indices.image {
                    let item = cache.items.get(&index.to_string()).unwrap();

                    if item.image_link.is_empty() {
                        // no image link, not ready for metadata upload
                        indices.metadata.retain(|&x| x != index);
                    }
                }
            }
        }

        if !indices.animation.is_empty() {
            println!(
                "\n{} {}Uploading animation files",
                style("[4/5]").bold().dim(),
                UPLOAD_EMOJI
            );
        }

        if !indices.animation.is_empty() {
            errors.extend(
                upload_data(
                    &sugar_config,
                    &asset_pairs,
                    &mut cache,
                    &indices.animation,
                    DataType::Animation,
                    storage.borrow(),
                    args.interrupted.clone(),
                )
                .await?,
            );

            // updates the list of metadata indices since the image upload
            // might fail - removes any index that the animation upload failed
            if !indices.metadata.is_empty() {
                for index in indices.animation {
                    let item = cache.items.get(&index.to_string()).unwrap();

                    if item.animation_link.is_none() {
                        // no animation link, not ready for metadata upload
                        indices.metadata.retain(|&x| x != index);
                    }
                }
            }
        }

        println!(
            "\n{} {}Uploading metadata files {}",
            style(format!("[{}/{}]", total_steps, total_steps))
                .bold()
                .dim(),
            UPLOAD_EMOJI,
            if indices.metadata.is_empty() {
                "(skipping)"
            } else {
                ""
            }
        );

        if !indices.metadata.is_empty() {
            errors.extend(
                upload_data(
                    &sugar_config,
                    &asset_pairs,
                    &mut cache,
                    &indices.metadata,
                    DataType::Metadata,
                    storage.borrow(),
                    args.interrupted.clone(),
                )
                .await?,
            );
        }
    } else {
        println!("\n....no files need uploading, skipping remaining steps.");
    }

    // move all non-numeric keys to the beginning and sort as strings
    // sort numeric keys as integers
    cache
        .items
        .sort_by(|key_a, _, key_b, _| -> std::cmp::Ordering {
            let a = key_a.parse::<i32>();
            let b = key_b.parse::<i32>();

            if a.is_err() && b.is_err() {
                // string, string
                key_a.cmp(key_b)
            } else if a.is_ok() && b.is_err() {
                // number, string
                std::cmp::Ordering::Greater
            } else if a.is_err() && b.is_ok() {
                // string, number
                std::cmp::Ordering::Less
            } else {
                // number, number
                a.unwrap().cmp(&b.unwrap())
            }
        });
    cache.sync_file()?;

    // sanity check

    let mut count = 0;

    for (index, item) in &cache.items.0 {
        let asset_pair = asset_pairs.get(&isize::from_str(index)?).unwrap();

        // we first check that the asset has an animation file; if there is one,
        // we need to check that the cache item has the link and the link is not empty
        let missing_animation_link = if asset_pair.animation.is_some() {
            if let Some(link) = &item.animation_link {
                link.is_empty()
            } else {
                true
            }
        } else {
            // the asset does not have animation file
            false
        };

        // only increment the count if the cache item is complete (all links are present)
        if !(item.image_link.is_empty() || item.metadata_link.is_empty() || missing_animation_link)
        {
            count += 1;
        }
    }

    println!(
        "\n{}",
        style(format!(
            "{}/{} asset pair(s) uploaded.",
            count,
            asset_pairs.len()
        ))
        .bold()
    );

    if count != asset_pairs.len() {
        let message = if !errors.is_empty() {
            let mut message = String::new();
            write!(
                message,
                "Failed to upload all files, {0} error(s) occurred:",
                errors.len()
            )?;

            let mut unique = HashSet::new();

            for err in errors {
                unique.insert(err.to_string());
            }

            for u in unique {
                message.push_str(&style("\n=> ").dim().to_string());
                message.push_str(&u);
            }

            message
        } else {
            "Not all files were uploaded.".to_string()
        };

        return Err(UploadError::Incomplete(message).into());
    }

    Ok(())
}

/// Upload the data to the selected storage.
async fn upload_data(
    sugar_config: &SugarConfig,
    asset_pairs: &HashMap<isize, AssetPair>,
    cache: &mut Cache,
    indices: &[isize],
    data_type: DataType,
    uploader: &dyn Uploader,
    interrupted: Arc<AtomicBool>,
) -> Result<Vec<UploadError>> {
    let mut extension = HashSet::with_capacity(1);
    let mut paths = Vec::new();

    for index in indices {
        let item = match asset_pairs.get(index) {
            Some(asset_index) => asset_index,
            None => return Err(anyhow::anyhow!("Failed to get asset at index {}", index)),
        };
        // chooses the file path based on the data type
        let file_path = match data_type {
            DataType::Image => item.image.clone(),
            DataType::Metadata => item.metadata.clone(),
            DataType::Animation => {
                if let Some(animation) = item.animation.clone() {
                    animation
                } else {
                    return Err(anyhow::anyhow!(
                        "Missing animation path for asset at index {}",
                        index
                    ));
                }
            }
        };

        let path = Path::new(&file_path);
        let ext = path
            .extension()
            .and_then(OsStr::to_str)
            .expect("Failed to convert extension from unicode");
        extension.insert(String::from(ext));

        paths.push(file_path);
    }

    // validates that all files have the same extension
    let extension = if extension.len() == 1 {
        extension.iter().next().unwrap()
    } else {
        return Err(anyhow!("Invalid file extension: {:?}", extension));
    };

    let content_type = match data_type {
        DataType::Image => format!("image/{}", extension),
        DataType::Metadata => "application/json".to_string(),
        DataType::Animation => format!("video/{}", extension),
    };

    // uploading data

    println!("\nSending data: (Ctrl+C to abort)");

    let pb = progress_bar_with_style(paths.len() as u64);

    let mut assets = Vec::new();

    for file_path in paths {
        // path to the media/metadata file
        let path = Path::new(&file_path);
        let file_name = String::from(
            path.file_name()
                .and_then(OsStr::to_str)
                .expect("Filed to get file name."),
        );
        let (asset_id, cache_item) = get_cache_item(path, cache)?;

        let content = match data_type {
            // replaces the media link without modifying the original file to avoid
            // changing the hash of the metadata file
            DataType::Metadata => get_updated_metadata(
                &file_path,
                &cache_item.image_link,
                &cache_item.animation_link,
            )?,
            _ => file_path.clone(),
        };

        assets.push(AssetInfo {
            asset_id: asset_id.to_string(),
            name: file_name,
            content,
            data_type: data_type.clone(),
            content_type: content_type.clone(),
        });
    }

    let errors = uploader
        .upload(
            sugar_config,
            cache,
            data_type,
            &mut assets,
            &pb,
            interrupted,
        )
        .await?;

    if !errors.is_empty() {
        pb.abandon_with_message(format!("{}", style("Upload failed ").red().bold()));
    } else {
        pb.finish_with_message(format!("{}", style("Upload successful ").green().bold()));
    }

    // makes sure the cache file is updated
    cache.sync_file()?;

    Ok(errors)
}