Compare commits

..

No commits in common. "f7431ca6a4a5f8e208c061f1c4915f9ea46b3f07" and "3cb95a9f0491b67eac55620627ae1b051d5c8133" have entirely different histories.

View file

@ -86,65 +86,63 @@ pub fn decrypt(
ContentType::Image => DecryptedData::Image(blob, container.len()),
ContentType::Audio => DecryptedData::Audio(blob),
ContentType::Video => DecryptedData::Video(blob),
ContentType::ZipArchive => handle_zip_archive(blob, container),
ContentType::Gzip => handle_gzip(blob, container),
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::GzipArchive => {
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 {
"<Invalid utf-8 path>".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::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 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<Blob>, container: Vec<u8>) -> 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.flatten() {
let file_path = if let Ok(file_path) = file.path() {
file_path.display().to_string()
} else {
"<Invalid utf-8 path>".to_string()
};
entries.push(ArchiveMeta {
name: file_path,
file_size: file.size(),
});
}
}
if entries.is_empty() {
DecryptedData::Blob(blob)
} else {
DecryptedData::Archive(blob, entries)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum ContentType {
Text,
@ -152,7 +150,7 @@ enum ContentType {
Audio,
Video,
ZipArchive,
Gzip,
GzipArchive,
Unknown,
}
@ -190,7 +188,7 @@ impl<T: AsRef<[u8]>> ContentTypeExt for T {
} else if mime_type == "application/zip" {
ContentType::ZipArchive
} else if mime_type == "application/gzip" {
ContentType::Gzip
ContentType::GzipArchive
} else {
ContentType::Unknown
}
@ -223,7 +221,7 @@ mod content_type {
test_content_type!(mp4_is_video, "movie.mp4", 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!(gzip_is_gzip, "image.png.gz", ContentType::Gzip);
test_content_type!(gzip_is_gzip, "image.png.gz", ContentType::GzipArchive);
test_content_type!(binary_is_unknown, "omegaupload", ContentType::Unknown);
test_content_type!(pgp_is_text, "text.pgp", ContentType::Text);
}