cloud_image_download/
cloud_image.rs1use crate::checksums::CheckSums;
3use crate::download::get_filename_destination;
4use crate::image_history::DbImageHistory;
5use crate::website::Url;
6use chrono::NaiveDateTime;
7use colored::Colorize;
8use log::{error, info, warn};
9use std::fmt;
10use std::path::Path;
11
12#[derive(Default, PartialEq, Debug)]
13pub struct CloudImage {
14 pub url: Url,
15 pub name: String,
16 pub checksum: CheckSums,
17 pub date: NaiveDateTime,
18}
19
20impl CloudImage {
21 #[must_use]
24 pub fn new(url: Url, checksum: CheckSums, name: String, date: NaiveDateTime) -> Self {
25 CloudImage {
26 url,
27 name,
28 checksum,
29 date,
30 }
31 }
32
33 #[must_use]
37 pub fn verify(&self, destination: &Path, normalize: &Option<String>) -> bool {
38 let Some(filename) = get_filename_destination(self, destination, normalize) else {
39 return false;
40 };
41 match self.checksum.verify_file(&filename) {
42 Ok(no_error) => {
43 if let Some(success) = no_error {
44 if success {
45 info!("{} Successfully verified {filename}", "🗸".green());
46 return true;
47 }
48 warn!("{} Verifying failed for {filename}", "𐄂".red());
49 false
50 } else {
51 warn!("{} {filename} not verified.", "𐄂".yellow());
54 true
55 }
56 }
57 Err(e) => {
58 error!("Error verifying {filename}: {e}");
59 false
60 }
61 }
62 }
63
64 pub fn is_in_db(&self, db: &DbImageHistory) -> bool {
65 db.is_image_in_db(Some(self)).unwrap_or_default()
68 }
69}
70
71impl fmt::Display for CloudImage {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match &self.checksum {
74 CheckSums::None => writeln!(f, "\t-> {}", self.url.url),
75 CheckSums::Sha256(checksum) | CheckSums::Sha512(checksum) => {
76 writeln!(f, "\t-> {} with checksum {}", self.url.url, checksum)
77 }
78 }
79 }
80}