2018-09-19 15:08:49 +02:00
|
|
|
function std.extra_mime(name)
|
2022-08-27 21:44:55 +02:00
|
|
|
local ext = std.ext(name)
|
2018-09-19 15:08:49 +02:00
|
|
|
local mpath = __ROOT__.."/".."mimes.json"
|
|
|
|
local xmimes = {}
|
|
|
|
if utils.file_exists(mpath) then
|
|
|
|
local f = io.open(mpath, "r")
|
|
|
|
if f then
|
|
|
|
xmimes = JSON.decodeString(f:read("*all"))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if(name:find("Makefile$")) then return "text/makefile",false
|
|
|
|
elseif ext == "php" then return "text/php",false
|
|
|
|
elseif ext == "c" or ext == "h" then return "text/c",false
|
|
|
|
elseif ext == "cpp" or ext == "hpp" then return "text/cpp",false
|
|
|
|
elseif ext == "md" then return "text/markdown",false
|
|
|
|
elseif ext == "lua" then return "text/lua",false
|
2022-08-27 21:44:55 +02:00
|
|
|
elseif ext == "yml" then return "application/x-yaml", false
|
2018-09-19 15:08:49 +02:00
|
|
|
elseif xmimes[ext] then return xmimes[ext].mime, xmimes[ext].binary
|
|
|
|
--elseif ext == "pgm" then return "image/x-portable-graymap", true
|
|
|
|
else
|
|
|
|
return "application/octet-stream",true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function std.mimeOf(name)
|
|
|
|
local mime = std.mime(name)
|
|
|
|
if mime ~= "application/octet-stream" then
|
|
|
|
return mime
|
|
|
|
else
|
|
|
|
return std.extra_mime(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 12:08:35 +01:00
|
|
|
--[[ function std.isBinary(name)
|
2018-09-19 15:08:49 +02:00
|
|
|
local mime = std.mime(name)
|
|
|
|
if mime ~= "application/octet-stream" then
|
|
|
|
return std.is_bin(name)
|
|
|
|
else
|
|
|
|
local xmime,bin = std.extra_mime(name)
|
|
|
|
return bin
|
|
|
|
end
|
2019-12-15 12:08:35 +01:00
|
|
|
end ]]
|
2019-12-04 19:52:26 +01:00
|
|
|
|
|
|
|
function std.sendFile(m)
|
|
|
|
local mime = std.mimeOf(m)
|
|
|
|
local finfo = ulib.file_stat(m)
|
|
|
|
local len = tostring(math.floor(finfo.size))
|
|
|
|
local len1 = tostring(math.floor(finfo.size - 1))
|
|
|
|
if mime == "audio/mpeg" then
|
2019-12-15 12:08:35 +01:00
|
|
|
std.status(200)
|
|
|
|
std.header("Pragma", "public")
|
|
|
|
std.header("Expires", "0")
|
|
|
|
std.header("Content-Type", mime)
|
|
|
|
std.header("Content-Length", len)
|
|
|
|
std.header("Content-Disposition", "inline; filename=" .. std.basename(m))
|
|
|
|
std.header("Content-Range:", "bytes 0-" .. len1 .. "/" .. len)
|
|
|
|
std.header("Accept-Ranges", "bytes")
|
|
|
|
std.header("X-Pad", "avoid browser bug")
|
|
|
|
std.header("Content-Transfer-Encoding", "binary")
|
|
|
|
std.header("Cache-Control", "no-cache, no-store")
|
|
|
|
std.header("Connection", "Keep-Alive")
|
2020-01-10 18:29:29 +01:00
|
|
|
std.header_flush()
|
|
|
|
std.f(m)
|
2019-12-04 19:52:26 +01:00
|
|
|
else
|
2020-01-10 18:29:29 +01:00
|
|
|
if HEADER['If-Modified-Since'] and HEADER['If-Modified-Since'] == finfo.ctime then
|
|
|
|
std.status(304)
|
|
|
|
std.header_flush()
|
|
|
|
else
|
|
|
|
std.status(200)
|
|
|
|
std.header("Content-Type", mime)
|
|
|
|
--std.header("Content-Length", len)
|
|
|
|
std.header("Cache-Control", "no-cache")
|
|
|
|
std.header("Last-Modified", finfo.ctime)
|
|
|
|
std.header_flush()
|
|
|
|
std.f(m)
|
|
|
|
end
|
2019-12-04 19:52:26 +01:00
|
|
|
end
|
|
|
|
end
|