mirror of
https://github.com/kepler155c/opus
synced 2024-11-05 08:26:16 +00:00
79 lines
1.1 KiB
Lua
79 lines
1.1 KiB
Lua
local synchronized = require('sync')
|
|
|
|
local urlfs = { }
|
|
|
|
function urlfs.mount(dir, url)
|
|
if not url then
|
|
error('URL is required')
|
|
end
|
|
return {
|
|
url = url,
|
|
}
|
|
end
|
|
|
|
function urlfs.delete(node, dir)
|
|
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)
|
|
|
|
if fl ~= 'r' then
|
|
error('Unsupported mode')
|
|
end
|
|
|
|
local c = node.cache
|
|
if not c then
|
|
synchronized(node.url, function()
|
|
c = Util.download(node.url)
|
|
end)
|
|
if c then
|
|
node.cache = c
|
|
node.size = #c
|
|
end
|
|
end
|
|
|
|
if not c then
|
|
return
|
|
end
|
|
|
|
local ctr = 0
|
|
local lines
|
|
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 urlfs
|