2023-12-26 14:48:33 +03:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M:preload()
|
2024-01-01 13:28:38 +03:00
|
|
|
local urls = {}
|
|
|
|
for _, file in ipairs(self.files) do
|
|
|
|
urls[#urls + 1] = tostring(file.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
local args
|
2023-12-26 14:48:33 +03:00
|
|
|
if ya.target_family() == "windows" then
|
2024-01-01 13:28:38 +03:00
|
|
|
args = { "-b", "--mime-type" }
|
2023-12-26 14:48:33 +03:00
|
|
|
else
|
2024-01-01 13:28:38 +03:00
|
|
|
args = { "-b", "-L", "--mime-type" }
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
|
|
|
|
2024-01-01 13:28:38 +03:00
|
|
|
local child, code = Command("file"):args(args):args(urls):stdout(Command.PIPED):spawn()
|
|
|
|
if not child then
|
|
|
|
ya.err("spawn `file` command returns " .. tostring(code))
|
|
|
|
return 0
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
|
|
|
|
2024-01-01 13:28:38 +03:00
|
|
|
local mimes, last = {}, ya.time()
|
|
|
|
local flush = function(force)
|
|
|
|
if not force and ya.time() - last < 0.1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if next(mimes) then
|
|
|
|
ya.manager_emit("update_mimetype", {}, mimes)
|
|
|
|
mimes, last = {}, ya.time()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local i, j = 1, 0
|
|
|
|
repeat
|
|
|
|
local next, event = child:read_line_with { timeout = 100 }
|
|
|
|
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
|
|
|
|
|
|
|
next = next:gsub("[\r\n]+$", "")
|
|
|
|
if ya.mime_valid(next) then
|
|
|
|
j, mimes[urls[i]] = j + 1, next
|
|
|
|
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
|