Skip to content

Commit

Permalink
Switch from mime dependency to mediatype
Browse files Browse the repository at this point in the history
The mime library is not actively maintained anymore:
hyperium/mime#135
  • Loading branch information
dbrgn committed Dec 24, 2022
1 parent d47fc60 commit 85a1f4f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
17 changes: 13 additions & 4 deletions examples/send_e2e_file.rs
Original file line number Diff line number Diff line change
@@ -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] <from> <to> <secret> <private-key> <path-to-file>
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 18 additions & 16 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -79,15 +80,15 @@ 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")]
thumbnail_blob_id: Option<BlobId>,
#[serde(rename = "p")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_opt_to_string")]
thumbnail_media_type: Option<Mime>,
thumbnail_media_type: Option<MediaTypeBuf>,

#[serde(rename = "k")]
#[serde(serialize_with = "key_to_hex")]
Expand Down Expand Up @@ -146,7 +147,7 @@ impl FileMessage {
pub fn builder(
file_blob_id: BlobId,
blob_encryption_key: Key,
media_type: Mime,
media_type: impl Into<MediaTypeBuf>,
file_size_bytes: u32,
) -> FileMessageBuilder {
FileMessageBuilder::new(
Expand All @@ -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<BlobId>,
thumbnail_media_type: Option<Mime>,
thumbnail_media_type: Option<MediaTypeBuf>,
blob_encryption_key: Key,
file_name: Option<String>,
file_size_bytes: u32,
Expand All @@ -190,12 +191,12 @@ impl FileMessageBuilder {
pub fn new(
file_blob_id: BlobId,
blob_encryption_key: Key,
media_type: Mime,
media_type: impl Into<MediaTypeBuf>,
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,
Expand All @@ -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<MediaTypeBuf>) -> Self {
self.thumbnail_opt(Some((blob_id, media_type)))
}

Expand All @@ -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<MediaTypeBuf>)>) -> 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;
Expand Down Expand Up @@ -425,6 +426,7 @@ fn key_to_hex<S: Serializer>(val: &Key, serializer: S) -> Result<S::Ok, S::Error
mod test {
use std::collections::HashMap;

use mediatype::media_type;
use serde_json as json;

use super::*;
Expand Down Expand Up @@ -544,10 +546,10 @@ mod test {
]);
let file_blob_id = BlobId::from_str("0123456789abcdef0123456789abcdef").unwrap();
let thumb_blob_id = BlobId::from_str("abcdef0123456789abcdef0123456789").unwrap();
let jpeg: Mime = "image/jpeg".parse().unwrap();
let png: Mime = "image/jpeg".parse().unwrap();
let msg = FileMessage::builder(file_blob_id.clone(), key.clone(), jpeg.clone(), 2048)
.thumbnail(thumb_blob_id.clone(), png.clone())
let jpeg = media_type!(IMAGE/JPEG);
let png = media_type!(IMAGE/PNG);
let msg = FileMessage::builder(file_blob_id.clone(), key.clone(), &jpeg, 2048)
.thumbnail(thumb_blob_id.clone(), &png)
.file_name("hello.jpg")
.description(String::from("An image file"))
.rendering_type(RenderingType::Media)
Expand All @@ -557,7 +559,7 @@ mod test {
assert_eq!(msg.file_blob_id, file_blob_id);
assert_eq!(msg.file_media_type, jpeg);
assert_eq!(msg.thumbnail_blob_id, Some(thumb_blob_id));
assert_eq!(msg.thumbnail_media_type, Some(png));
assert_eq!(msg.thumbnail_media_type, Some(png.into()));
assert_eq!(msg.blob_encryption_key, key);
assert_eq!(msg.file_name, Some("hello.jpg".to_string()));
assert_eq!(msg.file_size_bytes, 2048);
Expand Down

0 comments on commit 85a1f4f

Please sign in to comment.