Skip to content

Commit

Permalink
Add methods to read zip archive from buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
snpefk committed Sep 24, 2024
1 parent 1a3f891 commit 5feb1c0
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/opensi-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub enum Resource {
}

use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use serde::de::Error;

Check failure on line 131 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

unused import: `serde::de::Error`

Check failure on line 131 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `serde::de::Error`

Check failure on line 131 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `serde::de::Error`

Check failure on line 131 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `serde::de::Error`
use zip::ZipArchive;

const CONTROLS_ASCII_SET: &AsciiSet = &CONTROLS.add(b' ');

impl Atom {
Expand Down Expand Up @@ -159,14 +162,26 @@ impl Atom {
}

impl Package {
pub fn open(path: impl AsRef<Path>) -> Result<Package, std::io::Error> {

// Expecting byte array of zip file
pub fn from_zip_buffer(bytes: impl AsRef<[u8]>) -> Result<Package, io::Error> {

Check failure on line 167 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

failed to resolve: use of undeclared crate or module `io`

Check failure on line 167 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

failed to resolve: use of undeclared crate or module `io`

Check failure on line 167 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

failed to resolve: use of undeclared crate or module `io`

Check failure on line 167 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared crate or module `io`
let cursor = io::Cursor::new(bytes);

Check failure on line 168 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

failed to resolve: use of undeclared crate or module `io`

Check failure on line 168 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

failed to resolve: use of undeclared crate or module `io`

Check failure on line 168 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

failed to resolve: use of undeclared crate or module `io`

Check failure on line 168 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared crate or module `io`
Self::get_package_from_zip(cursor)
}

pub fn open_zip_file(path: impl AsRef<Path>) -> Result<Package, io::Error> {

Check failure on line 172 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

failed to resolve: use of undeclared crate or module `io`

Check failure on line 172 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

failed to resolve: use of undeclared crate or module `io`

Check failure on line 172 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

failed to resolve: use of undeclared crate or module `io`

Check failure on line 172 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared crate or module `io`
let package_file = File::open(path)?;
let mut zip = zip::ZipArchive::new(package_file)?;
let mut xml = zip.by_name("content.xml")?;
Self::get_package_from_zip(package_file)
}

fn get_package_from_zip<T: Read + io::Seek>(source: T) -> Result<Package, io::Error> {

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared crate or module `io`

Check failure on line 177 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared crate or module `io`
let mut zip_archive = ZipArchive::new(source)?;

let mut xml = zip_archive.by_name("content.xml")?;
let mut contents = String::new();
xml.read_to_string(&mut contents)?;

from_str(&contents).map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))
from_str(&contents).map_err(|e| io::Error::new(ErrorKind::InvalidData, e))

Check failure on line 184 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

failed to resolve: use of undeclared crate or module `io`

Check failure on line 184 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

failed to resolve: use of undeclared crate or module `io`

Check failure on line 184 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

failed to resolve: use of undeclared crate or module `io`

Check failure on line 184 in crates/opensi-core/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared crate or module `io`
}

// TODO: figure out how to do extraction on wasm
Expand Down

0 comments on commit 5feb1c0

Please sign in to comment.