Browse Source

Use cache instead of downloading file each time

This is particularly useful if internet is down.
tags/v0.1.0
haixuanTao 3 years ago
parent
commit
399f1dcfad
3 changed files with 8 additions and 0 deletions
  1. +1
    -0
      Cargo.lock
  2. +1
    -0
      libraries/extensions/download/Cargo.toml
  3. +6
    -0
      libraries/extensions/download/src/lib.rs

+ 1
- 0
Cargo.lock View File

@@ -937,6 +937,7 @@ dependencies = [
"reqwest",
"tempfile",
"tokio",
"tracing",
]

[[package]]


+ 1
- 0
libraries/extensions/download/Cargo.toml View File

@@ -11,3 +11,4 @@ eyre = "0.6.8"
tempfile = "3.3.0"
reqwest = "0.11.12"
tokio = { version = "1.17.0" }
tracing = "0.1.36"

+ 6
- 0
libraries/extensions/download/src/lib.rs View File

@@ -3,11 +3,17 @@ use eyre::Context;
use std::os::unix::prelude::PermissionsExt;
use std::path::Path;
use tokio::io::AsyncWriteExt;
use tracing::log::warn;

pub async fn download_file<T>(url: T, target_path: &Path) -> Result<(), eyre::ErrReport>
where
T: reqwest::IntoUrl + std::fmt::Display + Copy,
{
if target_path.exists() {
warn!("Using cache: {:?}", target_path.to_str());
return Ok(());
}

if let Some(parent) = target_path.parent() {
tokio::fs::create_dir_all(parent)
.await


Loading…
Cancel
Save