feat: work on version backend system

This commit is contained in:
DecDuck
2025-07-01 22:51:22 +10:00
parent fe43f79062
commit c1aaf8adcd
11 changed files with 322 additions and 202 deletions
+107
View File
@@ -0,0 +1,107 @@
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::{
fs::File,
io::{self, Read},
path::PathBuf,
};
use zip::{read::ZipFile, ZipArchive};
use crate::version::{
types::{MinimumFileObject, Skippable, VersionBackend, VersionFile},
utils::_list_files,
};
pub struct PathVersionBackend {
pub base_dir: PathBuf,
}
impl VersionBackend for PathVersionBackend {
fn list_files(&mut self) -> Vec<VersionFile> {
let mut vec = Vec::new();
_list_files(&mut vec, &self.base_dir);
let mut results = Vec::new();
for pathbuf in vec.iter() {
let file = File::open(pathbuf.clone()).unwrap();
let relative = pathbuf.strip_prefix(self.base_dir.clone()).unwrap();
let metadata = file.try_clone().unwrap().metadata().unwrap();
let permission_object = metadata.permissions();
let permissions = {
let perm: u32;
#[cfg(target_family = "unix")]
{
perm = permission_object.mode();
}
#[cfg(not(target_family = "unix"))]
{
perm = 0
}
perm
};
results.push(VersionFile {
relative_filename: relative.to_string_lossy().to_string(),
permission: permissions,
});
}
results
}
fn reader(&mut self, file: &VersionFile) -> Option<Box<(dyn MinimumFileObject + 'static)>> {
let file = File::open(self.base_dir.join(file.relative_filename.clone())).ok()?;
return Some(Box::new(file));
}
}
pub struct ZipVersionBackend {
archive: ZipArchive<File>,
}
impl ZipVersionBackend {
pub fn new(archive: PathBuf) -> Self {
let handle = File::open(archive).unwrap();
Self {
archive: ZipArchive::new(handle).unwrap(),
}
}
}
struct ZipFileWrapper<'a> {
inner: ZipFile<'a, File>,
}
impl Read for ZipFileWrapper<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.inner.read(buf)
}
}
impl Skippable for ZipFileWrapper<'_> {
fn skip(&mut self, amount: u64) {
io::copy(&mut self.inner.by_ref().take(amount), &mut io::sink()).unwrap();
}
}
impl MinimumFileObject for ZipFileWrapper<'_> {}
impl VersionBackend for ZipVersionBackend {
fn list_files(&mut self) -> Vec<VersionFile> {
let mut results = Vec::new();
for i in 0..self.archive.len() {
let entry = self.archive.by_index(i).unwrap();
results.push(VersionFile {
relative_filename: entry.name().to_owned(),
permission: entry.unix_mode().or(Some(0)).unwrap(),
});
}
results
}
fn reader(&mut self, file: &VersionFile) -> Option<Box<(dyn MinimumFileObject)>> {
let file = self.archive.by_name(&file.relative_filename).ok()?;
let zip_file_wrapper = ZipFileWrapper { inner: file };
//Some(Box::new(zip_file_wrapper))
None
}
}
+3
View File
@@ -0,0 +1,3 @@
pub mod utils;
pub mod types;
pub mod backends;
+47
View File
@@ -0,0 +1,47 @@
use std::io::{Read, Seek, SeekFrom};
use tokio::io::{self, AsyncRead};
#[derive(Debug)]
pub struct VersionFile {
pub relative_filename: String,
pub permission: u32,
}
pub trait Skippable {
fn skip(&mut self, amount: u64);
}
impl<T> Skippable for T
where
T: Seek,
{
fn skip(&mut self, amount: u64) {
self.seek(SeekFrom::Start(amount)).unwrap();
}
}
pub trait MinimumFileObject: Read + Send + Skippable {}
impl<T: Read + Send + Seek> MinimumFileObject for T {}
// Intentionally not a generic, because of types in read_file
pub struct ReadToAsyncRead {
pub inner: Box<(dyn Read + Send)>,
}
impl AsyncRead for ReadToAsyncRead {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<io::Result<()>> {
let mut read_buf = [0u8; 8192];
let amount = self.inner.read(&mut read_buf).unwrap();
buf.put_slice(&read_buf[0..amount]);
std::task::Poll::Ready(Ok(()))
}
}
pub trait VersionBackend {
fn list_files(&mut self) -> Vec<VersionFile>;
fn reader(&mut self, file: &VersionFile) -> Option<Box<(dyn MinimumFileObject)>>;
}
+112
View File
@@ -0,0 +1,112 @@
use std::{
fs::{self, metadata, File},
io::Read,
path::{Path, PathBuf},
};
use napi::{bindgen_prelude::*, tokio_stream::StreamExt};
use tokio_util::codec::{BytesCodec, FramedRead};
use zip::ZipArchive;
use crate::version::{
backends::{PathVersionBackend, ZipVersionBackend},
types::{MinimumFileObject, ReadToAsyncRead, VersionBackend, VersionFile},
};
pub fn _list_files(vec: &mut Vec<PathBuf>, path: &Path) {
if metadata(path).unwrap().is_dir() {
let paths = fs::read_dir(path).unwrap();
for path_result in paths {
let full_path = path_result.unwrap().path();
if metadata(&full_path).unwrap().is_dir() {
_list_files(vec, &full_path);
} else {
vec.push(full_path);
}
}
}
}
pub fn create_backend_for_path(path: &Path) -> Option<Box<(dyn VersionBackend + Send)>> {
let is_directory = path.is_dir();
if is_directory {
return Some(Box::new(PathVersionBackend {
base_dir: path.to_path_buf(),
}));
};
/*
Insert checks for whatever backend you like
*/
if path.ends_with(".zip") {
return Some(Box::new(ZipVersionBackend::new(path.to_path_buf())));
}
None
}
#[napi]
pub fn has_backend_for_path(path: String) -> bool {
let path = Path::new(&path);
let has_backend = create_backend_for_path(path).is_some();
has_backend
}
#[napi]
pub fn list_files(path: String) -> Vec<String> {
let path = Path::new(&path);
let mut backend = create_backend_for_path(path).unwrap();
let files = backend.list_files();
files.into_iter().map(|e| e.relative_filename).collect()
}
#[napi]
pub fn read_file(
path: String,
sub_path: String,
env: &Env,
start: Option<u32>,
end: Option<u32>,
) -> Option<ReadableStream<'static, BufferSlice<'static>>> {
let path = Path::new(&path);
let mut backend = create_backend_for_path(path).unwrap();
let version_file = VersionFile {
relative_filename: sub_path,
permission: 0, // Shouldn't matter
};
// Use `?` operator for cleaner error propagation from `Option`
let mut reader = backend.reader(&version_file)?;
// Skip the 'start' amount of bytes without seek
if let Some(skip) = start {
reader.skip(skip.into());
// io::copy(&mut reader.by_ref().take(skip.into()), &mut io::sink()).unwrap();
}
let async_reader = if let Some(limit) = end {
let amount = limit - start.or(Some(0)).unwrap();
ReadToAsyncRead {
inner: Box::new(reader.take(amount.into())),
}
} else {
ReadToAsyncRead { inner: reader }
};
// Create a FramedRead stream with BytesCodec for chunking
let stream = FramedRead::new(async_reader, BytesCodec::new())
// Use StreamExt::map to transform each Result item
.map(|result_item| {
result_item
// Apply Result::map to transform Ok(BytesMut) to Ok(Vec<u8>)
.map(|bytes| bytes.to_vec())
// Apply Result::map_err to transform Err(std::io::Error) to Err(napi::Error)
.map_err(|e| napi::Error::from(e)) // napi::Error implements From<tokio::io::Error>
});
// Create the napi-rs ReadableStream from the tokio_stream::Stream
// The unwrap() here means if stream creation fails, it will panic.
// For a production system, consider returning Result<Option<...>> and handling this.
Some(ReadableStream::create_with_stream_bytes(env, stream).unwrap())
}