2022-08-21 19:34:24 +02:00
|
|
|
local args=...
|
2022-08-24 13:34:57 +02:00
|
|
|
|
|
|
|
--LOG_ROOT = ulib.getenv("HOME")
|
|
|
|
|
2022-08-21 19:34:24 +02:00
|
|
|
if not args then
|
|
|
|
args = REQUEST
|
|
|
|
end
|
|
|
|
local vfs = require("vfs")
|
|
|
|
local DLCMD="wget --no-check-certificate -O"
|
|
|
|
local handle = {}
|
2022-08-24 13:34:57 +02:00
|
|
|
--local logger = Logger:new{ levels = {INFO = true, ERROR = true, DEBUG = false}}
|
2022-08-21 19:34:24 +02:00
|
|
|
local result = function(data)
|
|
|
|
return { error = false, result = data }
|
|
|
|
end
|
|
|
|
|
|
|
|
local error = function(msg)
|
|
|
|
return {error = msg, result = false}
|
|
|
|
end
|
|
|
|
|
|
|
|
handle.token = function(data)
|
|
|
|
local file = vfs.ospath(data.file)
|
|
|
|
local stat = ulib.file_stat(file)
|
|
|
|
local ret = {
|
|
|
|
sid = "access_token="..SESSION.sessionid,
|
|
|
|
key = std.sha1(file..":"..stat.mtime)
|
|
|
|
}
|
|
|
|
return result(ret)
|
|
|
|
end
|
|
|
|
|
2022-08-24 13:34:57 +02:00
|
|
|
handle.duplicate = function(data)
|
|
|
|
if not data.src or not data.dest then
|
|
|
|
return error("Unknow source or destination file")
|
|
|
|
end
|
|
|
|
local real_src = vfs.ospath(data.src)
|
|
|
|
local real_dest = vfs.ospath(data.dest)
|
|
|
|
if not ulib.exists(real_src) then
|
|
|
|
return error("Source file doesnt exist")
|
|
|
|
end
|
|
|
|
if not ulib.send_file(real_src, real_dest) then
|
|
|
|
return error("Unable to duplicate file")
|
|
|
|
end
|
|
|
|
return result(true)
|
|
|
|
end
|
|
|
|
|
2022-08-21 19:34:24 +02:00
|
|
|
handle.discover = function(data)
|
|
|
|
local tmpfile = "/tmp/libreoffice_discover.xml"
|
|
|
|
local cmd = DLCMD.." "..tmpfile..' '..data.uri
|
|
|
|
os.execute(cmd)
|
|
|
|
-- move file to correct position
|
|
|
|
if ulib.exists(tmpfile) then
|
|
|
|
local f = assert(io.open(tmpfile, "rb"))
|
|
|
|
local content = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
return result(content)
|
|
|
|
else
|
|
|
|
return error("Unable to discover data")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
handle.file = function(data)
|
|
|
|
local rq = REQUEST.r
|
|
|
|
if not rq then
|
|
|
|
return error("Unknown request")
|
|
|
|
end
|
|
|
|
local ret, stat = vfs.fileinfo(data.file)
|
|
|
|
if not ret then
|
|
|
|
return error("Unable to query file info")
|
|
|
|
end
|
|
|
|
local path = vfs.ospath(data.file)
|
|
|
|
if rq:match("/wopi/files/[^/]*/contents$") then
|
|
|
|
if REQUEST.method == "GET" then
|
|
|
|
std.sendFile(path)
|
|
|
|
return nil
|
|
|
|
elseif REQUEST.method == "POST" then
|
2022-09-15 11:58:52 +02:00
|
|
|
--local clen = tonumber(HEADER['Content-Length'])
|
|
|
|
local barr = REQUEST["application/octet-stream"]
|
2022-08-21 19:34:24 +02:00
|
|
|
bytes.write(barr, path)
|
|
|
|
return result(true)
|
|
|
|
else
|
|
|
|
return error("Unknown request method")
|
|
|
|
end
|
|
|
|
elseif rq:match("/wopi/files/[^/]*$") then
|
|
|
|
return {
|
|
|
|
BaseFileName = stat.name,
|
|
|
|
Size = math.floor(stat.size),
|
|
|
|
UserCanWrite = vfs.checkperm(data.file,"write"),
|
|
|
|
mime = stat.mime,
|
2022-08-24 13:34:57 +02:00
|
|
|
PostMessageOrigin = "*",
|
|
|
|
UserCanNotWriteRelative = false
|
2022-08-21 19:34:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return error("Unknown request")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2022-08-24 13:34:57 +02:00
|
|
|
--logger:info(JSON.encode(REQUEST))
|
|
|
|
|
2022-08-21 19:34:24 +02:00
|
|
|
if args.action and handle[args.action] then
|
|
|
|
return handle[args.action](args.args)
|
|
|
|
else
|
|
|
|
return error("Invalid action parameter")
|
|
|
|
end
|