mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 18:23:52 -06:00
fix: keep audio-only containers as sound in PPTX (#2390)
Signed-off-by: MaheshDesu <mahesh.desu3@gmail.com>
This commit is contained in:
@@ -73,6 +73,19 @@ pub fn content_type(name: &str, bytes: &[u8]) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// The audio MIME for a container whose bytes are audio-only, so the PPTX writer stores it as
|
||||
/// sound rather than a movie. Video-bearing containers keep their `video/*` type.
|
||||
fn audio_content_type(ct: &'static str) -> &'static str {
|
||||
match ct {
|
||||
"video/x-ms-wmv" => "audio/x-ms-wma",
|
||||
"video/mp4" => "audio/mp4",
|
||||
"video/webm" => "audio/webm",
|
||||
"video/ogg" => "audio/ogg",
|
||||
"video/quicktime" => "audio/mp4",
|
||||
_ => ct,
|
||||
}
|
||||
}
|
||||
|
||||
/// Natural size of an image in points (96 dpi), fitted inside the slide.
|
||||
fn fitted(s: &Session, bytes: &[u8]) -> (f64, f64) {
|
||||
let (w, h) = image::ImageReader::new(std::io::Cursor::new(bytes))
|
||||
@@ -175,6 +188,10 @@ fn media(s: &mut Session, p: &Value, video: bool) -> Result<Value> {
|
||||
Err(_) if !video && !ct.starts_with("audio/") => return Err(bad(cmd, format!("{name} is not a supported audio file ({ct})"))),
|
||||
_ => {}
|
||||
}
|
||||
// The extension decides the container's MIME even when the probe finds only an audio stream
|
||||
// (#2390): keep it `audio/*` so the writer stores sound, not a movie. The validation above
|
||||
// still uses the original `ct`, because an unreadable file has no audio stream.
|
||||
let ct = if !video && has_audio && !has_video { audio_content_type(ct) } else { ct };
|
||||
let (duration_ms, width, height, playable) = match &info {
|
||||
Ok(i) => {
|
||||
let (w, h) = i.video.as_ref().map(|v| (v.width, v.height)).unwrap_or((0, 0));
|
||||
|
||||
@@ -182,7 +182,7 @@ mod tests {
|
||||
use deckcraft_model::ShapeId;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::cmd::base64_encode;
|
||||
use crate::cmd::{base64_decode, base64_encode};
|
||||
use crate::{MediaStatus, Session, UiRequest};
|
||||
|
||||
fn wav() -> Vec<u8> {
|
||||
@@ -291,6 +291,27 @@ mod tests {
|
||||
assert!(r["warning"].as_str().is_some_and(|w| w.contains("can't be played")), "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn audio_only_asf_saves_as_sound() {
|
||||
let mut s = Session::with_new();
|
||||
let bytes = include_bytes!("../../../media/tests/fixtures/tone.wma");
|
||||
let r = s.execute("insert.audio", &json!({"name": "tone.asf", "data": base64_encode(bytes)})).expect("insert");
|
||||
assert_eq!(r["contentType"], "audio/x-ms-wma", "{r}");
|
||||
|
||||
let saved = s.execute("file.saveBytes", &json!({"format": "pptx"})).expect("save");
|
||||
let zip = base64_decode(saved["data"].as_str().unwrap()).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(zip)).unwrap();
|
||||
let names: Vec<String> = (0..z.len()).map(|i| z.by_index(i).unwrap().name().to_string()).collect();
|
||||
assert!(names.iter().any(|n| n == "ppt/media/media1.wma"), "{names:?}");
|
||||
assert!(!names.iter().any(|n| n.ends_with(".wmv")), "{names:?}");
|
||||
|
||||
s.execute("file.openBytes", &json!({"name": "deck.pptx", "data": saved["data"]})).expect("open");
|
||||
let info = s.execute("media.info", &json!({})).expect("info");
|
||||
assert_eq!(info["video"], false, "{info}");
|
||||
assert!(info["durationMs"].as_u64().is_some_and(|ms| ms > 0), "{info}");
|
||||
assert_eq!(info["contentType"], "audio/x-ms-wma", "{info}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vp8_webm_is_playable_and_warns_that_only_the_audio_plays() {
|
||||
let mut s = Session::with_new();
|
||||
|
||||
Reference in New Issue
Block a user