Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Do not sniff mime types
Browse files Browse the repository at this point in the history
Instead rely on the Content-Type header (remote resources) and the file
extension (local resources) to identify SVG images.  This removes the
dependency on the file tool and on libmagic, and also the entire magic
module, which simplifies the code and the build a bit, and removes a
runtime dependency.

It also effectively enables SVG images on Windows which so far did
mostly not work because Windows neither includes the file tool nor
libmagic, so users had to go out of their way and install file/libmagic
to get SVG images support on Windows.
  • Loading branch information
swsnr committed Feb 21, 2023
1 parent e3cedc7 commit 2e7ca7b
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 267 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ jobs:
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
# On Linux link against libmagic, since it's almost always available
cargo_opts: "--features magic"
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
cargo_opts: "--no-default-features --features static"
Expand Down
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Use `cargo release` to create a new release.

## [Unreleased]

### Added
- With `--features magic` use `libmagic` to check for SVG data, instead of shelling out to `file` (see [GH-236]).

### Changed
- Update all dependencies.
- No longer sniff mime type from contents to identify SVG images.
Instead rely on the `Content-Type` header for HTTP(S) images and the file extension for local resources (see [GH-239]).

### Fixed
- Use `less -r` instead of `less -R` in `mdless` if both `$PAGER` and `$MDCAT_PAGER` are unset (see [GH-238]).

[GH-236]: https://github.com/swsnr/mdcat/pull/236
[GH-238]: https://github.com/swsnr/mdcat/issues/238
[GH-239]: https://github.com/swsnr/mdcat/pull/239

## [1.0.0] – 2023-01-07

Expand Down
35 changes: 11 additions & 24 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ env_proxy = "0.4.1"
gethostname = "0.4.1"
image = "0.24.5"
mime = "0.3.16"
mime_guess = "2.0.4"
pulldown-cmark = { version = "0.9.2", default-features = false, features = ['simd'] }
shell-words = "1.1.0"
tracing = { version = "0.1.37", features = ["release_max_level_warn"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
url = "2.3.1"
reqwest = { version = "0.11.14", default-features = false, features = ["gzip", "brotli", "blocking"]}
once_cell = "1.17.0"
magic = { version = "0.13.0", optional = true }

[dependencies.syntect]
version = "5.0.0"
Expand Down
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ Try `mdcat --help` or read the [mdcat(1)](./mdcat.1.adoc) manpage.
* [Chocolatey]: `choco install mdcat`
* You can also build `mdcat` manually with `cargo install mdcat`.

[Homebrew]: https://brew.sh
[MacPorts]: https://www.macports.org
[Arch Linux]: https://www.archlinux.org/packages/community/x86_64/mdcat/
[scoop]: https://github.com/lukesampson/scoop
[chocolatey]: https://github.com/chocolatey

## Building

Run `cargo build --release`.
The resulting `mdcat` executable links against the system's SSL library, i.e. openssl on Linux.
To build a self-contained executable use `cargo build --features=static`; the resulting executable uses a pure Rust SSL implementation.
It still uses the system's CA roots however.

Pass `--features magic` to use `libmagic` instead of the `file` program to check mimetypes.

The build process also generates the following additional files in `$OUT_DIR`:

* Completions for Bash, Zsh, Fish, and Powershell, for both `mdcat` and `mdless`, in `completions` sub-directory.
Expand All @@ -92,16 +96,6 @@ If you package mdcat you may want to include these files too.

[AsciiDoctor]: https://asciidoctor.org/

## Requirements

If `mdcat` wasn't built with the `magic` feature, image type detection requires the `file` tool with support for `--brief` and `--mime-type` flags to be available in `$PATH`.

[Homebrew]: https://brew.sh
[MacPorts]: https://www.macports.org
[Arch Linux]: https://www.archlinux.org/packages/community/x86_64/mdcat/
[scoop]: https://github.com/lukesampson/scoop
[chocolatey]: https://github.com/chocolatey

## Troubleshooting

`mdcat` can output extensive tracing information when asked to.
Expand Down
8 changes: 4 additions & 4 deletions mdcat.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ In particular this disables all image support which relies on proprietary escape
In iTerm2, Kitty, Terminology and WezTerm mdcat prints inline images.
mdcat supports most standard pixel formats by default.

mdcat silently ignores images larger than 100 MiB.

For some terminals mdcat needs to detect the mimetype.
If `mdcat` was not built with `libmagic` (see output of `--version` to check) it uses the `file --brief --mime-type` for this purpose, and thus requires a compatible `file` utility in `$PATH`.
mdcat silently ignores images larger than 100 MiB, under the assumption that images of that size cannot reasonably be rendered in a terminal.

=== SVG support

Expand All @@ -65,6 +62,9 @@ In Terminology mdcat also renders SVG images, using the built-in support of Term
In iTerm2, Kitty and WezTerm mdcat requires `rsvg-convert` to render SVG images to pixel graphics before displaying them;
if `rsvg-convert` is not found in `$PATH` mdcat does not render SVG images in these terminals.

For local SVG files mdcat relies on the file extension to identify SVG images.
For remote images from HTTP(S) URLs mdcat inspects the `Content-Type` header to identify SVG images.

=== HTTP/HTTPS support

mdcat fetches images from HTTP(S) URLs for rendering if the underlying terminal supports image rendering;
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::terminal::capabilities::TerminalCapabilities;
use crate::terminal::TerminalSize;
use url::Url;

mod magic;
mod references;
mod resources;
mod svg;
Expand Down
201 changes: 0 additions & 201 deletions src/magic.rs

This file was deleted.

Loading

0 comments on commit 2e7ca7b

Please sign in to comment.