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
|
|
|
|
c = Util.httpGet(node.url)
|
|
|
|
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
|