2018-09-12 18:31:25 +02:00
|
|
|
local vfs = {}
|
|
|
|
|
|
|
|
|
|
|
|
vfs.ospath = function(path)
|
|
|
|
local user = SESSION.user
|
2022-08-21 16:26:57 +02:00
|
|
|
local prefix = string.match(path, "%a+:/")
|
2018-09-12 18:31:25 +02:00
|
|
|
if(prefix ~= nil) then
|
|
|
|
local suffix = string.gsub(path,prefix,"")
|
2022-08-21 16:26:57 +02:00
|
|
|
if prefix == "home:/" then
|
2018-09-12 18:31:25 +02:00
|
|
|
return string.format(VFS_HOME,user)..'/'..suffix
|
2022-08-21 16:26:57 +02:00
|
|
|
elseif prefix == "desktop:/" then
|
2018-09-12 18:31:25 +02:00
|
|
|
return string.format(VFS_HOME,user).."/.desktop/"..suffix
|
2022-08-21 16:26:57 +02:00
|
|
|
elseif prefix == "shared:/" then
|
2018-09-12 18:31:25 +02:00
|
|
|
return require("shared").ospath(std.trim(suffix,"/"))
|
2022-08-21 16:26:57 +02:00
|
|
|
elseif prefix == "os:/" then
|
2018-09-12 18:31:25 +02:00
|
|
|
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)
|
2020-06-05 19:55:49 +02:00
|
|
|
local folder = string.gsub(path, utils.escape_pattern(file).."$","")
|
2018-09-12 18:31:25 +02:00
|
|
|
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
|
2021-02-11 13:46:33 +01:00
|
|
|
local header = string.match(data, "^data%:[%w%.-%+]+%/[%w%.-%+]+;base64,")
|
2018-09-12 18:31:25 +02:00
|
|
|
if header ~= nil then
|
2020-06-06 14:15:25 +02:00
|
|
|
local b64data = string.gsub(data, utils.escape_pattern(header),"")
|
2018-09-12 18:31:25 +02:00
|
|
|
local barr = std.b64decode(b64data)
|
2019-12-15 12:13:04 +01:00
|
|
|
bytes.write(barr,osfile)
|
|
|
|
--[[ if std.isBinary(osfile) then
|
|
|
|
|
2018-09-12 18:31:25 +02:00
|
|
|
else
|
|
|
|
local f = io.open(osfile, "w")
|
|
|
|
f:write(bytes.__tostring(barr))
|
|
|
|
f:close()
|
2019-12-15 12:13:04 +01:00
|
|
|
end ]]
|
2018-09-15 21:35:07 +02:00
|
|
|
else
|
|
|
|
return false, "Wrong data format"
|
2018-09-12 18:31:25 +02:00
|
|
|
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)
|
2021-11-21 19:13:06 +01:00
|
|
|
if(not path) then
|
|
|
|
return false, "Unknown upload destination, abort!"
|
|
|
|
end
|
2018-09-12 18:31:25 +02:00
|
|
|
local r,m = vfs.checkperm(path,"write")
|
|
|
|
if(r) then
|
|
|
|
local uid = ulib.uid(SESSION.user)
|
2021-11-21 19:13:06 +01:00
|
|
|
local index = 0
|
|
|
|
while(REQUEST["upload-"..index..".tmp"] ~= nil) do
|
|
|
|
local file = m.."/"..REQUEST["upload-"..index..".file"]
|
2022-08-22 23:04:44 +02:00
|
|
|
local ret = ulib.move(REQUEST["upload-"..index..".tmp"], file)
|
|
|
|
if not ret then
|
2022-08-23 01:20:05 +02:00
|
|
|
ret = ulib.send_file(REQUEST["upload-"..index..".tmp"], file)
|
2022-08-22 23:04:44 +02:00
|
|
|
end
|
|
|
|
if not ret then
|
|
|
|
return false, "Unable to copy file"
|
|
|
|
end
|
2021-11-21 19:13:06 +01:00
|
|
|
ulib.chown(file, uid.id, uid.gid)
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
if(index == 0) then
|
|
|
|
return false, "No file is uploaded"
|
|
|
|
end
|
|
|
|
return true, index
|
2018-09-12 18:31:25 +02:00
|
|
|
else
|
|
|
|
return r,m
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
vfs.checkperm = function(path, right)
|
|
|
|
local osfile = vfs.ospath(path)
|
|
|
|
local perm = vfs.perm(osfile)
|
2021-02-07 11:35:26 +01:00
|
|
|
--print(osfile)
|
2018-09-12 18:31:25 +02:00
|
|
|
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
|
2021-02-07 11:35:26 +01:00
|
|
|
--print("Permission granted")
|
2018-09-12 18:31:25 +02:00
|
|
|
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
|
2021-02-07 11:35:26 +01:00
|
|
|
--print("file belong to user")
|
2018-09-12 18:31:25 +02:00
|
|
|
return st.perm.owner
|
|
|
|
elseif uid.groups and uid.groups[st.gid] then
|
2021-02-07 11:35:26 +01:00
|
|
|
--print("User belong to this group")
|
2018-09-12 18:31:25 +02:00
|
|
|
return st.perm.group
|
|
|
|
else
|
2021-02-07 11:35:26 +01:00
|
|
|
--print("User belong to other")
|
2018-09-12 18:31:25 +02:00
|
|
|
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
|