mirror of
https://github.com/lxsang/antd-web-apps
synced 2025-07-26 02:29:47 +02:00
use silk framework for os
This commit is contained in:
46
os/libs/common.lua
Normal file
46
os/libs/common.lua
Normal file
@ -0,0 +1,46 @@
|
||||
require("sqlite")
|
||||
function fail(msg)
|
||||
std.json()
|
||||
std.t(JSON.encode({error=msg}))
|
||||
end
|
||||
|
||||
function result(obj)
|
||||
std.json()
|
||||
std.t(JSON.encode({result=obj, error=false}))
|
||||
end
|
||||
|
||||
function die (msg)
|
||||
fail(msg)
|
||||
debug.traceback=nil
|
||||
error("Permission denied")
|
||||
end
|
||||
|
||||
-- check if the sysdb is create, otherwise create the table
|
||||
function sysdb()
|
||||
local meta = {}
|
||||
meta.sessionid = ""
|
||||
meta.username = ""
|
||||
meta.stamp = 0
|
||||
return require("dbmodel").get("sysdb", "sessions", meta)
|
||||
end
|
||||
|
||||
function is_auth()
|
||||
if SESSION.sessionid == nil or SESSION.sessionid == '0' then return false end
|
||||
-- query session id from database
|
||||
local db = sysdb()
|
||||
if db == nil then return false end
|
||||
local cond = {exp= {["="] = { sessionid = SESSION.sessionid }}}
|
||||
local data = db:find(cond)
|
||||
--print(JSON.encode(data))
|
||||
db:close()
|
||||
if data == nil or data[1] == nil then die(msg) end
|
||||
-- next time check the stamp
|
||||
SESSION.user = data[1].username
|
||||
return true
|
||||
end
|
||||
|
||||
function auth_or_die(msg)
|
||||
if(is_auth() == false) then
|
||||
die(msg)
|
||||
end
|
||||
end
|
20
os/libs/dbmodel.lua
Normal file
20
os/libs/dbmodel.lua
Normal file
@ -0,0 +1,20 @@
|
||||
local model = {}
|
||||
|
||||
model.get = function(name, tbl, data)
|
||||
local db = DBModel:new{db = name, name=tbl}
|
||||
db:open()
|
||||
if db:available() then return db end
|
||||
if data == nil then return nil end
|
||||
local meta = {}
|
||||
--print(JSON.encode(data))
|
||||
for k,v in pairs(data) do
|
||||
if type(v) == "number" or type(v) == "boolean" then
|
||||
meta[k] = "NUMERIC"
|
||||
else
|
||||
meta[k] = "TEXT"
|
||||
end
|
||||
end
|
||||
db:createTable(meta)
|
||||
return db
|
||||
end
|
||||
return model
|
119
os/libs/packages.lua
Normal file
119
os/libs/packages.lua
Normal file
@ -0,0 +1,119 @@
|
||||
local packages={}
|
||||
local vfs = require("vfs")
|
||||
local uid = ulib.uid(SESSION.user)
|
||||
|
||||
packages._cache = function(y)
|
||||
local p = vfs.ospath(y)
|
||||
local f = io.open(p.."/packages.json", "w")
|
||||
local has_cache = false
|
||||
local i = 1
|
||||
local meta = {}
|
||||
if f then
|
||||
local files = vfs.readDir(y)
|
||||
for k,v in pairs(files) do
|
||||
if v.type == "dir" then
|
||||
local f1 = io.open(vfs.ospath(v.path.."/package.json"))
|
||||
if f1 then
|
||||
|
||||
local name = std.basename(v.path)
|
||||
local mt = JSON.decodeString(f1:read("*all"))
|
||||
mt.path = v.path
|
||||
meta[i] ='"'..name..'":'..JSON.encode(mt)
|
||||
i = i+1
|
||||
f1:close()
|
||||
has_cache = true;
|
||||
end
|
||||
end
|
||||
end
|
||||
f:write(table.concat(meta, ","))
|
||||
f:close()
|
||||
if has_cache == false then
|
||||
ulib.delete(p.."/packages.json");
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- we will change this later
|
||||
packages.list = function(paths)
|
||||
std.json()
|
||||
std.t("{\"result\" : { ")
|
||||
local first = true
|
||||
--std.f(__ROOT__.."/system/packages.json")
|
||||
for k,v in pairs(paths) do
|
||||
local osp = vfs.ospath(v.."/packages.json")
|
||||
if ulib.exists(osp) == false then
|
||||
packages._cache(v)
|
||||
end
|
||||
if ulib.exists(osp) then
|
||||
if first == false then
|
||||
std.t(",")
|
||||
else
|
||||
first = false
|
||||
end
|
||||
std.f(osp)
|
||||
end
|
||||
end
|
||||
std.t("}, \"error\":false}")
|
||||
end
|
||||
|
||||
-- generate the packages caches
|
||||
packages.cache = function(args)
|
||||
-- perform a packages caches
|
||||
for x,y in pairs(args.paths) do
|
||||
packages._cache(y)
|
||||
end
|
||||
result(true)
|
||||
end
|
||||
-- install a function from zip file
|
||||
packages.install = function(args)
|
||||
local path = vfs.ospath(args.dest)
|
||||
local zip = vfs.ospath(args.zip)
|
||||
if(ulib.exists(path) == false) then
|
||||
-- create directory if not exist
|
||||
ulib.mkdir(path)
|
||||
-- change permission
|
||||
ulib.chown(path, uid.id, uid.gid)
|
||||
end
|
||||
-- extract the zip file to it
|
||||
if(ulib.unzip(zip, path)) then
|
||||
-- read metadata
|
||||
local meta = JSON.decodeFile(path.."/metadata.json")
|
||||
meta.path = args.dest
|
||||
meta.scope = "user"
|
||||
local f=io.open(path.."/package.json","w")
|
||||
if f then
|
||||
f:write(JSON.encode(meta))
|
||||
f:close()
|
||||
end
|
||||
result(true)
|
||||
else
|
||||
fail("Problem extracting zip file")
|
||||
end
|
||||
|
||||
end
|
||||
-- uninstall the package
|
||||
packages.uninstall = function(path)
|
||||
local osf = vfs.ospath(path)
|
||||
if(osf and ulib.exists(osf) ) then
|
||||
--remove it
|
||||
ulib.delete(osf)
|
||||
result(true)
|
||||
else
|
||||
fail("Cannot find package")
|
||||
end
|
||||
end
|
||||
-- set user packages environment
|
||||
packages.init = function(paths)
|
||||
if(paths) then
|
||||
for k,v in pairs(paths) do
|
||||
local p = vfs.ospath(v)
|
||||
if p and (ulib.exists(p) == false) then
|
||||
ulib.mkdir(p)
|
||||
-- change permission
|
||||
ulib.chown(p, uid.id, uid.gid)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return packages
|
52
os/libs/shared.lua
Normal file
52
os/libs/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("dbmodel").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(ulib.exists(v.path)) then
|
||||
local r = ulib.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("dbmodel").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;
|
27
os/libs/uman.lua
Normal file
27
os/libs/uman.lua
Normal file
@ -0,0 +1,27 @@
|
||||
local uman={}
|
||||
|
||||
uman.userinfo = function(user)
|
||||
local info = {}
|
||||
local uid = ulib.uid(user)
|
||||
if uid then
|
||||
-- read the setting
|
||||
-- use the decodeFile function of JSON instead
|
||||
local file = require('vfs').ospath("home:///").."/.settings.json"
|
||||
local st = JSON.decodeFile(file)
|
||||
if(st) then
|
||||
info = st
|
||||
end
|
||||
info.user = {
|
||||
username = user,
|
||||
id = uid.id,
|
||||
name = user,
|
||||
groups = uid.groups
|
||||
}
|
||||
--print(JSON.encode(info))
|
||||
return info
|
||||
else
|
||||
return {}
|
||||
end
|
||||
end
|
||||
|
||||
return uman
|
214
os/libs/vfs.lua
Normal file
214
os/libs/vfs.lua
Normal file
@ -0,0 +1,214 @@
|
||||
local vfs = {}
|
||||
|
||||
|
||||
vfs.ospath = function(path)
|
||||
local user = SESSION.user
|
||||
local prefix = string.match(path, "%a+://")
|
||||
if(prefix ~= nil) then
|
||||
local suffix = string.gsub(path,prefix,"")
|
||||
if prefix == "home://" then
|
||||
return string.format(VFS_HOME,user)..'/'..suffix
|
||||
elseif prefix == "desktop://" then
|
||||
return string.format(VFS_HOME,user).."/.desktop/"..suffix
|
||||
elseif prefix == "shared://" then
|
||||
return require("shared").ospath(std.trim(suffix,"/"))
|
||||
elseif prefix == "os://" then
|
||||
return WWW_ROOT.."/"..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 ulib.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 ulib.exists(osfile)
|
||||
end
|
||||
|
||||
vfs.fileinfo = function(vfspath)
|
||||
local ospath = vfs.ospath(vfspath)
|
||||
if ospath then
|
||||
if(ulib.exists(ospath) == false) then return false,"File not found" end
|
||||
local r = ulib.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 = ulib.uid(SESSION.user)
|
||||
ulib.mkdir(osfile)
|
||||
-- change permission
|
||||
ulib.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
|
||||
ulib.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
|
||||
|
||||
if ulib.exists(osfile) then
|
||||
local r1,m1 = vfs.checkperm(path,"write")
|
||||
if not r1 then
|
||||
return r1, m1..": "..path
|
||||
end
|
||||
end
|
||||
|
||||
local uid = ulib.uid(SESSION.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
|
||||
ulib.chown(osfile, uid.id, uid.gid)
|
||||
return true,nil
|
||||
else
|
||||
return r,m..": "..folder
|
||||
end
|
||||
end
|
||||
|
||||
vfs.upload = function(path)
|
||||
local r,m = vfs.checkperm(path,"write")
|
||||
if(r) then
|
||||
local uid = ulib.uid(SESSION.user)
|
||||
local file = m.."/"..REQUEST.query["upload.file"]
|
||||
ulib.move(REQUEST.query["upload.tmp"], file)
|
||||
ulib.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)
|
||||
if not ulib.exists(osfile) then
|
||||
return false,"Resource does not exist"
|
||||
end
|
||||
-- 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.user
|
||||
local uid = ulib.uid(user)
|
||||
local st = ulib.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.user)
|
||||
local r = ulib.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
|
Reference in New Issue
Block a user