fix: ignore further classified mime-types (#707)

This commit is contained in:
三咲雅 · Misaki Masa 2024-02-21 17:28:18 +08:00 committed by GitHub
parent 0cb572a837
commit 17f946dcdb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 17 additions and 37 deletions

View file

@ -1,5 +1,12 @@
local M = {}
local function match_mimetype(s)
local type, subtype = s:match("([-a-z]+/)([+-.a-zA-Z0-9]+)%s*$")
if string.find("application/audio/biosig/chemical/font/image/inode/message/model/rinex/text/vector/video/x-epoc/", type, 1, true) then
return type .. subtype
end
end
function M:preload()
local urls = {}
for _, file in ipairs(self.files) do
@ -14,7 +21,7 @@ function M:preload()
local mimes, last = {}, ya.time()
local flush = function(force)
if not force and ya.time() - last < 0.1 then
if not force and ya.time() - last < 0.3 then
return
end
if next(mimes) then
@ -23,9 +30,9 @@ function M:preload()
end
end
local i, j = 1, 0
local i, j, mime = 1, 0, nil
repeat
local next, event = child:read_line_with { timeout = 300 }
local line, event = child:read_line_with { timeout = 300 }
if event == 3 then
flush(true)
goto continue
@ -33,9 +40,11 @@ function M:preload()
break
end
next = next:gsub("[\r\n]+$", "")
if ya.mime_valid(next) then
j, mimes[urls[i]] = j + 1, next
mime = match_mimetype(line)
if mime and string.find(line, mime, 1, true) ~= 1 then
goto continue
elseif mime then
j, mimes[urls[i]] = j + 1, mime
flush(false)
end

View file

@ -2,7 +2,6 @@ use std::ops::ControlFlow;
use mlua::{Lua, Table};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use yazi_shared::mime_valid;
use super::Utils;
@ -41,11 +40,6 @@ impl Utils {
})?,
)?;
ya.set(
"mime_valid",
lua.create_function(|_, mime: mlua::String| Ok(mime_valid(mime.as_bytes())))?,
)?;
Ok(())
}
}

View file

@ -1,3 +1,5 @@
pub const MIME_DIR: &str = "inode/directory";
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum CharKind {
Space,

View file

@ -9,7 +9,6 @@ mod errors;
pub mod event;
pub mod fs;
mod layer;
mod mime;
mod natsort;
mod number;
mod os;
@ -25,7 +24,6 @@ pub use defer::*;
pub use env::*;
pub use errors::*;
pub use layer::*;
pub use mime::*;
pub use natsort::*;
pub use number::*;
pub use os::*;

View file

@ -1,23 +0,0 @@
pub const MIME_DIR: &str = "inode/directory";
pub fn mime_valid(b: &[u8]) -> bool {
let parts = b.split(|&b| b == b'/').collect::<Vec<_>>();
if parts.len() != 2 || parts[1].is_empty() {
return false;
}
matches!(
parts[0],
b"application"
| b"audio"
| b"example"
| b"font"
| b"image"
| b"inode"
| b"message"
| b"model"
| b"multipart"
| b"text"
| b"video"
)
}