Use better error message for invalid video files

This commit is contained in:
Zoe Roux 2023-09-15 00:20:44 +02:00
parent fb2280798a
commit 01486dfbec
4 changed files with 16 additions and 15 deletions

View File

@ -20,7 +20,7 @@ impl error::ResponseError for ApiError {
HttpResponse::build(self.status_code()) HttpResponse::build(self.status_code())
.insert_header(ContentType::json()) .insert_header(ContentType::json())
.body(format!( .body(format!(
"{{ \"status\": \"{status}\", \"error\": \"{err}\" }}", "{{ \"status\": \"{status}\", \"errors\": [\"{err}\"] }}",
status = self.status_code(), status = self.status_code(),
err = self.to_string() err = self.to_string()
)) ))

View File

@ -127,7 +127,7 @@ async fn extract(path: String, sha: &String, subs: &Vec<Subtitle>) {
.expect("Error running ffmpeg extract"); .expect("Error running ffmpeg extract");
} }
pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> { pub async fn identify(path: String) -> Option<MediaInfo> {
let extension_table: HashMap<&str, &str> = let extension_table: HashMap<&str, &str> =
HashMap::from([("subrip", "srt"), ("ass", "ass"), ("vtt", "vtt")]); HashMap::from([("subrip", "srt"), ("ass", "ass"), ("vtt", "vtt")]);
@ -138,7 +138,9 @@ pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> {
.output() .output()
.await .await
.expect("Error running the mediainfo command"); .expect("Error running the mediainfo command");
assert!(mediainfo.status.success()); if !mediainfo.status.success() {
return None;
}
let output = json::parse(str::from_utf8(mediainfo.stdout.as_slice()).unwrap()).unwrap(); let output = json::parse(str::from_utf8(mediainfo.stdout.as_slice()).unwrap()).unwrap();
let general = output["media"]["track"] let general = output["media"]["track"]
@ -182,8 +184,8 @@ pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> {
.collect(); .collect();
if !PathBuf::from(format!("/metadata/{sha}")).exists() { if !PathBuf::from(format!("/metadata/{sha}")).exists() {
std::fs::create_dir_all(format!("/metadata/{sha}/att"))?; std::fs::create_dir_all(format!("/metadata/{sha}/att")).unwrap();
std::fs::create_dir_all(format!("/metadata/{sha}/sub"))?; std::fs::create_dir_all(format!("/metadata/{sha}/sub")).unwrap();
extract(path.clone(), &sha, &subs).await; extract(path.clone(), &sha, &subs).await;
} }
@ -191,8 +193,8 @@ pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> {
v.as_str().and_then(|x| x.parse::<F>().ok()) v.as_str().and_then(|x| x.parse::<F>().ok())
} }
Ok(MediaInfo { Some(MediaInfo {
length: parse::<f32>(&general["Duration"]).unwrap(), length: parse::<f32>(&general["Duration"])?,
container: general["Format"].as_str().unwrap().to_string(), container: general["Format"].as_str().unwrap().to_string(),
video: { video: {
let v = output["media"]["track"] let v = output["media"]["track"]

View File

@ -105,13 +105,12 @@ async fn identify_resource(
.await .await
.map_err(|_| ApiError::NotFound)?; .map_err(|_| ApiError::NotFound)?;
identify(path).await.map(|info| Json(info)).map_err(|e| { identify(path)
eprintln!( .await
"Unhandled error occured while identifing the resource: {}", .map(|info| Json(info))
e .ok_or_else(|| ApiError::BadRequest {
); error: "Invalid video file. Could not parse informations.".to_string(),
ApiError::InternalError })
})
} }
/// Get attachments /// Get attachments

View File

@ -54,7 +54,7 @@ impl Transcoder {
pub async fn build_master(&self, resource: String, slug: String) -> Option<String> { pub async fn build_master(&self, resource: String, slug: String) -> Option<String> {
let mut master = String::from("#EXTM3U\n"); let mut master = String::from("#EXTM3U\n");
let path = get_path(resource, slug).await.ok()?; let path = get_path(resource, slug).await.ok()?;
let info = identify(path).await.ok()?; let info = identify(path).await?;
// TODO: Only add this if transmuxing is possible. // TODO: Only add this if transmuxing is possible.
if true { if true {