2016-12-11 19:24:52 +00:00
|
|
|
local synchronized = require('sync')
|
2017-09-05 06:09:31 +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)
|
2016-12-11 19:24:52 +00:00
|
|
|
if not url then
|
|
|
|
error('URL is required')
|
|
|
|
end
|
|
|
|
return {
|
|
|
|
url = url,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
function urlfs.delete(_, dir)
|
2016-12-11 19:24:52 +00:00
|
|
|
fs.unmount(dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
function urlfs.exists()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function urlfs.getSize(node)
|
|
|
|
return node.size or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function urlfs.isReadOnly()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function urlfs.isDir()
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function urlfs.getDrive()
|
|
|
|
return 'url'
|
|
|
|
end
|
|
|
|
|
|
|
|
function urlfs.open(node, fn, fl)
|
|
|
|
|
2017-09-15 05:08:04 +00:00
|
|
|
if fl == 'w' or fl == 'wb' then
|
|
|
|
fs.delete(fn)
|
|
|
|
return fs.open(fn, fl)
|
|
|
|
end
|
|
|
|
|
2017-06-02 19:42:40 +00:00
|
|
|
if fl ~= 'r' and fl ~= 'rb' then
|
2016-12-11 19:24:52 +00:00
|
|
|
error('Unsupported mode')
|
|
|
|
end
|
|
|
|
|
|
|
|
local c = node.cache
|
|
|
|
if not c then
|
|
|
|
synchronized(node.url, function()
|
|
|
|
c = Util.download(node.url)
|
|
|
|
end)
|
2016-12-27 03:26:43 +00:00
|
|
|
if c then
|
2016-12-11 19:24:52 +00:00
|
|
|
node.cache = c
|
|
|
|
node.size = #c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 03:26:43 +00:00
|
|
|
if not c then
|
2016-12-11 19:24:52 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local ctr = 0
|
|
|
|
local lines
|
2017-06-02 19:42:40 +00:00
|
|
|
|
|
|
|
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
|
2016-12-11 19:24:52 +00:00
|
|
|
return {
|
2017-06-02 19:42:40 +00:00
|
|
|
read = function()
|
2016-12-11 19:24:52 +00:00
|
|
|
ctr = ctr + 1
|
2017-06-02 19:50:05 +00:00
|
|
|
return c:sub(ctr, ctr):byte()
|
2016-12-11 19:24:52 +00:00
|
|
|
end,
|
|
|
|
close = function()
|
2017-06-02 19:42:40 +00:00
|
|
|
ctr = 0
|
2016-12-11 19:24:52 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return urlfs
|