yazi/yazi-plugin/preset/plugins/mime-local.lua
三咲雅 misaki masa 9accf929f4
Some checks are pending
Cachix / Publish Flake (push) Waiting to run
Check / clippy (push) Waiting to run
Check / rustfmt (push) Waiting to run
Check / stylua (push) Waiting to run
Draft / build-unix (gcc-aarch64-linux-gnu, ubuntu-latest, aarch64-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-i686-linux-gnu, ubuntu-latest, i686-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-riscv64-linux-gnu, ubuntu-latest, riscv64gc-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-sparc64-linux-gnu, ubuntu-latest, sparc64-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (macos-latest, aarch64-apple-darwin) (push) Waiting to run
Draft / build-unix (macos-latest, x86_64-apple-darwin) (push) Waiting to run
Draft / build-unix (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Waiting to run
Draft / build-windows (windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Draft / build-windows (windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Draft / build-musl (aarch64-unknown-linux-musl) (push) Waiting to run
Draft / build-musl (x86_64-unknown-linux-musl) (push) Waiting to run
Draft / build-snap (amd64, ubuntu-latest) (push) Waiting to run
Draft / build-snap (arm64, ubuntu-24.04-arm) (push) Waiting to run
Draft / snap (push) Blocked by required conditions
Draft / draft (push) Blocked by required conditions
Draft / nightly (push) Blocked by required conditions
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
feat: trash bin (#4144)
2026-07-22 03:13:58 +08:00

108 lines
2.4 KiB
Lua

-- stylua: ignore
local TYPE_PATS = { "text", "image", "video", "application", "audio", "font", "inode", "message", "model", "vector", "biosig", "chemical", "rinex", "x%-epoc" }
local M = {}
function M:fetch(job)
local urls, paths = {}, {}
for i, file in ipairs(job.files) do
urls[i], paths[i] = file.url, tostring(file.path)
end
local child, err = M.spawn_file1(paths)
if not child then
M.placeholder(err, urls, paths)
return true, err
end
local updates, last = {}, ya.time()
local flush = function(force)
if not force and ya.time() - last < 0.3 then
return
end
if next(updates) then
ya.emit("update_mimes", { updates = updates })
updates, last = {}, ya.time()
end
end
local i, state, match, ignore = 1, {}, nil, nil
repeat
local line, event = child:read_line_with { timeout = 300 }
if event == 3 then
flush(true)
goto continue
elseif event ~= 0 then
break
end
match, ignore = M.match_mimetype(line)
if match then
updates[urls[i]], state[i], i = match, true, i + 1
flush(false)
elseif not ignore then
state[i], i = false, i + 1
end
::continue::
until i > #paths
flush(true)
return state
end
function M.match_mimetype(line)
for _, pat in ipairs(TYPE_PATS) do
local typ, sub = line:match(string.format("(%s/)([+-.a-zA-Z0-9]+)%%s+$", pat))
if not sub then
elseif line:find(typ .. sub, 1, true) == 1 then
return typ:gsub("^x%-", "", 1) .. sub:gsub("^x%-", "", 1):gsub("^vnd%.", "", 1)
else
return nil, true
end
end
end
function M.file1_bin() return os.getenv("YAZI_FILE_ONE") or "file" end
function M.spawn_file1(paths)
local bin = M.file1_bin()
local windows = ya.target_family() == "windows"
local cmd = Command(bin):arg({ "-bL", "--mime-type" }):stdout(Command.PIPED)
if windows then
cmd:arg({ "-f", "-" }):stdin(Command.PIPED)
else
cmd:arg("--"):arg(paths)
end
local child, err = cmd:spawn()
if not child then
local e = Error.fs {
kind = err.kind or "Other",
code = err.code,
message = string.format("Failed to start `%s`, error: %s", bin, err),
}
return nil, e
elseif windows then
child:write_all(table.concat(paths, "\n"))
child:flush()
ya.drop(child:take_stdin())
end
return child
end
function M.placeholder(err, urls, paths)
if err.kind ~= "NotFound" then
return
end
local updates = {}
for i = 1, #paths do
updates[urls[i]] = "null/file1-not-found"
end
ya.emit("update_mimes", { updates = updates })
end
return M