2020-11-20 21:05:29 +01:00
|
|
|
local arg = ...
|
|
|
|
|
|
|
|
ulib = require("ulib")
|
|
|
|
vfs = require("vfs")
|
|
|
|
|
|
|
|
local handle = {}
|
|
|
|
local docpath = nil
|
|
|
|
|
|
|
|
local result = function(data)
|
|
|
|
return {
|
|
|
|
error = false,
|
|
|
|
result = data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local error = function(data)
|
|
|
|
return {
|
|
|
|
error = data,
|
|
|
|
result = false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local mkdirp =function(p)
|
|
|
|
if not vfs.exists(p) then
|
|
|
|
if not vfs.mkdir(p) then
|
|
|
|
return false, error("Unable to create directory: "..p)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true, nil
|
|
|
|
end
|
|
|
|
|
2023-03-27 20:38:17 +02:00
|
|
|
handle.merge_files = function(data)
|
2020-11-22 22:00:55 +01:00
|
|
|
local firstfile = data.file[1]
|
|
|
|
local fpath = docpath.."/"..data.cid
|
|
|
|
local r, e = mkdirp(fpath)
|
|
|
|
if not r then return e end
|
|
|
|
fpath = fpath.."/"..os.date("%d-%m-%Y_%H_%M_%S")..".pdf"
|
|
|
|
-- concat the files
|
|
|
|
if #data.file > 1 then
|
|
|
|
local cmd = "gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="..vfs.ospath(fpath)
|
|
|
|
for i,v in ipairs(data.file) do
|
2020-11-24 21:07:46 +01:00
|
|
|
cmd = cmd.." \""..vfs.ospath(v).."\""
|
2020-11-22 22:00:55 +01:00
|
|
|
end
|
|
|
|
os.execute(cmd)
|
|
|
|
if not vfs.exists(fpath) then
|
|
|
|
return error("Unable to merge PDF files")
|
|
|
|
end
|
|
|
|
cmd = "chmod 777 "..vfs.ospath(fpath)
|
|
|
|
os.execute(cmd)
|
|
|
|
else
|
2020-12-05 12:09:52 +01:00
|
|
|
local cmd = "mv \""..vfs.ospath(firstfile).."\" \""..vfs.ospath(fpath).."\""
|
|
|
|
os.execute(cmd)
|
|
|
|
if not vfs.exists(fpath) then
|
|
|
|
return error("Unable to move PDF file")
|
2020-11-22 22:00:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- move the thumb file to the cache folder
|
2023-03-27 20:38:17 +02:00
|
|
|
local thumb = docpath.."/cache/"..enc.sha1(firstfile:gsub(docpath, ""))..".png"
|
|
|
|
local desthumb = docpath.."/cache/"..enc.sha1(fpath:gsub(docpath, ""))..".png"
|
2020-11-22 22:00:55 +01:00
|
|
|
if vfs.exists(thumb) then
|
|
|
|
vfs.move(thumb, desthumb)
|
|
|
|
end
|
|
|
|
-- remove all other thumb files
|
|
|
|
for i,v in ipairs(data.file) do
|
2023-03-27 20:38:17 +02:00
|
|
|
thumb = docpath.."/cache/"..enc.sha1(v:gsub(docpath, ""))..".png"
|
2020-11-22 22:00:55 +01:00
|
|
|
if vfs.exists(thumb) then
|
|
|
|
vfs.delete(thumb)
|
|
|
|
end
|
|
|
|
-- delete all files
|
|
|
|
if vfs.exists(v) then
|
|
|
|
vfs.delete(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result(fpath)
|
|
|
|
end
|
|
|
|
|
2020-11-20 21:05:29 +01:00
|
|
|
handle.preview = function(path)
|
|
|
|
-- convert -resize 300x500 noel.pdf[0] thumb.png
|
2023-03-27 20:38:17 +02:00
|
|
|
local name = enc.sha1(path:gsub(docpath,""))..".png"
|
2020-11-20 21:05:29 +01:00
|
|
|
-- try to find the thumb
|
|
|
|
local tpath = docpath.."/cache/"..name
|
|
|
|
if not vfs.exists(tpath) then
|
|
|
|
-- regenerate thumb
|
2020-11-24 21:07:46 +01:00
|
|
|
local cmd = "convert -resize 250x500 \""..vfs.ospath(path).."\"[0] "..vfs.ospath(tpath)
|
2023-03-27 20:38:17 +02:00
|
|
|
LOG_ERROR(cmd)
|
2020-11-20 21:05:29 +01:00
|
|
|
os.execute(cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
if vfs.exists(tpath) then
|
2020-11-22 22:00:55 +01:00
|
|
|
local cmd = "chmod 777 "..vfs.ospath(tpath)
|
2020-11-20 21:05:29 +01:00
|
|
|
os.execute(cmd)
|
2020-11-22 22:00:55 +01:00
|
|
|
return result(tpath)
|
2020-11-20 21:05:29 +01:00
|
|
|
else
|
|
|
|
return error("do not exist")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-22 22:00:55 +01:00
|
|
|
handle.deletedoc = function(param)
|
|
|
|
-- move file to unclassified
|
2023-03-27 20:38:17 +02:00
|
|
|
local newfile = docpath.."/unclassified/"..utils.basename(param.file)
|
2020-11-22 22:00:55 +01:00
|
|
|
vfs.move(param.file, newfile)
|
|
|
|
-- delete thumb file
|
2023-03-27 20:38:17 +02:00
|
|
|
local thumb = docpath.."/cache/"..enc.sha1(param.file:gsub(docpath,""))..".png"
|
2020-11-22 22:00:55 +01:00
|
|
|
if vfs.exists(thumb) then
|
|
|
|
vfs.delete(thumb)
|
|
|
|
end
|
|
|
|
return result("Document entry deleted")
|
|
|
|
end
|
|
|
|
|
|
|
|
handle.updatedoc = function(param)
|
2023-03-27 20:38:17 +02:00
|
|
|
local r = handle.merge_files(param.data)
|
2020-11-22 22:00:55 +01:00
|
|
|
if r.error then return r end
|
|
|
|
|
|
|
|
if param.rm then
|
|
|
|
-- move ve the old file to unclassified
|
2023-03-27 20:38:17 +02:00
|
|
|
local newfile = docpath.."/unclassified/"..utils.basename(param.rm)
|
2020-12-05 20:30:15 +01:00
|
|
|
local cmd = "rm -f "..vfs.ospath(param.rm)
|
|
|
|
os.execute(cmd)
|
|
|
|
--if vfs.exists(param.rm) then
|
|
|
|
-- vfs.move(param.rm, newfile)
|
|
|
|
--end
|
2020-11-22 22:00:55 +01:00
|
|
|
-- move the thumb file if needed
|
2023-03-27 20:38:17 +02:00
|
|
|
local thumb = docpath.."/cache/"..enc.sha1(param.rm:gsub(docpath,""))..".png"
|
|
|
|
local newwthumb = docpath.."/cache/"..enc.sha1(newfile:gsub(docpath, ""))..".png"
|
2020-11-22 22:00:55 +01:00
|
|
|
if vfs.exists(thumb) then
|
|
|
|
vfs.move(thumb, newwthumb)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
param.data.file = r.result
|
|
|
|
print(r.result)
|
|
|
|
param.data.mtime = os.time(os.date("!*t"))
|
2023-03-27 20:38:17 +02:00
|
|
|
return result(param.data)
|
|
|
|
--return handle.update({
|
|
|
|
-- table = "docs",
|
|
|
|
-- data = param.data
|
|
|
|
--})
|
2020-11-22 22:00:55 +01:00
|
|
|
end
|
|
|
|
|
2020-11-20 21:05:29 +01:00
|
|
|
if arg.action and handle[arg.action] then
|
|
|
|
-- check if the database exits
|
|
|
|
docpath = arg.docpath
|
|
|
|
|
|
|
|
return handle[arg.action](arg.args)
|
|
|
|
else
|
|
|
|
return error("Invalid action parameter")
|
|
|
|
end
|