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 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() function M:preload()
local urls = {} local urls = {}
for _, file in ipairs(self.files) do for _, file in ipairs(self.files) do
@ -14,7 +21,7 @@ function M:preload()
local mimes, last = {}, ya.time() local mimes, last = {}, ya.time()
local flush = function(force) 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 return
end end
if next(mimes) then if next(mimes) then
@ -23,9 +30,9 @@ function M:preload()
end end
end end
local i, j = 1, 0 local i, j, mime = 1, 0, nil
repeat repeat
local next, event = child:read_line_with { timeout = 300 } local line, event = child:read_line_with { timeout = 300 }
if event == 3 then if event == 3 then
flush(true) flush(true)
goto continue goto continue
@ -33,9 +40,11 @@ function M:preload()
break break
end end
next = next:gsub("[\r\n]+$", "") mime = match_mimetype(line)
if ya.mime_valid(next) then if mime and string.find(line, mime, 1, true) ~= 1 then
j, mimes[urls[i]] = j + 1, next goto continue
elseif mime then
j, mimes[urls[i]] = j + 1, mime
flush(false) flush(false)
end end

View file

@ -2,7 +2,6 @@ use std::ops::ControlFlow;
use mlua::{Lua, Table}; use mlua::{Lua, Table};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use yazi_shared::mime_valid;
use super::Utils; 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(()) Ok(())
} }
} }

View file

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

View file

@ -9,7 +9,6 @@ mod errors;
pub mod event; pub mod event;
pub mod fs; pub mod fs;
mod layer; mod layer;
mod mime;
mod natsort; mod natsort;
mod number; mod number;
mod os; mod os;
@ -25,7 +24,6 @@ pub use defer::*;
pub use env::*; pub use env::*;
pub use errors::*; pub use errors::*;
pub use layer::*; pub use layer::*;
pub use mime::*;
pub use natsort::*; pub use natsort::*;
pub use number::*; pub use number::*;
pub use os::*; 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"
)
}