Compare commits

...

3 commits

Author SHA1 Message Date
f7431ca6a4
Clippy 2022-01-15 21:40:36 -08:00
67aa009746
Move decryption handlers to functions 2022-01-15 21:38:33 -08:00
8a09da764e
Rename GzipArchive to Gzip 2022-01-15 21:34:52 -08:00

View file

@ -86,7 +86,15 @@ pub fn decrypt(
ContentType::Image => DecryptedData::Image(blob, container.len()), ContentType::Image => DecryptedData::Image(blob, container.len()),
ContentType::Audio => DecryptedData::Audio(blob), ContentType::Audio => DecryptedData::Audio(blob),
ContentType::Video => DecryptedData::Video(blob), ContentType::Video => DecryptedData::Video(blob),
ContentType::ZipArchive => { 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<Blob>, container: Vec<u8>) -> DecryptedData {
let mut entries = vec![]; let mut entries = vec![];
let cursor = Cursor::new(container); let cursor = Cursor::new(container);
if let Ok(mut zip) = zip::ZipArchive::new(cursor) { if let Ok(mut zip) = zip::ZipArchive::new(cursor) {
@ -111,14 +119,14 @@ pub fn decrypt(
entries.sort_by(|a, b| a.name.cmp(&b.name)); entries.sort_by(|a, b| a.name.cmp(&b.name));
DecryptedData::Archive(blob, entries) DecryptedData::Archive(blob, entries)
} }
ContentType::GzipArchive => {
fn handle_gzip(blob: Arc<Blob>, container: Vec<u8>) -> DecryptedData {
let mut entries = vec![]; let mut entries = vec![];
let cursor = Cursor::new(container); let cursor = Cursor::new(container);
let gzip_dec = flate2::read::GzDecoder::new(cursor); let gzip_dec = flate2::read::GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gzip_dec); let mut archive = tar::Archive::new(gzip_dec);
if let Ok(files) = archive.entries() { if let Ok(files) = archive.entries() {
for file in files { for file in files.flatten() {
if let Ok(file) = file {
let file_path = if let Ok(file_path) = file.path() { let file_path = if let Ok(file_path) = file.path() {
file_path.display().to_string() file_path.display().to_string()
} else { } else {
@ -130,17 +138,11 @@ pub fn decrypt(
}); });
} }
} }
} if entries.is_empty() {
if entries.len() > 0 {
DecryptedData::Archive(blob, entries)
} else {
DecryptedData::Blob(blob) DecryptedData::Blob(blob)
} else {
DecryptedData::Archive(blob, entries)
} }
},
ContentType::Unknown => DecryptedData::Blob(blob),
};
Ok((data, MimeType(mime_type.to_owned())))
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
@ -150,7 +152,7 @@ enum ContentType {
Audio, Audio,
Video, Video,
ZipArchive, ZipArchive,
GzipArchive, Gzip,
Unknown, Unknown,
} }
@ -188,7 +190,7 @@ impl<T: AsRef<[u8]>> ContentTypeExt for T {
} else if mime_type == "application/zip" { } else if mime_type == "application/zip" {
ContentType::ZipArchive ContentType::ZipArchive
} else if mime_type == "application/gzip" { } else if mime_type == "application/gzip" {
ContentType::GzipArchive ContentType::Gzip
} else { } else {
ContentType::Unknown ContentType::Unknown
} }
@ -221,7 +223,7 @@ mod content_type {
test_content_type!(mp4_is_video, "movie.mp4", ContentType::Video); test_content_type!(mp4_is_video, "movie.mp4", ContentType::Video);
test_content_type!(mkv_is_video, "movie.mkv", ContentType::Video); test_content_type!(mkv_is_video, "movie.mkv", ContentType::Video);
test_content_type!(zip_is_zip, "archive.zip", ContentType::ZipArchive); test_content_type!(zip_is_zip, "archive.zip", ContentType::ZipArchive);
test_content_type!(gzip_is_gzip, "image.png.gz", ContentType::GzipArchive); test_content_type!(gzip_is_gzip, "image.png.gz", ContentType::Gzip);
test_content_type!(binary_is_unknown, "omegaupload", ContentType::Unknown); test_content_type!(binary_is_unknown, "omegaupload", ContentType::Unknown);
test_content_type!(pgp_is_text, "text.pgp", ContentType::Text); test_content_type!(pgp_is_text, "text.pgp", ContentType::Text);
} }