From 67aa009746d5a59363cd1bff0d9a66ba33ba7066 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sat, 15 Jan 2022 21:38:33 -0800 Subject: [PATCH] Move decryption handlers to functions --- web/src/decrypt.rs | 106 +++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/web/src/decrypt.rs b/web/src/decrypt.rs index afd016a..bcbc493 100644 --- a/web/src/decrypt.rs +++ b/web/src/decrypt.rs @@ -86,63 +86,67 @@ pub fn decrypt( ContentType::Image => DecryptedData::Image(blob, container.len()), ContentType::Audio => DecryptedData::Audio(blob), ContentType::Video => DecryptedData::Video(blob), - ContentType::ZipArchive => { - let mut entries = vec![]; - let cursor = Cursor::new(container); - if let Ok(mut zip) = zip::ZipArchive::new(cursor) { - for i in 0..zip.len() { - match zip.by_index(i) { - Ok(file) => entries.push(ArchiveMeta { - name: file.name().to_string(), - file_size: file.size(), - }), - Err(err) => match err { - zip::result::ZipError::UnsupportedArchive(s) => { - log!("Unsupported: ", s.to_string()); - } - _ => { - log!(format!("Error: {}", err)); - } - }, - } - } - } - - entries.sort_by(|a, b| a.name.cmp(&b.name)); - DecryptedData::Archive(blob, entries) - } - ContentType::Gzip => { - let mut entries = vec![]; - let cursor = Cursor::new(container); - let gzip_dec = flate2::read::GzDecoder::new(cursor); - let mut archive = tar::Archive::new(gzip_dec); - if let Ok(files) = archive.entries() { - for file in files { - if let Ok(file) = file { - let file_path = if let Ok(file_path) = file.path() { - file_path.display().to_string() - } else { - "".to_string() - }; - entries.push(ArchiveMeta { - name: file_path, - file_size: file.size(), - }); - } - } - } - if entries.len() > 0 { - DecryptedData::Archive(blob, entries) - } else { - DecryptedData::Blob(blob) - } - } + ContentType::ZipArchive => handle_zip_archive(blob, container), + ContentType::Gzip => handle_gzip(blob, container), ContentType::Unknown => DecryptedData::Blob(blob), }; Ok((data, MimeType(mime_type.to_owned()))) } +fn handle_zip_archive(blob: Arc, container: Vec) -> DecryptedData { + let mut entries = vec![]; + let cursor = Cursor::new(container); + if let Ok(mut zip) = zip::ZipArchive::new(cursor) { + for i in 0..zip.len() { + match zip.by_index(i) { + Ok(file) => entries.push(ArchiveMeta { + name: file.name().to_string(), + file_size: file.size(), + }), + Err(err) => match err { + zip::result::ZipError::UnsupportedArchive(s) => { + log!("Unsupported: ", s.to_string()); + } + _ => { + log!(format!("Error: {}", err)); + } + }, + } + } + } + + entries.sort_by(|a, b| a.name.cmp(&b.name)); + DecryptedData::Archive(blob, entries) +} + +fn handle_gzip(blob: Arc, container: Vec) -> DecryptedData { + let mut entries = vec![]; + let cursor = Cursor::new(container); + let gzip_dec = flate2::read::GzDecoder::new(cursor); + let mut archive = tar::Archive::new(gzip_dec); + if let Ok(files) = archive.entries() { + for file in files { + if let Ok(file) = file { + let file_path = if let Ok(file_path) = file.path() { + file_path.display().to_string() + } else { + "".to_string() + }; + entries.push(ArchiveMeta { + name: file_path, + file_size: file.size(), + }); + } + } + } + if entries.len() > 0 { + DecryptedData::Archive(blob, entries) + } else { + DecryptedData::Blob(blob) + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] enum ContentType { Text,