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())
.insert_header(ContentType::json())
.body(format!(
"{{ \"status\": \"{status}\", \"error\": \"{err}\" }}",
"{{ \"status\": \"{status}\", \"errors\": [\"{err}\"] }}",
status = self.status_code(),
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");
}
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> =
HashMap::from([("subrip", "srt"), ("ass", "ass"), ("vtt", "vtt")]);
@ -138,7 +138,9 @@ pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> {
.output()
.await
.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 general = output["media"]["track"]
@ -182,8 +184,8 @@ pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> {
.collect();
if !PathBuf::from(format!("/metadata/{sha}")).exists() {
std::fs::create_dir_all(format!("/metadata/{sha}/att"))?;
std::fs::create_dir_all(format!("/metadata/{sha}/sub"))?;
std::fs::create_dir_all(format!("/metadata/{sha}/att")).unwrap();
std::fs::create_dir_all(format!("/metadata/{sha}/sub")).unwrap();
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())
}
Ok(MediaInfo {
length: parse::<f32>(&general["Duration"]).unwrap(),
Some(MediaInfo {
length: parse::<f32>(&general["Duration"])?,
container: general["Format"].as_str().unwrap().to_string(),
video: {
let v = output["media"]["track"]

View File

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

View File

@ -54,7 +54,7 @@ impl Transcoder {
pub async fn build_master(&self, resource: String, slug: String) -> Option<String> {
let mut master = String::from("#EXTM3U\n");
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.
if true {