Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unzip module #53

Merged
merged 6 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ lazy_static = { version = "1.4", optional = true }
once_cell = { version = "1.4", optional = true }
mysql = { version = "20.0", optional = true }
dashmap = { version = "3.11", optional = true }
zip = { version = "0.5.8", optional = true }


[features]
Expand All @@ -49,3 +50,4 @@ git = ["git2", "chrono"]
http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
jobs = ["flume"]
unzip = ["zip", "jobs"]
2 changes: 2 additions & 0 deletions dmsrc/unzip.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define rustg_unzip_download_async(url, unzip_directory) call(RUST_G, "unzip_download_async")(url, unzip_directory)
#define rustg_unzip_check(job_id) call(RUST_G, "unzip_check")("[job_id]")
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use thiserror::Error;
#[cfg(feature = "png")]
use png::{DecodingError, EncodingError};

#[cfg(feature = "unzip")]
use zip::result::ZipError;

pub type Result<T> = result::Result<T, Error>;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -42,6 +45,9 @@ pub enum Error {
#[cfg(feature = "http")]
#[error(transparent)]
SerializationError(#[from] serde_json::Error),
#[cfg(feature = "unzip")]
#[error(transparent)]
UnzipError(#[from] ZipError)
}

impl From<Utf8Error> for Error {
Expand Down
2 changes: 1 addition & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn setup_http_client() -> reqwest::blocking::Client {
Client::builder().default_headers(headers).build().unwrap()
}

static HTTP_CLIENT: Lazy<reqwest::blocking::Client> = Lazy::new(setup_http_client);
pub static HTTP_CLIENT: Lazy<reqwest::blocking::Client> = Lazy::new(setup_http_client);

// ----------------------------------------------------------------------------
// Request construction and execution
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod noise_gen;
pub mod sql;
#[cfg(feature = "url")]
pub mod url;
#[cfg(feature = "unzip")]
pub mod unzip;

#[cfg(not(target_pointer_width = "32"))]
compile_error!("rust-g must be compiled for a 32-bit target");
68 changes: 68 additions & 0 deletions src/unzip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::{error::Result, jobs, http::HTTP_CLIENT};
use reqwest::blocking::RequestBuilder;
use std::fs;
use std::io::Write;
use std::path::Path;
use zip::ZipArchive;

struct UnzipPrep {
req: RequestBuilder,
unzip_directory: String,
}

fn construct_unzip(
url: &str,
unzip_directory: &str
) -> UnzipPrep {

let req = HTTP_CLIENT.get(url);
let dir_copy = unzip_directory.to_string();

UnzipPrep {
req,
unzip_directory: dir_copy,
}
}

byond_fn! { unzip_download_async(url, unzip_directory) {
let unzip = construct_unzip(&url, &unzip_directory);
Some(jobs::start(move ||
do_unzip_download(unzip).unwrap_or_else(|e| e.to_string())
))
} }

fn do_unzip_download(prep: UnzipPrep) -> Result<String> {
let unzip_path = Path::new(&prep.unzip_directory);
let response = prep.req.send()?;

let content = response.bytes()?;

let reader = std::io::Cursor::new(content);
let mut archive = ZipArchive::new(reader)?;

for i in 0..archive.len()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm kinda dumbfounded that there's no Iterator for this. oh well

Copy link
Contributor

@vuonojenmustaturska vuonojenmustaturska Oct 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Felt weird to me so I looked it up, https://users.rust-lang.org/t/is-it-possible-to-create-an-iterator-for-ziparchive-in-zip-crate/5744/3

tldr you can’t make an Iterator that takes a ZipArchive and yields ZipFile items. This is because a ZipFile mutably borrows the ZipArchive's reader, so a single archive can only have one ZipFile at a time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spooky! 👻

{
let mut entry = archive.by_index(i)?;

let file_path = unzip_path.join(entry.name());

if let Some(parent) = file_path.parent() {
fs::create_dir_all(parent)?
}

let file = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&file_path)?;

let mut writer = std::io::BufWriter::new(file);
std::io::copy(&mut entry, &mut writer)?;
writer.flush()?;
}

Ok("true".to_string())
}

byond_fn! { unzip_check(id) {
Some(jobs::check(id))
} }