update Docify

This commit is contained in:
lxsang
2020-11-22 21:00:55 +00:00
parent 685841635f
commit 6ee45214af
11 changed files with 655 additions and 506 deletions

View File

@ -31,6 +31,49 @@ local mkdirp =function(p)
return true, nil
end
local merge_files = function(data)
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
cmd = cmd.." "..vfs.ospath(v)
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
if not vfs.move(firstfile, fpath) then
return error("Unable to move file")
end
end
-- move the thumb file to the cache folder
local thumb = docpath.."/cache/"..std.sha1(firstfile:gsub(docpath, ""))..".png"
local desthumb = docpath.."/cache/"..std.sha1(fpath:gsub(docpath, ""))..".png"
if vfs.exists(thumb) then
vfs.move(thumb, desthumb)
end
-- remove all other thumb files
for i,v in ipairs(data.file) do
thumb = docpath.."/cache/"..std.sha1(v:gsub(docpath, ""))..".png"
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
handle.init = function(args)
local r, e = mkdirp(docpath)
@ -124,6 +167,20 @@ handle.init = function(args)
return result("Docify initialized")
end
handle.select = function(param)
local db = sqlite._getdb(vfs.ospath(dbpath))
if not db then
return error("Unable to get database "..dbpath)
end
local r = sqlite.select(db, param.table, "*", param.cond)
sqlite.dbclose(db)
if r == nil then
return error("Unable to select data from "..param.table)
else
return result(r)
end
end
handle.fetch = function(table)
local db = sqlite._getdb(vfs.ospath(dbpath))
if not db then
@ -176,19 +233,113 @@ handle.preview = function(path)
local tpath = docpath.."/cache/"..name
if not vfs.exists(tpath) then
-- regenerate thumb
local cmd = "convert -resize 200x500 "..vfs.ospath(path).."[0] "..vfs.ospath(tpath)
local cmd = "convert -resize 250x500 "..vfs.ospath(path).."[0] "..vfs.ospath(tpath)
os.execute(cmd)
end
if vfs.exists(tpath) then
local cmd = "rm "..vfs.ospath(tpath)
local cmd = "chmod 777 "..vfs.ospath(tpath)
os.execute(cmd)
return result("exist")
return result(tpath)
else
return error("do not exist")
end
end
handle.get_doc = function(id)
local db = sqlite._getdb(vfs.ospath(dbpath))
if not db then
return error("Unable to get database "..dbpath)
end
local r = sqlite.select(db, "docs", "*", "id = "..id)
if r == nil or #r == 0 then
sqlite.dbclose(db)
return error("Unable to select data from "..param.table)
else
r = r[1]
local o = sqlite.select(db, "owners", "*", "id = "..r.oid)
sqlite.dbclose(db)
if o == nil or #o == 0 then
return result(r)
else
o = o[1]
r.owner = o.name
if r.ctime then
r.ctime = os.date("%d/%m/%Y %H:%M:%S", r.ctime)
end
if r.mtime then
r.mtime = os.date("%d/%m/%Y %H:%M:%S", r.mtime)
end
local edate = ""
return result(r)
end
end
end
handle.deletedoc = function(param)
local db = sqlite._getdb(vfs.ospath(dbpath))
if not db then
return error("Unable to get database "..dbpath)
end
local sql = "DELETE FROM docs WHERE id="..param.id..";"
local ret = sqlite.query(db, sql) == 1
sqlite.dbclose(db)
if not ret then
return error("Unable to delete doc meta-data from database")
end
-- move file to unclassified
local newfile = docpath.."/unclassified/"..std.basename(param.file)
vfs.move(param.file, newfile)
-- delete thumb file
local thumb = docpath.."/cache/"..std.sha1(param.file:gsub(docpath,""))..".png"
if vfs.exists(thumb) then
vfs.delete(thumb)
end
return result("Document entry deleted")
end
handle.updatedoc = function(param)
local r = merge_files(param.data)
if r.error then return r end
if param.rm then
-- move ve the old file to unclassified
local newfile = docpath.."/unclassified/"..std.basename(param.rm)
if vfs.exists(param.rm) then
vfs.move(param.rm, newfile)
end
-- move the thumb file if needed
local thumb = docpath.."/cache/"..std.sha1(param.rm:gsub(docpath,""))..".png"
local newwthumb = docpath.."/cache/"..std.sha1(newfile:gsub(docpath, ""))..".png"
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"))
return handle.update({
table = "docs",
data = param.data
})
end
handle.insertdoc = function(data)
local r = merge_files(data)
if r.error then return r end
-- save data
data.file = r.result
data.ctime = os.time(os.date("!*t"))
data.mtime = os.time(os.date("!*t"))
local ret = handle.insert({
table = "docs",
data = data
})
return ret
end
handle.update = function(param)
if not param.data.id or param.data.id == 0 then
return error("Record id is 0 or not found")

View File

@ -2,4 +2,15 @@
afx-app-window[data-id = "Docify"] .header .label-text
{
font-weight: bold;
}
div[data-id = "preview-container"]
{
overflow: auto;
display: block;
}
canvas[data-id = "preview-canvas"]
{
display: block;
margin:0 auto;
}

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
<afx-app-window apptitle="Docify" width="700" height="400" data-id="Docify">
<afx-app-window apptitle="Docify" width="700" height="500" data-id="Docify">
<afx-hbox >
<div data-width="5"></div>
<afx-vbox data-width="150">
@ -8,15 +8,26 @@
</afx-vbox>
<afx-resizer data-width="4"></afx-resizer>
<afx-vbox data-width = "300">
<afx-grid-view data-id="gridview"></afx-grid-view>
<afx-label class="header" iconclass = "fa fa-bars" text="__(Documents)" data-height="22"></afx-label>
<afx-list-view data-id="docview"></afx-list-view>
<afx-hbox data-height="30">
<div data-width="5"></div>
<afx-button data-id="bt-add-doc" data-width = "25" text = "" iconclass = "fa fa-plus-circle"></afx-button>
<afx-button data-width = "25" text = "" iconclass = "fa fa-minus-circle"></afx-button>
<afx-button data-width = "25" text = "" iconclass = "fa fa-pencil-square-o"></afx-button>
<afx-button data-id="bt-del-doc" data-width = "25" text = "" iconclass = "fa fa-minus-circle"></afx-button>
<afx-button data-id="bt-edit-doc" data-width = "25" text = "" iconclass = "fa fa-pencil-square-o"></afx-button>
</afx-hbox>
</afx-vbox>
<afx-resizer data-width="4"></afx-resizer>
<afx-vbox></afx-vbox>
<afx-vbox>
<div data-id = "preview-container">
<canvas data-id="preview-canvas"></canvas>
</div>
<afx-grid-view data-id="docgrid"></afx-grid-view>
<div style="text-align: right;" data-height="30" >
<afx-button text="__(Open)" data-id="btopen" ></afx-button>
<afx-button text="__(Download)" data-id="btdld" ></afx-button>
<afx-button text="__(Print)" data-id="btopen" ></afx-button>
</div>
</afx-vbox>
</afx-hbox>
</afx-app-window>

Binary file not shown.