Use a MIME DB to determine the download URL file extensions

* Also apply it to the default MIME type for Mixcloud posts
* Add a dependency on the `mime_db` crate
This commit is contained in:
Paul van Tilburg 2022-08-15 21:06:00 +02:00
parent 59e1f8a987
commit 3a3fbc96f4
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
4 changed files with 28 additions and 8 deletions

12
Cargo.lock generated
View File

@ -1218,6 +1218,17 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime-db"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d7c816ec30c41f873e1eea969aa2261d78756629e468a427244ff8658b75e7d"
dependencies = [
"reqwest",
"serde",
"tokio",
]
[[package]]
name = "mio"
version = "0.6.23"
@ -1627,6 +1638,7 @@ dependencies = [
"cached",
"chrono",
"enum_dispatch",
"mime-db",
"reqwest",
"rocket",
"rocket_dyn_templates",

View File

@ -12,6 +12,7 @@ async-trait = "0.1.57"
cached = { version = "0.39.0", features = ["async"] }
chrono = { version = "0.4.19", features = ["serde"] }
enum_dispatch = "0.3.8"
mime-db = "1.6.0"
reqwest = { version = "0.11.10", features = ["json"] }
rocket = { version = "0.5.0-rc.2", features = ["json"] }
rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] }

View File

@ -199,7 +199,8 @@ impl From<UserWithCloudcasts> for Channel {
impl From<Cloudcast> for Item {
fn from(cloudcast: Cloudcast) -> Self {
let mut file = PathBuf::from(cloudcast.key.trim_end_matches('/'));
file.set_extension("m4a"); // FIXME: Don't hardcoded the extension!
let extension = mime_db::extension(DEFAULT_FILE_TYPE).expect("MIME type has extension");
file.set_extension(extension);
// FIXME: Don't hardcode the description!
let description = Some(format!("Taken from Mixcloud: {0}", cloudcast.url));

View File

@ -168,19 +168,25 @@ impl From<YouTubeVideoWithStream> for Item {
YouTubeVideoWithStream {
video,
stream,
content_length,
content_length: length,
}: YouTubeVideoWithStream,
) -> Self {
let id = video.id().to_string();
let mut link = Url::parse(VIDEO_BASE_URL).expect("valid URL");
let description = Some(format!("Taken from YouTube: {0}", link));
link.query_pairs_mut().append_pair("v", &id);
let mime_type = stream.mime_type().to_string();
// Ignore everything from MIME type parameter seperator on for extension look-up.
let mime_sep = mime_type.find(';').unwrap_or(mime_type.len());
let extension = mime_db::extension(&mime_type[..mime_sep]).unwrap_or_default();
let file = PathBuf::from(&id).with_extension(extension);
let enclosure = Enclosure {
file: PathBuf::from(&format!("{id}.webm")), // FIXME: Don't hardcode the extension!
mime_type: stream.mime_type().to_string(),
length: content_length,
file,
mime_type,
length,
};
let mut link = Url::parse(VIDEO_BASE_URL).expect("valid URL");
link.query_pairs_mut().append_pair("v", &id);
let description = Some(format!("Taken from YouTube: {0}", link));
let duration = Some(video.length().as_secs() as u32);
let image = video
.thumbnails()