mirror of
https://github.com/lxsang/antd-lua-plugin
synced 2025-07-13 05:34:25 +02:00
mimgrating from another repo
This commit is contained in:
15
example-app/os/fs/delete.lua
Normal file
15
example-app/os/fs/delete.lua
Normal file
@ -0,0 +1,15 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
|
||||
local vfs = require("fs.vfs")
|
||||
local rq = (JSON.decodeString(REQUEST.query.json))
|
||||
|
||||
if rq ~= nil then
|
||||
local r,e = vfs.delete(rq.path)
|
||||
if r then
|
||||
result(r)
|
||||
else
|
||||
fail(e)
|
||||
end
|
||||
else
|
||||
fail("Uknown request")
|
||||
end
|
10
example-app/os/fs/exists.lua
Normal file
10
example-app/os/fs/exists.lua
Normal file
@ -0,0 +1,10 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
|
||||
local vfs = require("fs.vfs")
|
||||
local rq = (JSON.decodeString(REQUEST.query.json))
|
||||
|
||||
if rq ~= nil then
|
||||
result(vfs.exists(rq.path))
|
||||
else
|
||||
fail("Uknown request")
|
||||
end
|
8
example-app/os/fs/fileinfo.lua
Normal file
8
example-app/os/fs/fileinfo.lua
Normal file
@ -0,0 +1,8 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
local vfspath = (JSON.decodeString(REQUEST.query.json)).path
|
||||
local r,m = require("fs.vfs").fileinfo(vfspath)
|
||||
if r then
|
||||
result(m)
|
||||
else
|
||||
fail(m)
|
||||
end
|
6
example-app/os/fs/fsconf.lua.tpl
Normal file
6
example-app/os/fs/fsconf.lua.tpl
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
local conf = {
|
||||
home="/Users/%s/tmp/",
|
||||
shared="/Users/%s/tmp/Public/"
|
||||
}
|
||||
return conf
|
14
example-app/os/fs/fsh.lua
Normal file
14
example-app/os/fs/fsh.lua
Normal file
@ -0,0 +1,14 @@
|
||||
local handler
|
||||
|
||||
handler = function(str)
|
||||
local func = str:match("^%a+/")
|
||||
if func == "get/" then
|
||||
require("fs.get")(str:gsub(func,""))
|
||||
elseif func == "shared/" then
|
||||
require("fs.shared").get(str:gsub(func,""))
|
||||
else
|
||||
fail("Action is not supported: "..func)
|
||||
end
|
||||
end
|
||||
|
||||
return handler
|
40
example-app/os/fs/get.lua
Normal file
40
example-app/os/fs/get.lua
Normal file
@ -0,0 +1,40 @@
|
||||
local get
|
||||
get = function(uri)
|
||||
vfsfile = utils.decodeURI(uri)
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
local r,m = require("fs.vfs").checkperm(vfsfile,'read')
|
||||
if r then
|
||||
local mime = std.mimeOf(m)
|
||||
if mime == "audio/mpeg" then
|
||||
local finfo = unix.file_stat(m)
|
||||
local len = tostring(math.floor(finfo.size))
|
||||
local len1 = tostring(math.floor(finfo.size-1))
|
||||
std.status(200, "OK")
|
||||
std.custom_header("Pragma","public")
|
||||
std.custom_header("Expires","0")
|
||||
std.custom_header("Content-Type",mime)
|
||||
std.custom_header("Content-Length",len)
|
||||
std.custom_header("Content-Disposition","inline; filename="..std.basename(m))
|
||||
std.custom_header("Content-Range:","bytes 0-"..len1.."/"..len)
|
||||
std.custom_header("Accept-Ranges","bytes")
|
||||
std.custom_header("X-Pad", "avoid browser bug")
|
||||
std.custom_header("Content-Transfer-Encoding", "binary")
|
||||
std.custom_header("Cache-Control","no-cache, no-store")
|
||||
std.custom_header("Connection", "Keep-Alive")
|
||||
std.custom_header("Etag","a404b-c3f-47c3a14937c80")
|
||||
std.header_flush()
|
||||
else
|
||||
std.header(mime)
|
||||
end
|
||||
|
||||
if std.is_bin(m) then
|
||||
std.fb(m)
|
||||
else
|
||||
std.f(m)
|
||||
end
|
||||
else
|
||||
fail(m)
|
||||
end
|
||||
end
|
||||
|
||||
return get;
|
11
example-app/os/fs/mkdir.lua
Normal file
11
example-app/os/fs/mkdir.lua
Normal file
@ -0,0 +1,11 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
|
||||
local rq = (JSON.decodeString(REQUEST.query.json))
|
||||
|
||||
if rq ~= nil then
|
||||
local r,m = require("fs.vfs").mkdir(rq.path)
|
||||
if r then result(r) else fail(m) end
|
||||
else
|
||||
fail("Uknown request")
|
||||
end
|
||||
|
13
example-app/os/fs/move.lua
Normal file
13
example-app/os/fs/move.lua
Normal file
@ -0,0 +1,13 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
|
||||
local rq = (JSON.decodeString(REQUEST.query.json))
|
||||
|
||||
if rq ~= nil then
|
||||
local r,m = require("fs.vfs").move(rq.src,rq.dest)
|
||||
if r then result(r) else fail(m) end
|
||||
else
|
||||
fail("Uknown request")
|
||||
end
|
||||
|
||||
|
||||
|
36
example-app/os/fs/publish.lua
Normal file
36
example-app/os/fs/publish.lua
Normal file
@ -0,0 +1,36 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
|
||||
local rq = (JSON.decodeString(REQUEST.query.json))
|
||||
|
||||
if rq ~= nil then
|
||||
local p = nil
|
||||
if rq.publish then
|
||||
p = require("fs.vfs").ospath(rq.path)
|
||||
else
|
||||
p = require("fs.shared").ospath(rq.path)
|
||||
end
|
||||
local user = SESSION.iotos_user
|
||||
local uid = unix.uid(user)
|
||||
local st = unix.file_stat(p)
|
||||
if uid.id ~= st.uid then die("Only the owner can share or unshare this file") end
|
||||
local entry = { sid = std.sha1(p), user = SESSION.iotos_user, path = p, uid = uid.id }
|
||||
local db = require("db.model").get("sysdb", "shared", entry)
|
||||
if db == nil then die("Cannot get system database") end
|
||||
local cond = nil
|
||||
if rq.publish then
|
||||
cond = { exp = { ["="] = { path = p } } }
|
||||
local data = db:find(cond)
|
||||
if data == nil or data[0] == nil then
|
||||
-- insert entry
|
||||
db:insert(entry)
|
||||
end
|
||||
else
|
||||
cond = { ["="] = { sid = rq.path } }
|
||||
db:delete(cond)
|
||||
end
|
||||
db:close()
|
||||
result(entry.sid)
|
||||
|
||||
else
|
||||
fail("Uknown request")
|
||||
end
|
10
example-app/os/fs/scandir.lua
Normal file
10
example-app/os/fs/scandir.lua
Normal file
@ -0,0 +1,10 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
local vfspath = (JSON.decodeString(REQUEST.query.json)).path
|
||||
local r = require("fs.vfs").readDir(vfspath)
|
||||
if r == nil then
|
||||
fail("Resource not found")
|
||||
else
|
||||
--print(JSON.encode(readDir(ospath, vfspath)))
|
||||
result(r)
|
||||
end
|
||||
|
52
example-app/os/fs/shared.lua
Normal file
52
example-app/os/fs/shared.lua
Normal file
@ -0,0 +1,52 @@
|
||||
local shared = {}
|
||||
shared.get = function(sharedid)
|
||||
if sharedid == "all" then
|
||||
-- get all shared files
|
||||
local db = require("db.model").get("sysdb", "shared", nil)
|
||||
if db == nil then die("Cannot get shared database") end
|
||||
local data = db:getAll()
|
||||
if data == nil then die("No file found") end
|
||||
local i = 1
|
||||
local ret = {}
|
||||
for k,v in pairs(data) do
|
||||
if(unix.exists(v.path)) then
|
||||
local r = unix.file_stat(v.path)
|
||||
if(r.error == nil) then
|
||||
r.path = "shared://"..v.sid
|
||||
r.filename = std.basename(v.path)
|
||||
if r.mime == "application/octet-stream" then
|
||||
r.mime = std.extra_mime(r.filename)
|
||||
end
|
||||
ret[i] = r
|
||||
i = i+1
|
||||
end
|
||||
else
|
||||
local cond = { ["="] = { sid = v.sid } }
|
||||
db:delete(cond)
|
||||
end
|
||||
end
|
||||
db:close()
|
||||
--std.json()
|
||||
result(ret)
|
||||
else
|
||||
|
||||
local p = shared.ospath(sharedid)
|
||||
std.header(std.mimeOf(p))
|
||||
if std.is_bin(p) then
|
||||
std.fb(p)
|
||||
else
|
||||
std.f(p)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared.ospath = function(sharedid)
|
||||
local db = require("db.model").get("sysdb", "shared", nil)
|
||||
if db == nil then die("Cannot get shared database") end
|
||||
local cond = { exp = { ["="] = { sid = sharedid } } }
|
||||
local data = db:find(cond)
|
||||
db:close()
|
||||
if data == nil or data[1] == nil then die("Cannot get shared file with: "..sharedid) end
|
||||
return data[1].path
|
||||
end
|
||||
return shared;
|
8
example-app/os/fs/upload.lua
Normal file
8
example-app/os/fs/upload.lua
Normal file
@ -0,0 +1,8 @@
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
local vfs = require("fs.vfs")
|
||||
if REQUEST.query then
|
||||
local r,m = require("fs.vfs").upload(REQUEST.query.path)
|
||||
if r then result(r) else fail(m) end
|
||||
else
|
||||
fail("Query not found")
|
||||
end
|
203
example-app/os/fs/vfs.lua
Normal file
203
example-app/os/fs/vfs.lua
Normal file
@ -0,0 +1,203 @@
|
||||
local vfs = {}
|
||||
|
||||
vfs.ospath = function(path)
|
||||
local user = SESSION.iotos_user
|
||||
local conf = require("fs.fsconf")
|
||||
local prefix = string.match(path, "%a+://")
|
||||
if(prefix ~= nil) then
|
||||
local suffix = string.gsub(path,prefix,"")
|
||||
if prefix == "home://" then
|
||||
return string.format(conf.home,user)..'/'..suffix
|
||||
elseif prefix == "desktop://" then
|
||||
return string.format(conf.home,user).."/.desktop/"..suffix
|
||||
elseif prefix == "shared://" then
|
||||
return require("fs.shared").ospath(std.trim(suffix,"/"))
|
||||
elseif prefix == "os://" then
|
||||
return OSROOT.."/"..suffix
|
||||
else
|
||||
return nil
|
||||
end
|
||||
else
|
||||
return nil;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
vfs.delete = function(path)
|
||||
local r,m = vfs.checkperm(path,"write")
|
||||
if r then
|
||||
if unix.delete(m) then
|
||||
-- change permission
|
||||
return true,nil
|
||||
else
|
||||
return false,"Cant not delete the file"
|
||||
end
|
||||
else
|
||||
return r,m
|
||||
end
|
||||
end
|
||||
|
||||
vfs.exists = function(path)
|
||||
local osfile = vfs.ospath(path)
|
||||
return unix.exists(osfile)
|
||||
end
|
||||
|
||||
vfs.fileinfo = function(vfspath)
|
||||
local ospath = vfs.ospath(vfspath)
|
||||
if ospath then
|
||||
if(unix.exists(ospath) == false) then return false,"File not found" end
|
||||
local r = unix.file_stat(ospath)
|
||||
if(r.error ~= nil) then return false,r.error end
|
||||
r.path = vfspath
|
||||
r.name = std.basename(vfspath)
|
||||
if r.mime == "application/octet-stream" then
|
||||
r.mime = std.extra_mime(r.name)
|
||||
end
|
||||
return true,r
|
||||
else
|
||||
return false,"Resource not found"
|
||||
end
|
||||
end
|
||||
|
||||
vfs.mkdir = function(path)
|
||||
local file = std.basename(path)
|
||||
local folder = string.gsub(path, utils.escape_pattern(file),"")
|
||||
local r,m = vfs.checkperm(folder,"write")
|
||||
|
||||
if r then
|
||||
local osfile = m.."/"..file
|
||||
local uid = unix.uid(SESSION.iotos_user)
|
||||
unix.mkdir(osfile)
|
||||
-- change permission
|
||||
unix.chown(osfile, uid.id, uid.gid)
|
||||
return true,nil
|
||||
else
|
||||
return r,m
|
||||
end
|
||||
end
|
||||
|
||||
vfs.move = function(src,dest)
|
||||
local file = std.basename(dest)
|
||||
local folder = string.gsub(dest, utils.escape_pattern(file),"")
|
||||
|
||||
local sp,sm = vfs.checkperm(src,"write")
|
||||
if sp then
|
||||
local dp,dm = vfs.checkperm(folder,"write")
|
||||
if dp then
|
||||
unix.move(sm,dm.."/"..file)
|
||||
-- change permission
|
||||
return true,nil
|
||||
else
|
||||
return dp,dm
|
||||
end
|
||||
else
|
||||
return sp,sm
|
||||
end
|
||||
end
|
||||
|
||||
vfs.write = function(path,data)
|
||||
local file = std.basename(path)
|
||||
local folder = string.gsub(path, utils.escape_pattern(file),"")
|
||||
|
||||
local r,m = vfs.checkperm(folder,"write")
|
||||
if r then
|
||||
local osfile = m.."/"..file
|
||||
local uid = unix.uid(SESSION.iotos_user)
|
||||
--
|
||||
if data ~= "" then
|
||||
local header = string.match(data, "^data%:%w+%/%w+;base64,")
|
||||
if header ~= nil then
|
||||
local b64data = string.gsub(data, header,"")
|
||||
local barr = std.b64decode(b64data)
|
||||
if std.isBinary(osfile) then
|
||||
bytes.write(barr,osfile)
|
||||
else
|
||||
local f = io.open(osfile, "w")
|
||||
f:write(bytes.__tostring(barr))
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
else
|
||||
bytes.write(bytes.new(0),osfile)
|
||||
end
|
||||
--f:close()
|
||||
-- change permission
|
||||
unix.chown(osfile, uid.id, uid.gid)
|
||||
return true,nil
|
||||
else
|
||||
return r,m
|
||||
end
|
||||
end
|
||||
|
||||
vfs.upload = function(path)
|
||||
local r,m = vfs.checkperm(path,"write")
|
||||
if(r) then
|
||||
local uid = unix.uid(SESSION.iotos_user)
|
||||
local file = m.."/"..REQUEST.query["upload.file"]
|
||||
unix.move(REQUEST.query["upload.tmp"], file)
|
||||
unix.chown(file, uid.id, uid.gid)
|
||||
return true, nil
|
||||
else
|
||||
return r,m
|
||||
end
|
||||
end
|
||||
|
||||
vfs.checkperm = function(path, right)
|
||||
local osfile = vfs.ospath(path)
|
||||
local perm = vfs.perm(osfile)
|
||||
print(osfile)
|
||||
-- check if user own the file
|
||||
if perm ~= nil then
|
||||
if perm[right] == true then
|
||||
print("Permission granted")
|
||||
return true,osfile
|
||||
else
|
||||
print("Permission denie")
|
||||
return false,"You dont have "..right.." permission on this file"
|
||||
end
|
||||
else
|
||||
return false,"User is unrecognized"
|
||||
end
|
||||
end
|
||||
|
||||
vfs.perm = function(file)
|
||||
local user = SESSION.iotos_user
|
||||
local uid = unix.uid(user)
|
||||
local st = unix.file_stat(file)
|
||||
-- check if user own the file
|
||||
if uid ~= nil and st ~= nil and st.perm ~= nil then
|
||||
--print(JSON.encode({uid, st}))
|
||||
if(uid.id == st.uid) then -- the user owned the file
|
||||
print("file belong to user")
|
||||
return st.perm.owner
|
||||
elseif uid.groups and uid.groups[st.gid] then
|
||||
print("User belong to this group")
|
||||
return st.perm.group
|
||||
else
|
||||
print("User belong to other")
|
||||
return st.perm.other
|
||||
end
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
vfs.readDir = function(vfspath)
|
||||
if(string.sub(vfspath,-1) == "/") then
|
||||
prefix = string.sub(vfspath,1,-2)
|
||||
else
|
||||
prefix = vfspath
|
||||
end
|
||||
local ospath = vfs.ospath(vfspath,SESSION.iotos_user)
|
||||
local r = unix.read_dir(ospath, prefix)
|
||||
if(r.error ~= nil) then return nil end
|
||||
-- add extra mime type
|
||||
for k,v in pairs(r) do
|
||||
if v.mime == "application/octet-stream" then
|
||||
v.mime = std.extra_mime(v.filename)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
return vfs
|
13
example-app/os/fs/write.lua
Normal file
13
example-app/os/fs/write.lua
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
auth_or_die("User unauthorized. Please login")
|
||||
local rq = (JSON.decodeString(REQUEST.query.json))
|
||||
|
||||
if rq ~= nil then
|
||||
local r,m = require("fs.vfs").write(rq.path, rq.data)
|
||||
sqlite.dbclose()
|
||||
if r then result(r) else fail(m) end
|
||||
else
|
||||
fail("Uknown request")
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user