Skip to content

Commit

Permalink
Merge pull request #1 from fastn-stack/ignore-static
Browse files Browse the repository at this point in the history
Ignore static
  • Loading branch information
harshdoesdev authored Oct 10, 2023
2 parents dfabe71 + 61b6f1c commit 3e1dc91
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 5 deletions.
31 changes: 30 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vercel-cache-helper"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
description = "Hello world"

Expand All @@ -21,3 +21,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = "0.3"
tokio-util = { version = "0.7.9", features = ["codec"]}
walkdir = "2"
7 changes: 4 additions & 3 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use indicatif::{ProgressBar, ProgressStyle};
use std::io::{Seek, Read};
use std::io::{Read, Seek};

pub async fn upload(
remote_client: vercel_cache_helper::vercel::remote_client::RemoteClient,
Expand Down Expand Up @@ -39,7 +39,7 @@ pub async fn upload(

let mut output_dir_archive = tempfile::tempfile()?;

vercel_cache_helper::utils::create_tar_zst_archive(
vercel_cache_helper::utils::create_filtered_tar_zst_archive(
&output_dir.path().to_path_buf(),
&output_dir_archive,
)?;
Expand All @@ -64,7 +64,8 @@ pub async fn upload(
pb.set_message("Uploading artfiacts...");

let response = output_put_req
.buffer(&mut output_archive_buf, output_archive_size).await?;
.buffer(&mut output_archive_buf, output_archive_size)
.await?;

if !response.status().is_success() {
println!("Could not upload artifacts.");
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const VALID_EXTENSIONS: [&str; 5] = ["html", "css", "js", "json", "ftd"];
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::convert::From;
pub enum Error {
FileNotFound(String),
InvalidInput(String),
WalkDirError(String),
EnvVarNotFound(String),
ReqwestError(String),
Other(Box<dyn std::error::Error + Send + Sync>),
Expand All @@ -16,6 +17,7 @@ impl std::fmt::Display for Error {
match self {
Error::FileNotFound(message) => write!(f, "File not found: {}", message),
Error::InvalidInput(message) => write!(f, "Invalid input: {}", message),
Error::WalkDirError(message) => write!(f, "Walkdir error: {}", message),
Error::EnvVarNotFound(var_name) => {
write!(f, "Environment variable not found: {}", var_name)
}
Expand All @@ -35,6 +37,12 @@ impl From<std::io::Error> for Error {
}
}

impl From<walkdir::Error> for Error {
fn from(walkdir_error: walkdir::Error) -> Self {
Error::WalkDirError(walkdir_error.to_string())
}
}

impl From<std::env::VarError> for Error {
fn from(env_error: std::env::VarError) -> Self {
Error::EnvVarNotFound(env_error.to_string())
Expand All @@ -48,6 +56,7 @@ impl From<reqwest::Error> for Error {
}

pub mod commands;
pub mod constants;
pub mod utils;
pub mod vercel;

Expand Down
60 changes: 60 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,63 @@ pub fn extract_tar_zst(file: std::fs::File, dest_path: &std::path::PathBuf) -> s
println!("Unpacked archive in: {}", &dest_path.to_string_lossy());
Ok(())
}

pub fn create_filtered_tar_zst_archive(
src_folder: &std::path::PathBuf,
dest_file: &std::fs::File,
) -> vercel_cache_helper::Result<()> {
println!("Creating archive from: {:?}", src_folder);

if !src_folder.exists() {
println!("Source folder does not exist: {:?}", src_folder);
return Ok(());
}

if std::fs::read_dir(src_folder)?.next().is_none() {
println!("Source folder is empty: {:?}", src_folder);
return Ok(());
}

let zst_encoder =
match zstd::stream::write::Encoder::new(dest_file, zstd::DEFAULT_COMPRESSION_LEVEL) {
Ok(encoder) => encoder,
Err(err) => {
println!("Error creating Zstd encoder: {:?}", err);
return Err(err.into());
}
};

let mut tar_builder = tar::Builder::new(zst_encoder);

let walker = walkdir::WalkDir::new(src_folder).into_iter();
for entry in walker {
let entry = entry?;
let path = entry.path();

if path.starts_with(src_folder.join("-")) {
continue;
}

if let Some(extension) = path.extension() {
let ext_str = extension.to_string_lossy().to_lowercase();
if vercel_cache_helper::constants::VALID_EXTENSIONS.contains(&ext_str.as_ref()) {
// Calculate the relative path from src_folder to the current file
let relative_path = path.strip_prefix(src_folder).unwrap();

// Append the file to the archive with the relative path
tar_builder.append_path_with_name(path, relative_path)?;
}
}
}

match tar_builder.into_inner()?.finish() {
Ok(_) => {
println!("Archive created successfully.");
Ok(())
}
Err(err) => {
println!("Error closing the archive: {:?}", err);
Err(err.into())
}
}
}

0 comments on commit 3e1dc91

Please sign in to comment.