Skip to content

Commit

Permalink
feat: added support for compress files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kremilly committed Jun 5, 2024
1 parent 307b2e1 commit 16cecce
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 6 deletions.
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ credentials.json

# Ignore .pd files
*.pd
*.sha256
*.sha256

# Ignore compress files
*.tar.gz
*.tar.xz
*.tar.bz2
*.tar
*.zip
*.rar
*.7z
*.gz
*.xz
*.bz2
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ open = "5.1.3"
lopdf = "0.32.0"
sha2 = "0.10.8"
zip = "2.1.0"
walkdir = "2.5.0"
4 changes: 3 additions & 1 deletion paimon.pbd
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ readme = "https://gist.githubusercontent.com/kremilly/5fd360d994bb0fe108b648d0e4
checksum = "https://gist.githubusercontent.com/kremilly/499d6d51d096c1813cea0eade8eb0bc4/raw/d7c5965aeaf005cf0b612e3468ab47c30480083b/paimon.sha256"
checksum.unmatch = "keep"

compress = "file.zip"

downloads {
https://arxiv.org/pdf/2405.01513 !ignore
https://www.scielo.br/j/rdbci/a/fwDKj9FMX7YTRDgkJGG4gnR?format=pdf&lang=pt !ignore
https://en.wikipedia.org/wiki/Rust_(programming_language) !ignore
https://en.wikipedia.org/wiki/Google !ignore
https://www.nasa.gov/wp-content/uploads/static/history/alsj/a17/A17_FlightPlan.pdf !ignore
https://sci-hub.se/10.1080/0025570x.2002.11953151
https://sci-hub.se/10.1080/0025570x.2002.11953151 !ignore
https://olacesar.com/e-books/protegido.pdf
https://github.com/huyubing/books-pdf/blob/master/slime.pdf !ignore
https://raw.githubusercontent.com/facebook/react/main/README.md !ignore
Expand Down
6 changes: 4 additions & 2 deletions src/cmd/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ impl Checksum {
Ok(())
}

pub async fn compare_lines(contents: &str, path: &str, checksum_file: &str, flags: &Flags) -> Result<(), Box<dyn Error>> {
pub async fn compare_lines(contents: &str, checksum_file: &str, flags: &Flags) -> Result<(), Box<dyn Error>> {
if !flags.no_checksum && !flags.no_checksum_validate {
let path = VarsBlock::get_path(contents);

if let Some(url) = VarsBlock::get_checksum(contents).await {
let local_hash_file = format!(
"{}{}", path, FileMisc::replace_extension(checksum_file, "sha256")
Expand All @@ -76,7 +78,7 @@ impl Checksum {
for (_, (local, remote)) in local_lines.iter().zip(remote_lines.iter()).enumerate() {
if !local.contains(remote) {
ChecksumAlerts::is_different(local);
Self::checksum_unmatch_delete_file(contents, path, local);
Self::checksum_unmatch_delete_file(contents, &path, local);
} else {
ChecksumAlerts::is_equal(local);
}
Expand Down
76 changes: 76 additions & 0 deletions src/cmd/compress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use walkdir::WalkDir;

use zip::{
CompressionMethod,

write::{
FileOptions,
ExtendedFileOptions
},
};

use std::{
fs::File,
path::Path,

io::{
self,
Read,
Write
},
};

use crate::{
syntax::vars_block::VarsBlock,

ui::{
ui_base::UI,
compress_alerts::CompressAlerts,
}
};

pub struct PathCompress;

impl PathCompress {

pub fn compress(contents: &str) -> io::Result<()> {
if let Some(zip_file) = VarsBlock::get_compress(contents) {
UI::section_header("Compressing files");

let folder_path = VarsBlock::get_path(contents);

let output_path = Path::new(&zip_file);
let output_file = File::create(output_path)?;

let mut zip = zip::ZipWriter::new(output_file);
let options: FileOptions<ExtendedFileOptions> = FileOptions::default()
.compression_method(CompressionMethod::Deflated)
.unix_permissions(0o755);

for entry in WalkDir::new(&folder_path) {
let entry = entry?;
let path = entry.path();
let name = path.strip_prefix(Path::new(&folder_path)).unwrap();

if path.is_file() {
let file = name.to_str().unwrap();
CompressAlerts::added(&file, &zip_file);

zip.start_file(name.to_str().unwrap(), options.clone())?;

let mut f = File::open(path)?;
let mut buffer = Vec::new();

f.read_to_end(&mut buffer)?;
zip.write_all(&buffer)?;
}
}

zip.finish()?;
CompressAlerts::completed(&zip_file);
}

Ok(())
}

}
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod compress;
pub mod checksum;
pub mod download;
pub mod read_list;
2 changes: 2 additions & 0 deletions src/regexp/regex_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ impl BlocksRegExp {

pub const GET_README_VAR: &'static str = r#"(?i)readme\s*=\s*"([^"]+)""#;

pub const GET_COMPRESS_VAR: &'static str = r#"(?i)compress\s*=\s*"([^"]+)""#;

pub const GET_CHECKSUM_VAR: &'static str = r#"(?i)checksum\s*=\s*"([^"]+)""#;

pub const GET_CHECKSUM_UNMATCH_ACTION: &'static str = r#"(?i)checksum\.unmatch\s*=\s*"([^"]+)""#;
Expand Down
5 changes: 4 additions & 1 deletion src/syntax/downloads_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
cmd::{
checksum::Checksum,
download::Download,
compress::PathCompress,
},

syntax::{
Expand Down Expand Up @@ -79,11 +80,13 @@ impl DownloadsBlock {
ReadMeBlock::render_var_and_save_file(&contents, flags).await?;

Checksum::generate_hashes(&path, checksum_file, refs, flags).await?;
Checksum::compare_lines(&contents, &path, checksum_file, flags).await?;
Checksum::compare_lines(&contents, checksum_file, flags).await?;
} else {
eprintln!("'downloads' block not found in file.");
}

PathCompress::compress(&contents)?;

Ok(())
}

Expand Down
10 changes: 10 additions & 0 deletions src/syntax/vars_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ impl VarsBlock {
}
}

pub fn get_compress(contents: &str) -> Option<String> {
let readme_pattern = Regex::new(BlocksRegExp::GET_COMPRESS_VAR).unwrap();

if let Some(caps) = readme_pattern.captures(&contents) {
caps.get(1).map(|m| m.as_str().to_string())
} else {
None
}
}

pub async fn get_checksum(contents: &str) -> Option<String> {
let readme_pattern = Regex::new(BlocksRegExp::GET_CHECKSUM_VAR).unwrap();

Expand Down
37 changes: 37 additions & 0 deletions src/ui/compress_alerts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extern crate colored;

use colored::*;

use crate::{
ui::emojis::Emojis,
system::system::System,
};

pub struct CompressAlerts;

impl CompressAlerts {

pub fn added(file: &str, zip_file: &str) {
let current_datetime = System::date_time();

println!(
"[{}] -> Added {} in: {}. {}",
current_datetime.green(),
file.blue(),
zip_file.cyan(),
Emojis::ADD
);
}

pub fn completed(zip_file: &str) {
let current_datetime = System::date_time();

println!(
"[{}] -> All files in the folder have been compressed into {}. {}",
current_datetime.green(),
zip_file.blue(),
Emojis::COMPRESS
);
}

}
2 changes: 2 additions & 0 deletions src/ui/emojis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ impl Emojis {
pub const CHECKED: &'static str = "✅";
pub const ERROR: &'static str = "❌";
pub const FORBIDDEN: &'static str = "⛔";
pub const ADD: &'static str = "➕";
pub const COMPRESS: &'static str = "📦";

}
3 changes: 2 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod ui_base;
pub mod macros_alerts;
pub mod errors_alerts;
pub mod success_alerts;
pub mod checksum_alerts;
pub mod checksum_alerts;
pub mod compress_alerts;

0 comments on commit 16cecce

Please sign in to comment.