DTPM: upload files as stream and directory support #2
| @ -205,13 +205,13 @@ impl DtpmConfig { | ||||
| 
 | ||||
|     pub fn load_data(mut self) -> Result<Self> { | ||||
|         for file_entry in self.filesystems.iter_mut() { | ||||
|             if let FileContent::Path(path) = &file_entry.content { | ||||
|                 if Path::new(path).is_dir() { | ||||
|                     let compressed_data = compress_directory(path)?; | ||||
|             if let FileContent::Path(content_path) = &file_entry.content { | ||||
|                 if Path::new(content_path).is_dir() { | ||||
|                     let compressed_data = compress_directory(content_path)?; | ||||
|                     file_entry.content = FileContent::Archive(compressed_data); | ||||
|                 } else { | ||||
|                     let content = std::fs::read(path) | ||||
|                         .unwrap_or_else(|_| panic!("Unable to read file {path}")); | ||||
|                     let content = std::fs::read(content_path) | ||||
|                         .unwrap_or_else(|_| panic!("Unable to read file {content_path}")); | ||||
|                     let encoded = BASE64.encode(content); | ||||
|                     file_entry.content = FileContent::Data(encoded); | ||||
|                 } | ||||
| @ -223,9 +223,10 @@ impl DtpmConfig { | ||||
| } | ||||
| 
 | ||||
| pub fn compress_directory(input_dir: &str) -> Result<Vec<u8>> { | ||||
|     let path = format!("{}/", input_dir.split('/').next_back().unwrap_or("files")); | ||||
|     let mut tar_builder = Builder::new(Encoder::new(Vec::new(), 3)?); | ||||
| 
 | ||||
|     tar_builder.append_dir_all(".", input_dir)?; | ||||
|     tar_builder.append_dir_all(path, input_dir)?; | ||||
| 
 | ||||
|     tar_builder.finish()?; | ||||
| 
 | ||||
| @ -235,9 +236,30 @@ pub fn compress_directory(input_dir: &str) -> Result<Vec<u8>> { | ||||
|     Ok(compressed_data) | ||||
| } | ||||
| 
 | ||||
| pub fn decompress_directory(save_path: &str, archive_buff: Vec<u8>) -> Result<()> { | ||||
| pub fn decompress_directory(dst: &str, archive_buff: Vec<u8>) -> Result<()> { | ||||
|     /* | ||||
|     for entry in Archive::new(Decoder::new(archive_buff.as_slice())?).entries()? { | ||||
|         let entry = entry?; | ||||
|         let path = entry.path(); | ||||
|         println!( | ||||
|             "Entry: {:?}; type {:?}; size: {}", | ||||
|             path?, | ||||
|             entry.header().entry_type(), | ||||
|             entry.size() | ||||
|         ); | ||||
|     } | ||||
|     */ | ||||
|     let mut archive = Archive::new(Decoder::new(archive_buff.as_slice())?); | ||||
| 
 | ||||
|     archive.unpack(save_path)?; | ||||
|     for entry in archive.entries()? { | ||||
|         let mut file = entry?; | ||||
|         let file_path = format!("{dst}/{}", &file.path()?.to_string_lossy()); | ||||
|         if file.header().entry_type() == tar::EntryType::Directory { | ||||
|             std::fs::create_dir_all(file_path)?; | ||||
|         } else { | ||||
|             file.unpack(file_path)?; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| @ -41,7 +41,6 @@ fn test_decompression_02() { | ||||
| 
 | ||||
|     decompress_directory(&TEMP_DIR, archive_buff).unwrap(); | ||||
| 
 | ||||
|     let path = format!("{}/{}", *TEMP_DIR, "fixtures/dtpm_config.yaml"); | ||||
|     dbg!(&path); | ||||
|     let path = format!("{}/{}", *TEMP_DIR, "tests/fixtures/dtpm_config.yaml"); | ||||
|     assert!(std::path::Path::new(&path).exists()); | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user