opus/sys/modules/opus/fs/urlfs.lua

120 lines
1.8 KiB
Lua
Raw Normal View History

2019-06-18 19:19:24 +00:00
--local rttp = require('rttp')
local Util = require('opus.util')
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local fs = _G.fs
2016-12-11 19:24:52 +00:00
local urlfs = { }
function urlfs.mount(path, url, force)
2018-01-24 22:39:38 +00:00
if not url then
error('URL is required')
end
-- only mount if the file does not exist already
if not fs.exists(path) or force then
return {
url = url,
created = os.epoch('utc'),
modification = os.epoch('utc'),
}
end
end
function urlfs.attributes(node, path)
return path == node.mountPoint and {
created = node.created,
isDir = false,
modification = node.modification,
size = node.size or 0,
2018-01-24 22:39:38 +00:00
}
2016-12-11 19:24:52 +00:00
end
function urlfs.delete(node, path)
if path == node.mountPoint then
fs.unmount(path)
end
2016-12-11 19:24:52 +00:00
end
function urlfs.exists(node, path)
return path == node.mountPoint
2016-12-11 19:24:52 +00:00
end
function urlfs.getSize(node, path)
return path == node.mountPoint and node.size or 0
2016-12-11 19:24:52 +00:00
end
function urlfs.isReadOnly()
return false
2016-12-11 19:24:52 +00:00
end
function urlfs.isDir()
2018-01-24 22:39:38 +00:00
return false
2016-12-11 19:24:52 +00:00
end
function urlfs.getDrive()
2018-01-24 22:39:38 +00:00
return 'url'
2016-12-11 19:24:52 +00:00
end
function urlfs.open(node, fn, fl)
2018-01-24 22:39:38 +00:00
if fl == 'w' or fl == 'wb' then
fs.delete(fn)
return fs.open(fn, fl)
end
if fl ~= 'r' and fl ~= 'rb' then
error('Unsupported mode')
end
local c = node.cache
if not c then
2019-06-18 19:19:24 +00:00
c = Util.httpGet(node.url)
2018-01-24 22:39:38 +00:00
if c then
node.cache = c
node.size = #c
end
end
if not c then
return
end
local ctr = 0
local lines
if fl == 'r' then
return {
read = function()
ctr = ctr + 1
return c:sub(ctr, ctr)
end,
2018-01-24 22:39:38 +00:00
readLine = function()
if not lines then
lines = Util.split(c)
end
ctr = ctr + 1
return lines[ctr]
end,
readAll = function()
return c
end,
close = function()
lines = nil
end,
}
end
return {
2020-06-06 03:38:26 +00:00
readAll = function()
return c
end,
2018-01-24 22:39:38 +00:00
read = function()
ctr = ctr + 1
return c:sub(ctr, ctr):byte()
end,
close = function()
ctr = 0
end,
}
2016-12-11 19:24:52 +00:00
end
return urlfs