Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Remove tree_magic_mini
Browse files Browse the repository at this point in the history
It's not exactly well-maintained, has a couple of bugs and obvious
programming mistakes, requires a system-wide shared mime database and
provides no way to detect the absence of said database.
  • Loading branch information
swsnr committed Oct 14, 2022
1 parent f40b3da commit a00bb6e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 80 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,9 @@ To publish a new release run `scripts/release` from the project directory.
- Move repository back to <https://github.com/lunaryorn/mdcat>.
- Restore release binaries.

### Removed
- Support for `tree_magic_mini` for mime-type detection; mdcat now only uses the `file` tool.

## [0.28.0] – 2022-07-31

### Changed
Expand Down
53 changes: 0 additions & 53 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions Cargo.toml
Expand Up @@ -12,9 +12,6 @@ license = "MPL-2.0"
authors = ["Sebastian Wiesner <sebastian@swsnr.de>"]
edition = "2021"

[features]
default = ["tree_magic_mini"]

[dependencies]
ansi_term = "0.12.1"
anyhow = "1.0.58"
Expand All @@ -30,7 +27,6 @@ tracing = { version = "0.1.35", features = ["release_max_level_warn"] }
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
ureq = { version = "2.5.0", features = ["tls", "native-certs"] }
url = "2.2.2"
tree_magic_mini = { version = "3.0.3", optional = true }

[dependencies.syntect]
version = "5.0.0"
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -74,9 +74,7 @@ Try `mdcat --help` or read the [mdcat(1)](./mdcat.1.adoc) manpage.

## Requirements

- For image type detection either:
- a system-wide [shared mime database](https://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec/), or,
- if `mdcat` is built without default features, the `file` tool with support for `--brief` and `--mime-type` flags.
For image type detection either the `file` tool with support for `--brief` and `--mime-type` flags must be available in `$PATH`.

[Homebrew]: https://brew.sh
[MacPorts]: https://www.macports.org
Expand Down
38 changes: 18 additions & 20 deletions src/magic.rs
Expand Up @@ -6,7 +6,10 @@

//! Magic util functions for detecting image types.

use anyhow::Result;
use anyhow::{anyhow, Context};
use mime::Mime;
use tracing::{event, Level};

/// Whether the given data is SVG data.
pub fn is_svg(buffer: &[u8]) -> bool {
Expand All @@ -19,31 +22,26 @@ pub fn is_png(buffer: &[u8]) -> bool {
}

fn is_mimetype(buffer: &[u8], mime: &Mime) -> bool {
is_mimetype_impl(buffer, mime)
get_mimetype_for_buffer_with_file(buffer).map_or_else(
|error| {
event!(
Level::WARN,
?error,
"checking for mime type {} failed: {}",
mime,
error
);
false
},
|detected| detected == *mime,
)
}

#[cfg(feature = "tree_magic_mini")]
fn is_mimetype_impl(buffer: &[u8], expected_type: &Mime) -> bool {
tree_magic_mini::match_u8(expected_type.as_ref(), buffer)
}

#[cfg(not(feature = "tree_magic_mini"))]
fn is_mimetype_impl(buffer: &[u8], expected_type: &Mime) -> bool {
is_mimetype_file(buffer, mime).unwrap_or_else(|error| {
event!(Level::WARN, ?error, "checking for {} failed", mime);
false
})
}

#[cfg(not(feature = "tree_magic_mini"))]
fn is_mimetype_file(buffer: &[u8], expected_type: &Mime) -> Result<bool> {
fn get_mimetype_for_buffer_with_file(buffer: &[u8]) -> Result<Mime> {
use std::io::prelude::*;
use std::io::ErrorKind;
use std::process::*;

use anyhow::{anyhow, Context, Result};
use tracing::{event, Level};

let mut process = Command::new("file")
.arg("--brief")
.arg("--mime-type")
Expand Down Expand Up @@ -79,7 +77,7 @@ fn is_mimetype_file(buffer: &[u8], expected_type: &Mime) -> Result<bool> {
let detected_type = stdout
.parse::<Mime>()
.with_context(|| format!("Failed to parse mime type from output: {}", stdout))?;
Ok(detected_type == *expected_type)
Ok(detected_type)
} else {
Err(anyhow!(
"file --brief --mime-type failed with status {}: {}",
Expand Down

0 comments on commit a00bb6e

Please sign in to comment.