Skip to content

Commit

Permalink
fixed file io
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Leon committed Feb 21, 2024
1 parent 4fdafbf commit d242ca1
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 240 deletions.
2 changes: 1 addition & 1 deletion bucket-api
1 change: 1 addition & 0 deletions src/controller/account/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub async fn login(
Ok(finish_resp.jwt_token as JwtToken)
}


pub async fn register(
query_client: &mut QueryClient,
email: &str,
Expand Down
3 changes: 2 additions & 1 deletion src/controller/bucket/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::error::Error;
use std::fmt::Debug;
use std::rc::Rc;
use std::str::FromStr;

use bucket_common_types::exclusive_share_link::ExclusiveShareLink;
use bucket_common_types::share_link::ShareLink;
Expand Down Expand Up @@ -275,7 +276,7 @@ pub async fn bucket_download<DH: BucketFileDownloadHandler, T>(
path: file.file_path.clone(),
date: None,
size_in_bytes: file.file_size_in_bytes,
file_format: file.file_type,
file_format: mime::Mime::from_str(file.file_format.as_str())?,
};
let mut download_handler = create_download_handler.handle(
virtual_file,
Expand Down
76 changes: 44 additions & 32 deletions src/controller/bucket/download_handler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::controller::bucket::errors::BucketDownloadHandlerErrors;
use crate::encryption_v1::module::{
DecryptionModule, EncryptionModule, ZeroKnowledgeDecryptionModuleV1,
};
use crate::controller::bucket::io::file::{BucketFile, BucketFileTrait};
use crate::encryption_v1::module::DecryptionModule;
use crate::encryption_v1::module::EncryptionModule;
use crate::encryption_v1::module::ZeroKnowledgeDecryptionModuleV1;
use async_trait::async_trait;
use bucket_common_types::BucketEncryption;
use gloo::file::Blob;
use futures::future::Either;
use mime::Mime;
use crate::controller::bucket::io::file::{BucketFile, BucketFileTrait};

use std::str::FromStr;

#[derive(Debug, thiserror::Error)]
pub enum BucketUploadHandlerErrors {}
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct WebBucketFileWriter {
Error = super::io::web_file::WebBucketFileError,
FileHandle = gloo::file::File,
>*/
BucketFile
BucketFile,
>,
pub offset: u64,
pub decryption_module: Option<ZeroKnowledgeDecryptionModuleV1>,
Expand All @@ -63,21 +63,27 @@ impl BucketFileDownloadHandler for WebBucketFileWriter {
_encryption: Option<BucketEncryption>,
_download_size_in_bytes: u64,
) -> Result<(), Self::Error> {
let blob = Blob::from(self.write_target_file.clone());
//let blob = Blob::from(self.write_target_file.clone());
//let bytes = read_as_bytes(&blob).await?;
let mime: Mime = from_filename
.split('.')
.last()
.unwrap_or("application/octet-stream")
.parse()?;
let file = BucketFile::new(
from_filename.as_str(),
&Mime::from_str("application/octet-stream").unwrap(),
)
.unwrap();

self.write_target_file = gloo::file::File::new_with_options(
&from_filename,
blob,
Some(mime.to_string().as_str()),
None,
);
/*let mime: Mime = from_filename
.split('.')
.last()
.unwrap_or("application/octet-stream")
.parse()?;
self.write_target_file = gloo::file::File::new_with_options(
&from_filename,
blob,
Some(mime.to_string().as_str()),
None,
);
*/
//write(self.write_target_file, );
Ok(())
}
Expand All @@ -87,20 +93,28 @@ impl BucketFileDownloadHandler for WebBucketFileWriter {
//let end = chunk.len() as u64;
//read_as_array_buffer(&self.write_target_file.slice(start, end), |res|{ res.unwrap(); });
//self.write_target_file.
let decrypted_buffer = match &mut self.decryption_module {
Some(x) => {
let mut decrypted_buffer: Vec<u8> = chunk.clone();
decrypted_buffer = x.update(chunk)?;
decrypted_buffer
let decrypted_buffer: futures::future::Either<Vec<u8>, &Vec<u8>> =
match &mut self.decryption_module {
Some(x) => {
let mut decrypted_buffer: Vec<u8> = chunk.clone();
decrypted_buffer = x.update(chunk)?;
Either::Left(decrypted_buffer)
}
None => Either::Right(chunk),
};
match decrypted_buffer {
Either::Left(decrypted_buffer) => {
self.write_target_file
.write_chunk(&decrypted_buffer, self.offset);
self.offset += decrypted_buffer.len() as u64;
}
None => {
chunk
Either::Right(decrypted_buffer) => {
self.write_target_file
.write_chunk(&decrypted_buffer, self.offset);
self.offset += decrypted_buffer.len() as u64;
}
};
}

self.write_target_file
.write_chunk(decrypted_buffer, self.offset);
self.offset += decrypted_buffer.len();
/*
* Create custom object for URL to download the file.
* The file will be stored as a "xxxx.temp" file. After the download the file is renamed to the correct filename.
Expand All @@ -119,8 +133,6 @@ impl BucketFileDownloadHandler for WebBucketFileWriter {
}
// Called when the last chunk has been downloaded.
fn on_download_finish(self) -> Result<(), Self::Error> {
//let write_target_file = self.write_target_file;
//read_as_bytes(write_target_file);
//TODO: Check if file match checksums.
match self.decryption_module {
None => {}
Expand Down
2 changes: 2 additions & 0 deletions src/controller/bucket/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum DownloadError {
GetBucketDetailsRequestFailed(#[source] tonic::Status),
#[error("GetBucketDetailsFromUrlRequestFailed")]
GetBucketDetailsFromUrlRequestFailed(#[source] tonic::Status),
#[error(transparent)]
FromStrError(#[from] FromStrError),
}
#[derive(Debug, thiserror::Error)]
pub enum UploadToUrlError {
Expand Down
161 changes: 17 additions & 144 deletions src/controller/bucket/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::{
io::{Read, Write},
vec,
};
use infer::Type;

use mime::Mime;
#[cfg(not(target_family = "wasm"))]
use crate::controller::bucket::io::native_file::VirtualNativeBucketFile;
Expand All @@ -17,9 +15,11 @@ pub struct VirtualFileDetails {
pub size_in_bytes: u64,
pub file_format: mime::Mime,
}

/// Traits to collectively implement the read/write to the local filesystem depending on target.
/// While mapping the local file to web file.
/// Current supported targets are native using tokio, or web through WASM.
#[tonic::async_trait(?Send)]
pub trait BucketFileTrait {
type Error;
type FileHandle;
Expand All @@ -30,17 +30,17 @@ pub trait BucketFileTrait {
//fn new(create_file_handle:fn() -> Self::FileHandle) -> Self;

// DON'T IMPLEMENT THE CREATION OF NEW FILE, ONLY TAKE EXISTING "FILE HANDLE" ONE KEEP IT SIMPLE.

fn new(filename: &str, mime: &Mime) -> Result<Self, Self::Error> where Self: Sized;
fn from(file_handle: Self::FileHandle, filename: String) -> Self where Self: Sized;
fn get_file_handle(&self) -> Self::FileHandle;
fn get_file_handle(&self) -> &Self::FileHandle;
async fn read_chunk(&self, size: u64, offset: u64) -> Result<Vec<u8>, Self::Error>;
fn read_stream(&self) -> Result<Box<dyn Read>, Self::Error>;
fn get_extension(&self) -> Result<String, Self::Error>;
/// Get the mime-type from the extension.
fn get_mime_type(&self) -> Result<Mime, Self::Error>;
/// Uses the first couple of bytes in the file ot determine the mime-type
async fn infer_mime_type(&self) -> Result<infer::Type, Self::Error>;
fn write_chunk(&self, chunk: vec::Vec<u8>, offset: u64) -> Result<(), Self::Error>;
async fn infer_mime_type(& self) -> Result<infer::Type, Self::Error>;
fn write_chunk(&self, chunk: &vec::Vec<u8>, offset: u64) -> Result<(), Self::Error>;
fn write_stream(&self, stream: &dyn Write) -> Result<(), Self::Error>;
fn get_size(&self) -> u64;
}
Expand All @@ -50,146 +50,19 @@ pub trait BucketFileTrait {
pub type BuketFile = VirtualWebBucketFile;
#[cfg(not(target_family = "wasm"))]
pub type BucketFile = VirtualNativeBucketFile;
/*
pub enum BucketFile {
WebFile(VirtualWebBucketFile),
#[cfg(feature = "native")]
NativeFile(VirtualNativeBucketFile),
}
impl BucketFileTrait for BucketFile {
type Error = WebBucketFileError;
type FileHandle = WebFileHandle;
fn from(file_handle: Self::FileHandle, filename: String) -> Self where Self: Sized {
Self::WebFile(VirtualWebBucketFile::from(file_handle, filename))
}
fn get_file_handle(&self) -> Self::FileHandle {
match self {
BucketFile::WebFile(web) => {
web.get_file_handle()
}
BucketFile::NativeFile(_) => { panic!() }
}
}
async fn read_chunk(&self, size: u64, offset: u64) -> Result<Vec<u8>, Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.read_chunk(size,offset)
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn read_stream(&self) -> Result<Box<dyn Read>, Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.read_stream()
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn get_extension(&self) -> Result<String, Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.get_extension()
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn get_mime_type(&self) -> Result<Mime, Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.get_mime_type()
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn infer_mime_type(&self) -> Result<Type, Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.infer_mime_type()
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn write_chunk(&self, chunk: Vec<u8>, offset: u64) -> Result<(), Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.write_chunk(chunk, offset)
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn write_stream(&self, stream: &dyn Write) -> Result<(), Self::Error> {
match self {
BucketFile::WebFile(web) => {
web.write_stream(stream)
}
BucketFile::NativeFile(_) => { panic!() }
}
}
fn get_size(&self) -> u64 {
match self {
BucketFile::WebFile(web) => {
web.get_size()
}
BucketFile::NativeFile(_) => { panic!() }
}
}
}
#[cfg(feature = "native")]
impl BucketFileTrait for VirtualNativeBucketFile {
type Error = ();
type FileHandle = ();
fn from(file_handle: Self::FileHandle, filename: String) -> Self where Self: Sized {
todo!()
}
fn get_file_handle(&self) -> Self::FileHandle {
todo!()
}
async fn read_chunk(&self, size: u64, offset: u64) -> Result<Vec<u8>, Self::Error> {
todo!()
}
fn read_stream(&self) -> Result<Box<dyn Read>, Self::Error> {
todo!()
}
fn get_extension(&self) -> Result<String, Self::Error> {
todo!()
}

fn get_mime_type(&self) -> Result<Mime, Self::Error> {
todo!()
}

fn infer_mime_type(&self) -> Result<Type, Self::Error> {
todo!()
}
#[cfg(test)]
mod tests {
#[test]
fn test_delete() {}

fn write_chunk(&self, chunk: Vec<u8>, offset: u64) -> Result<(), Self::Error> {
todo!()
}
#[test]
fn test_file_creation() {}

fn write_stream(&self, stream: &dyn Write) -> Result<(), Self::Error> {
todo!()
}
#[test]
fn test_write() {}

fn get_size(&self) -> u64 {
todo!()
}
}*/
#[test]
fn test_read() {}
}
Loading

0 comments on commit d242ca1

Please sign in to comment.