修复2进制文件dfpwm被视为文本传输的问题
This commit is contained in:
105
Client/main.lua
105
Client/main.lua
@@ -21,6 +21,84 @@ local function log(msg)
|
|||||||
--basalt.debug("[FileClient] " .. tostring(msg))
|
--basalt.debug("[FileClient] " .. tostring(msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- 改进的二进制检测函数,增加对DFPWM等特定格式的支持
|
||||||
|
local function isBinaryFile(path)
|
||||||
|
local extension = string.lower(string.match(path, "%.([^%.%s]+)$") or "")
|
||||||
|
local binaryExtensions = {
|
||||||
|
["wav"] = true,
|
||||||
|
["mp3"] = true,
|
||||||
|
["ogg"] = true,
|
||||||
|
["flac"] = true,
|
||||||
|
["dfpwm"] = true,
|
||||||
|
["png"] = true,
|
||||||
|
["jpg"] = true,
|
||||||
|
["jpeg"] = true,
|
||||||
|
["gif"] = true,
|
||||||
|
["bmp"] = true,
|
||||||
|
["ico"] = true,
|
||||||
|
["exe"] = true,
|
||||||
|
["dll"] = true,
|
||||||
|
["so"] = true,
|
||||||
|
["bin"] = true,
|
||||||
|
["dat"] = true,
|
||||||
|
["zip"] = true,
|
||||||
|
["rar"] = true,
|
||||||
|
["tar"] = true,
|
||||||
|
["gz"] = true,
|
||||||
|
["pdf"] = true,
|
||||||
|
["doc"] = true,
|
||||||
|
["docx"] = true,
|
||||||
|
["xls"] = true,
|
||||||
|
["xlsx"] = true,
|
||||||
|
["ppt"] = true,
|
||||||
|
["pptx"] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if binaryExtensions[extension] then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 对于没有扩展名的文件,检查内容
|
||||||
|
local absPath = path
|
||||||
|
if not fs.exists(absPath) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, handle = pcall(fs.open, absPath, "rb")
|
||||||
|
if not ok or not handle then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local data = handle.read(math.min(1024, fs.getSize(absPath)))
|
||||||
|
handle.close()
|
||||||
|
|
||||||
|
if not data then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 检查是否存在控制字符(除常见的空白字符外)
|
||||||
|
for i = 1, #data do
|
||||||
|
local b = data:byte(i)
|
||||||
|
-- 控制字符范围是 0-8, 11-12, 14-31, 127
|
||||||
|
if (b >= 0 and b <= 8) or (b == 11) or (b == 12) or (b >= 14 and b <= 31) or (b == 127) then
|
||||||
|
-- 如果控制字符过多(超过5%),则认为是二进制文件
|
||||||
|
local controlCount = 0
|
||||||
|
for j = 1, #data do
|
||||||
|
local byte = data:byte(j)
|
||||||
|
if (byte >= 0 and byte <= 8) or (byte == 11) or (byte == 12) or (byte >= 14 and byte <= 31) or (byte == 127) then
|
||||||
|
controlCount = controlCount + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if controlCount / #data > 0.05 then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
function table_to_json(t, indent)
|
function table_to_json(t, indent)
|
||||||
indent = indent or 0
|
indent = indent or 0
|
||||||
local spaces = string.rep(" ", indent)
|
local spaces = string.rep(" ", indent)
|
||||||
@@ -141,16 +219,6 @@ local function httpPost(path, data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- ========== 文件系统操作 ==========
|
-- ========== 文件系统操作 ==========
|
||||||
local function isLikelyText(data)
|
|
||||||
for i = 1, math.min(#data, 1024) do
|
|
||||||
local b = data:byte(i)
|
|
||||||
if b < 32 and b ~= 9 and b ~= 10 and b ~= 13 then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getFiles(currentPath, result, prefix)
|
local function getFiles(currentPath, result, prefix)
|
||||||
local computerPrefix = "computer_" .. computerID
|
local computerPrefix = "computer_" .. computerID
|
||||||
local fullPrefix = currentPath == "" and prefix:sub(1, -2) or prefix .. currentPath
|
local fullPrefix = currentPath == "" and prefix:sub(1, -2) or prefix .. currentPath
|
||||||
@@ -165,15 +233,16 @@ local function getFiles(currentPath, result, prefix)
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
local content = "[binary]"
|
local content = "[binary]"
|
||||||
local ok, handle = pcall(fs.open, absPath, "rb")
|
if not isBinaryFile(absPath) then
|
||||||
if ok and handle then
|
local ok, handle = pcall(fs.open, absPath, "r")
|
||||||
local data = handle.readAll()
|
if ok and handle then
|
||||||
handle.close()
|
local data = handle.readAll()
|
||||||
if data and isLikelyText(data) then
|
handle.close()
|
||||||
content = data
|
content = data or ""
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result[fullPrefix] = { isFile = true, content = content }
|
|
||||||
|
result[fullPrefix] = { isFile = true, content = content, isBinary = isBinaryFile(absPath) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -399,4 +468,4 @@ local function main()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- 启动主逻辑和Basalt事件循环
|
-- 启动主逻辑和Basalt事件循环
|
||||||
parallel.waitForAll(basalt.autoUpdate, main)
|
parallel.waitForAll(basalt.autoUpdate, main)
|
||||||
Reference in New Issue
Block a user