Skip to content

Commit

Permalink
MIME type sniffing (#30)
Browse files Browse the repository at this point in the history
* Preliminary implementation of MIME type sniffing

* Some refactoring and improvements.

* Fixing erroneous commit

* Made all tests pass

* Removed comment

* Applying sad formatting

* Addressed some comments

* Adding banyan fork of mime

* Fixed a small bug and added one expect

* Reformat

* Variable rename

* Fixed test issue.
  • Loading branch information
PlamenHristov authored Jun 7, 2024
1 parent 4428b3f commit e95b1bc
Show file tree
Hide file tree
Showing 13 changed files with 576 additions and 37 deletions.
35 changes: 33 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ reqwest = { version = "^0.12", default-features = false, optional = true, featur
serde = { version = "^1", features = ["derive"], optional = true }
serde_json = { version = "^1", optional = true }
url = { version = "^2", optional = true }
mime = {git = "https://github.com/banyancomputer/mime.git", rev = "d1a1744"}
mime_guess = "2.0.4"


[[example]]
name = "full_fs_exercise"
Expand Down
2 changes: 1 addition & 1 deletion src/codec/crypto/authentication_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl AuthenticationTag {
}

pub fn from_bytes(data: &[u8; TAG_LENGTH]) -> Self {
Self(data.clone())
Self(*data)
}

pub async fn encode<W: AsyncWrite + Unpin + Send>(
Expand Down
2 changes: 1 addition & 1 deletion src/codec/crypto/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Nonce {
}

pub fn from_bytes(data: &[u8; NONCE_LENGTH]) -> Self {
Self(data.clone())
Self(*data)
}

pub(crate) async fn encode<W: AsyncWrite + Unpin + Send>(
Expand Down
4 changes: 2 additions & 2 deletions src/codec/data_storage/data_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl DataBlock {
self.contents.len() >= self.data_options.chunk_count().into()
}

pub fn parse<'a>(input: Stream<'a>) -> ParserResult<'a, Self> {
pub fn parse(input: Stream<'_>) -> ParserResult<'_, Self> {
let (input, version) = le_u8.parse_peek(input)?;

if version != 0x01 {
Expand Down Expand Up @@ -172,7 +172,7 @@ impl DataBlock {
Ok((input, block))
}

pub fn parse_with_magic<'a>(input: Stream<'a>) -> ParserResult<'a, Self> {
pub fn parse_with_magic(input: Stream<'_>) -> ParserResult<'_, Self> {
let (input, _magic) = banyan_data_magic_tag(input)?;
Self::parse(input)
}
Expand Down
4 changes: 2 additions & 2 deletions src/codec/data_storage/encrypted_data_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl EncryptedDataChunk {
Ok((self.0.len(), cid))
}

pub fn decrypt<'a>(
&'a self,
pub fn decrypt(
&self,
options: &DataOptions,
access_key: &AccessKey,
) -> Result<DataChunk, EncryptedDataChunkError> {
Expand Down
15 changes: 15 additions & 0 deletions src/filesystem/drive/directory_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct DirectoryEntry {
name: NodeName,
kind: NodeKind,

mime_type: Option<mime::MediaType>,

size: u64,
}

Expand Down Expand Up @@ -48,6 +50,17 @@ impl DirectoryEntry {
pub fn size(&self) -> u64 {
self.size
}

pub fn mime_type(&self) -> Option<mime::MediaType> {
match self.kind {
NodeKind::File => self.mime_type.clone(),
NodeKind::Directory => None,
NodeKind::AssociatedData => None,
NodeKind::InternalLink => None,
NodeKind::NativeMount => None,
NodeKind::Unknown(_) => None,
}
}
}

impl TryFrom<&Node> for DirectoryEntry {
Expand All @@ -63,6 +76,8 @@ impl TryFrom<&Node> for DirectoryEntry {
name: node.name().clone(),
kind: node.kind().clone(),

mime_type: node.mime_type(),

size: node.size(),
})
}
Expand Down
Loading

0 comments on commit e95b1bc

Please sign in to comment.