1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apis/fs/urlfs.lua

102 lines
1.4 KiB
Lua
Raw Normal View History

2018-03-30 17:12:46 +00:00
local rttp = require('rttp')
2017-10-09 00:03:01 +00:00
local Util = require('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 = { }
2017-10-08 21:45:01 +00:00
function urlfs.mount(_, url)
2018-01-24 22:39:38 +00:00
if not url then
error('URL is required')
end
return {
url = url,
}
2016-12-11 19:24:52 +00:00
end
2017-10-08 21:45:01 +00:00
function urlfs.delete(_, dir)
2018-01-24 22:39:38 +00:00
fs.unmount(dir)
2016-12-11 19:24:52 +00:00
end
function urlfs.exists()
2018-01-24 22:39:38 +00:00
return true
2016-12-11 19:24:52 +00:00
end
function urlfs.getSize(node)
2018-01-24 22:39:38 +00:00
return node.size or 0
2016-12-11 19:24:52 +00:00
end
function urlfs.isReadOnly()
2018-01-24 22:39:38 +00:00
return true
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
2018-03-30 21:01:56 +00:00
if node.url:match("^(rttps?:)") then
2018-03-30 17:12:46 +00:00
local s, response = rttp.get(node.url)
c = s and response.statusCode == 200 and response.data
else
c = Util.httpGet(node.url)
end
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 {
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 {
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