Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ impl actix_web::ResponseError for ApiError {
ApiError::TooManyRequests(..) => StatusCode::TOO_MANY_REQUESTS,
ApiError::NotFound(_) => StatusCode::NOT_FOUND,
ApiError::BadRequest(..) => StatusCode::BAD_REQUEST,
ApiError::ModZip(err) => err.status_code(),
ApiError::PlatformParseError(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/mod_zip.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Seek;
use std::io::{BufReader, Cursor, Read};

use actix_web::http::StatusCode;
use actix_web::web::Bytes;
use image::codecs::png::PngDecoder;
use image::codecs::png::PngEncoder;
Expand All @@ -11,6 +12,8 @@ use zip::ZipArchive;
use zip::read::ZipFile;
use zip::result::ZipError;

use crate::endpoints::ApiError::ModZip;

#[derive(thiserror::Error, Debug)]
pub enum ModZipError {
#[error("I/O error: {0}")]
Expand Down Expand Up @@ -39,6 +42,23 @@ pub enum ModZipError {
InvalidBinaries(String),
}

impl ModZipError {
pub fn status_code(&self) -> StatusCode {
match self {
ModZipError::IoError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ModZipError::ImageError(e) => match (e) {
ImageError::Limits(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
ModZipError::ZipError(e) => match (e) {
ZipError::InvalidArchive(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
_ => StatusCode::BAD_REQUEST,
}
}
}

pub fn extract_mod_logo<R: Read>(file: &mut ZipFile<R>) -> Result<Vec<u8>, ModZipError> {
const FIVE_MEGABYTES: u64 = 5 * 1000 * 1000;
if file.size() > FIVE_MEGABYTES {
Expand Down
Loading