From 85a1f4f5d4c183893ff78e77c8f379e974d1f42c Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sat, 24 Dec 2022 16:20:50 +0100 Subject: [PATCH] Switch from mime dependency to mediatype The mime library is not actively maintained anymore: https://github.com/hyperium/mime/issues/135 --- CHANGELOG.md | 3 ++- Cargo.toml | 2 +- examples/send_e2e_file.rs | 17 +++++++++++++---- src/lib.rs | 2 +- src/types.rs | 34 ++++++++++++++++++---------------- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 510b93f99..10ff5b6a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,8 @@ Possible log types: ### Unreleased -... +- [changed] Switch from `mime` crate to `mediatype`. The `Mime` re-export + changed and must be adjusted. A `mediatype` re-export is provided. (#64) ### v0.15.1 (2021-12-06) diff --git a/Cargo.toml b/Cargo.toml index c58c8786a..38f99b16d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ byteorder = "1.0" data-encoding = "2.1" form_urlencoded = { version = "1", optional = true } log = "0.4" -mime = "0.3" +mediatype = { version = "0.19", features = ["serde"] } thiserror = "1" reqwest = { version = "0.11", features = ["rustls-tls-native-roots", "multipart"], default-features = false } serde = { version = "1.0", features = ["derive"] } diff --git a/examples/send_e2e_file.rs b/examples/send_e2e_file.rs index 0abf2393e..ade30ff7d 100644 --- a/examples/send_e2e_file.rs +++ b/examples/send_e2e_file.rs @@ -1,7 +1,9 @@ use std::{ffi::OsStr, fs::File, io::Read, path::Path, process}; use docopt::Docopt; -use threema_gateway::{encrypt_file_data, ApiBuilder, FileMessage, RenderingType}; +use threema_gateway::{ + encrypt_file_data, mediatype::MediaTypeBuf, ApiBuilder, FileMessage, RenderingType, +}; const USAGE: &str = " Usage: send_e2e_file [options] @@ -102,15 +104,22 @@ async fn main() { api.blob_upload_raw(&et, false).await, "Could not upload thumbnail to blob server" ); - let thumbnail_media_type = - mime_guess::from_path(&thumbpath.unwrap()).first_or_octet_stream(); + let thumbnail_media_type: MediaTypeBuf = mime_guess::from_path(thumbpath.unwrap()) + .first_or_octet_stream() + .to_string() + .parse() + .unwrap(); Some((blob_id, thumbnail_media_type)) } else { None }; // Create file message - let file_media_type = mime_guess::from_path(&filepath).first_or_octet_stream(); + let file_media_type: MediaTypeBuf = mime_guess::from_path(filepath) + .first_or_octet_stream() + .to_string() + .parse() + .unwrap(); let file_name = filepath.file_name().and_then(OsStr::to_str); let msg = FileMessage::builder(file_blob_id, key, file_media_type, file_data.len() as u32) .thumbnail_opt(thumb_blob_id) diff --git a/src/lib.rs b/src/lib.rs index b88b83a84..44aaa7dfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,7 @@ mod lookup; mod receive; mod types; -pub use mime::Mime; +pub use mediatype; pub use sodiumoxide::crypto::{ box_::{PublicKey, SecretKey}, secretbox::Key, diff --git a/src/types.rs b/src/types.rs index 0ec6f415d..4e5d8cb14 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,11 +1,12 @@ use std::{default::Default, fmt, str::FromStr, string::ToString}; use data_encoding::{HEXLOWER, HEXLOWER_PERMISSIVE}; +use mediatype::MediaTypeBuf; use serde::{Serialize, Serializer}; use crate::{ errors::{ApiError, FileMessageBuilderError}, - Key, Mime, + Key, }; /// A message type. @@ -79,7 +80,7 @@ pub struct FileMessage { file_blob_id: BlobId, #[serde(rename = "m")] #[serde(serialize_with = "serialize_to_string")] - file_media_type: Mime, + file_media_type: MediaTypeBuf, #[serde(rename = "t")] #[serde(skip_serializing_if = "Option::is_none")] @@ -87,7 +88,7 @@ pub struct FileMessage { #[serde(rename = "p")] #[serde(skip_serializing_if = "Option::is_none")] #[serde(serialize_with = "serialize_opt_to_string")] - thumbnail_media_type: Option, + thumbnail_media_type: Option, #[serde(rename = "k")] #[serde(serialize_with = "key_to_hex")] @@ -146,7 +147,7 @@ impl FileMessage { pub fn builder( file_blob_id: BlobId, blob_encryption_key: Key, - media_type: Mime, + media_type: impl Into, file_size_bytes: u32, ) -> FileMessageBuilder { FileMessageBuilder::new( @@ -161,9 +162,9 @@ impl FileMessage { /// Builder for [`FileMessage`](struct.FileMessage.html). pub struct FileMessageBuilder { file_blob_id: BlobId, - file_media_type: Mime, + file_media_type: MediaTypeBuf, thumbnail_blob_id: Option, - thumbnail_media_type: Option, + thumbnail_media_type: Option, blob_encryption_key: Key, file_name: Option, file_size_bytes: u32, @@ -190,12 +191,12 @@ impl FileMessageBuilder { pub fn new( file_blob_id: BlobId, blob_encryption_key: Key, - media_type: Mime, + media_type: impl Into, file_size_bytes: u32, ) -> Self { FileMessageBuilder { file_blob_id, - file_media_type: media_type, + file_media_type: media_type.into(), thumbnail_blob_id: None, thumbnail_media_type: None, blob_encryption_key, @@ -221,7 +222,7 @@ impl FileMessageBuilder { /// Before calling this function, you need to symmetrically encrypt the /// thumbnail data (in JPEG format) with the same key used for the file /// data and with the nonce `000...2`. - pub fn thumbnail(self, blob_id: BlobId, media_type: Mime) -> Self { + pub fn thumbnail(self, blob_id: BlobId, media_type: impl Into) -> Self { self.thumbnail_opt(Some((blob_id, media_type))) } @@ -230,11 +231,11 @@ impl FileMessageBuilder { /// Before calling this function, you need to symmetrically encrypt the /// thumbnail data (in JPEG format) with the same key used for the file /// data and with the nonce `000...2`. - pub fn thumbnail_opt(mut self, blob: Option<(BlobId, Mime)>) -> Self { + pub fn thumbnail_opt(mut self, blob: Option<(BlobId, impl Into)>) -> Self { match blob { Some((blob_id, media_type)) => { self.thumbnail_blob_id = Some(blob_id); - self.thumbnail_media_type = Some(media_type); + self.thumbnail_media_type = Some(media_type.into()); } None => { self.thumbnail_blob_id = None; @@ -425,6 +426,7 @@ fn key_to_hex(val: &Key, serializer: S) -> Result