Skip to content

Commit 85a1f4f

Browse files
committed
Switch from mime dependency to mediatype
The mime library is not actively maintained anymore: hyperium/mime#135
1 parent d47fc60 commit 85a1f4f

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Possible log types:
1414

1515
### Unreleased
1616

17-
...
17+
- [changed] Switch from `mime` crate to `mediatype`. The `Mime` re-export
18+
changed and must be adjusted. A `mediatype` re-export is provided. (#64)
1819

1920
### v0.15.1 (2021-12-06)
2021

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ byteorder = "1.0"
2727
data-encoding = "2.1"
2828
form_urlencoded = { version = "1", optional = true }
2929
log = "0.4"
30-
mime = "0.3"
30+
mediatype = { version = "0.19", features = ["serde"] }
3131
thiserror = "1"
3232
reqwest = { version = "0.11", features = ["rustls-tls-native-roots", "multipart"], default-features = false }
3333
serde = { version = "1.0", features = ["derive"] }

examples/send_e2e_file.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::{ffi::OsStr, fs::File, io::Read, path::Path, process};
22

33
use docopt::Docopt;
4-
use threema_gateway::{encrypt_file_data, ApiBuilder, FileMessage, RenderingType};
4+
use threema_gateway::{
5+
encrypt_file_data, mediatype::MediaTypeBuf, ApiBuilder, FileMessage, RenderingType,
6+
};
57

68
const USAGE: &str = "
79
Usage: send_e2e_file [options] <from> <to> <secret> <private-key> <path-to-file>
@@ -102,15 +104,22 @@ async fn main() {
102104
api.blob_upload_raw(&et, false).await,
103105
"Could not upload thumbnail to blob server"
104106
);
105-
let thumbnail_media_type =
106-
mime_guess::from_path(&thumbpath.unwrap()).first_or_octet_stream();
107+
let thumbnail_media_type: MediaTypeBuf = mime_guess::from_path(thumbpath.unwrap())
108+
.first_or_octet_stream()
109+
.to_string()
110+
.parse()
111+
.unwrap();
107112
Some((blob_id, thumbnail_media_type))
108113
} else {
109114
None
110115
};
111116

112117
// Create file message
113-
let file_media_type = mime_guess::from_path(&filepath).first_or_octet_stream();
118+
let file_media_type: MediaTypeBuf = mime_guess::from_path(filepath)
119+
.first_or_octet_stream()
120+
.to_string()
121+
.parse()
122+
.unwrap();
114123
let file_name = filepath.file_name().and_then(OsStr::to_str);
115124
let msg = FileMessage::builder(file_blob_id, key, file_media_type, file_data.len() as u32)
116125
.thumbnail_opt(thumb_blob_id)

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mod lookup;
8282
mod receive;
8383
mod types;
8484

85-
pub use mime::Mime;
85+
pub use mediatype;
8686
pub use sodiumoxide::crypto::{
8787
box_::{PublicKey, SecretKey},
8888
secretbox::Key,

src/types.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::{default::Default, fmt, str::FromStr, string::ToString};
22

33
use data_encoding::{HEXLOWER, HEXLOWER_PERMISSIVE};
4+
use mediatype::MediaTypeBuf;
45
use serde::{Serialize, Serializer};
56

67
use crate::{
78
errors::{ApiError, FileMessageBuilderError},
8-
Key, Mime,
9+
Key,
910
};
1011

1112
/// A message type.
@@ -79,15 +80,15 @@ pub struct FileMessage {
7980
file_blob_id: BlobId,
8081
#[serde(rename = "m")]
8182
#[serde(serialize_with = "serialize_to_string")]
82-
file_media_type: Mime,
83+
file_media_type: MediaTypeBuf,
8384

8485
#[serde(rename = "t")]
8586
#[serde(skip_serializing_if = "Option::is_none")]
8687
thumbnail_blob_id: Option<BlobId>,
8788
#[serde(rename = "p")]
8889
#[serde(skip_serializing_if = "Option::is_none")]
8990
#[serde(serialize_with = "serialize_opt_to_string")]
90-
thumbnail_media_type: Option<Mime>,
91+
thumbnail_media_type: Option<MediaTypeBuf>,
9192

9293
#[serde(rename = "k")]
9394
#[serde(serialize_with = "key_to_hex")]
@@ -146,7 +147,7 @@ impl FileMessage {
146147
pub fn builder(
147148
file_blob_id: BlobId,
148149
blob_encryption_key: Key,
149-
media_type: Mime,
150+
media_type: impl Into<MediaTypeBuf>,
150151
file_size_bytes: u32,
151152
) -> FileMessageBuilder {
152153
FileMessageBuilder::new(
@@ -161,9 +162,9 @@ impl FileMessage {
161162
/// Builder for [`FileMessage`](struct.FileMessage.html).
162163
pub struct FileMessageBuilder {
163164
file_blob_id: BlobId,
164-
file_media_type: Mime,
165+
file_media_type: MediaTypeBuf,
165166
thumbnail_blob_id: Option<BlobId>,
166-
thumbnail_media_type: Option<Mime>,
167+
thumbnail_media_type: Option<MediaTypeBuf>,
167168
blob_encryption_key: Key,
168169
file_name: Option<String>,
169170
file_size_bytes: u32,
@@ -190,12 +191,12 @@ impl FileMessageBuilder {
190191
pub fn new(
191192
file_blob_id: BlobId,
192193
blob_encryption_key: Key,
193-
media_type: Mime,
194+
media_type: impl Into<MediaTypeBuf>,
194195
file_size_bytes: u32,
195196
) -> Self {
196197
FileMessageBuilder {
197198
file_blob_id,
198-
file_media_type: media_type,
199+
file_media_type: media_type.into(),
199200
thumbnail_blob_id: None,
200201
thumbnail_media_type: None,
201202
blob_encryption_key,
@@ -221,7 +222,7 @@ impl FileMessageBuilder {
221222
/// Before calling this function, you need to symmetrically encrypt the
222223
/// thumbnail data (in JPEG format) with the same key used for the file
223224
/// data and with the nonce `000...2`.
224-
pub fn thumbnail(self, blob_id: BlobId, media_type: Mime) -> Self {
225+
pub fn thumbnail(self, blob_id: BlobId, media_type: impl Into<MediaTypeBuf>) -> Self {
225226
self.thumbnail_opt(Some((blob_id, media_type)))
226227
}
227228

@@ -230,11 +231,11 @@ impl FileMessageBuilder {
230231
/// Before calling this function, you need to symmetrically encrypt the
231232
/// thumbnail data (in JPEG format) with the same key used for the file
232233
/// data and with the nonce `000...2`.
233-
pub fn thumbnail_opt(mut self, blob: Option<(BlobId, Mime)>) -> Self {
234+
pub fn thumbnail_opt(mut self, blob: Option<(BlobId, impl Into<MediaTypeBuf>)>) -> Self {
234235
match blob {
235236
Some((blob_id, media_type)) => {
236237
self.thumbnail_blob_id = Some(blob_id);
237-
self.thumbnail_media_type = Some(media_type);
238+
self.thumbnail_media_type = Some(media_type.into());
238239
}
239240
None => {
240241
self.thumbnail_blob_id = None;
@@ -425,6 +426,7 @@ fn key_to_hex<S: Serializer>(val: &Key, serializer: S) -> Result<S::Ok, S::Error
425426
mod test {
426427
use std::collections::HashMap;
427428

429+
use mediatype::media_type;
428430
use serde_json as json;
429431

430432
use super::*;
@@ -544,10 +546,10 @@ mod test {
544546
]);
545547
let file_blob_id = BlobId::from_str("0123456789abcdef0123456789abcdef").unwrap();
546548
let thumb_blob_id = BlobId::from_str("abcdef0123456789abcdef0123456789").unwrap();
547-
let jpeg: Mime = "image/jpeg".parse().unwrap();
548-
let png: Mime = "image/jpeg".parse().unwrap();
549-
let msg = FileMessage::builder(file_blob_id.clone(), key.clone(), jpeg.clone(), 2048)
550-
.thumbnail(thumb_blob_id.clone(), png.clone())
549+
let jpeg = media_type!(IMAGE/JPEG);
550+
let png = media_type!(IMAGE/PNG);
551+
let msg = FileMessage::builder(file_blob_id.clone(), key.clone(), &jpeg, 2048)
552+
.thumbnail(thumb_blob_id.clone(), &png)
551553
.file_name("hello.jpg")
552554
.description(String::from("An image file"))
553555
.rendering_type(RenderingType::Media)
@@ -557,7 +559,7 @@ mod test {
557559
assert_eq!(msg.file_blob_id, file_blob_id);
558560
assert_eq!(msg.file_media_type, jpeg);
559561
assert_eq!(msg.thumbnail_blob_id, Some(thumb_blob_id));
560-
assert_eq!(msg.thumbnail_media_type, Some(png));
562+
assert_eq!(msg.thumbnail_media_type, Some(png.into()));
561563
assert_eq!(msg.blob_encryption_key, key);
562564
assert_eq!(msg.file_name, Some("hello.jpg".to_string()));
563565
assert_eq!(msg.file_size_bytes, 2048);

0 commit comments

Comments
 (0)