2024-02-24 21:56:40 +03:00
|
|
|
local SUPPORTED_TYPES = "application/audio/biosig/chemical/font/image/inode/message/model/rinex/text/vector/video/x-epoc/"
|
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
local M = {}
|
|
|
|
|
2024-02-21 12:28:18 +03:00
|
|
|
local function match_mimetype(s)
|
2024-02-24 21:56:40 +03:00
|
|
|
local type, sub = s:match("([-a-z]+/)([+-.a-zA-Z0-9]+)%s*$")
|
|
|
|
if type and sub and string.find(SUPPORTED_TYPES, type, 1, true) then
|
|
|
|
return type .. sub
|
2024-02-21 12:28:18 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-20 12:25:55 +03:00
|
|
|
function M:prefetch()
|
2024-01-01 13:28:38 +03:00
|
|
|
local urls = {}
|
|
|
|
for _, file in ipairs(self.files) do
|
|
|
|
urls[#urls + 1] = tostring(file.url)
|
|
|
|
end
|
|
|
|
|
2024-03-01 06:21:44 +03:00
|
|
|
local cmd = os.getenv("YAZI_FILE_ONE") or "file"
|
|
|
|
local child, code = Command(cmd):args({ "-bL", "--mime-type" }):args(urls):stdout(Command.PIPED):spawn()
|
2024-01-01 13:28:38 +03:00
|
|
|
if not child then
|
2024-03-28 12:14:11 +03:00
|
|
|
ya.err(string.format("Spawn `%s` command returns %s", cmd, code))
|
2024-01-01 13:28:38 +03:00
|
|
|
return 0
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
|
|
|
|
2024-04-19 08:45:01 +03:00
|
|
|
local updates, last = {}, ya.time()
|
2024-01-01 13:28:38 +03:00
|
|
|
local flush = function(force)
|
2024-02-21 12:28:18 +03:00
|
|
|
if not force and ya.time() - last < 0.3 then
|
2024-01-01 13:28:38 +03:00
|
|
|
return
|
|
|
|
end
|
2024-04-19 08:45:01 +03:00
|
|
|
if next(updates) then
|
|
|
|
ya.manager_emit("update_mimetype", { updates = updates })
|
|
|
|
updates, last = {}, ya.time()
|
2024-01-01 13:28:38 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-19 08:45:01 +03:00
|
|
|
local i, j, valid = 1, 0, nil
|
2024-01-01 13:28:38 +03:00
|
|
|
repeat
|
2024-02-21 12:28:18 +03:00
|
|
|
local line, event = child:read_line_with { timeout = 300 }
|
2024-01-01 13:28:38 +03:00
|
|
|
if event == 3 then
|
|
|
|
flush(true)
|
|
|
|
goto continue
|
|
|
|
elseif event ~= 0 then
|
2023-12-26 14:48:33 +03:00
|
|
|
break
|
|
|
|
end
|
2024-01-01 13:28:38 +03:00
|
|
|
|
2024-04-19 08:45:01 +03:00
|
|
|
valid = match_mimetype(line)
|
|
|
|
if valid and string.find(line, valid, 1, true) ~= 1 then
|
2024-02-21 12:28:18 +03:00
|
|
|
goto continue
|
2024-04-19 08:45:01 +03:00
|
|
|
elseif valid then
|
|
|
|
j, updates[urls[i]] = j + 1, valid
|
2024-01-01 13:28:38 +03:00
|
|
|
flush(false)
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
2024-01-01 13:28:38 +03:00
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
i = i + 1
|
2024-01-01 13:28:38 +03:00
|
|
|
::continue::
|
|
|
|
until i > #urls
|
2023-12-26 14:48:33 +03:00
|
|
|
|
2024-01-01 13:28:38 +03:00
|
|
|
flush(true)
|
|
|
|
return j == #urls and 3 or 2
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|