mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2025-07-12 22:03:29 +02:00
Docify: Use libsqlite to handle database in Docify
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
Simple PDF document manager
|
||||
|
||||
## Change logs
|
||||
- v0.1.0-b: use libsqlite for database handling
|
||||
- v0.0.9-b: Adapt to support AntOS 2.0.x
|
||||
- v0.0.8-b: Allow upload files directly from the app
|
||||
- v0.0.7-a: Change category and icon
|
||||
|
@ -1,12 +1,10 @@
|
||||
local arg = ...
|
||||
|
||||
ulib = require("ulib")
|
||||
sqlite = modules.sqlite()
|
||||
vfs = require("vfs")
|
||||
|
||||
local handle = {}
|
||||
local docpath = nil
|
||||
local dbpath = nil
|
||||
|
||||
local result = function(data)
|
||||
return {
|
||||
@ -31,7 +29,7 @@ local mkdirp =function(p)
|
||||
return true, nil
|
||||
end
|
||||
|
||||
local merge_files = function(data)
|
||||
handle.merge_files = function(data)
|
||||
local firstfile = data.file[1]
|
||||
local fpath = docpath.."/"..data.cid
|
||||
local r, e = mkdirp(fpath)
|
||||
@ -57,14 +55,14 @@ local merge_files = function(data)
|
||||
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"
|
||||
local thumb = docpath.."/cache/"..enc.sha1(firstfile:gsub(docpath, ""))..".png"
|
||||
local desthumb = docpath.."/cache/"..enc.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"
|
||||
thumb = docpath.."/cache/"..enc.sha1(v:gsub(docpath, ""))..".png"
|
||||
if vfs.exists(thumb) then
|
||||
vfs.delete(thumb)
|
||||
end
|
||||
@ -76,166 +74,15 @@ local merge_files = function(data)
|
||||
return result(fpath)
|
||||
end
|
||||
|
||||
handle.init = function(args)
|
||||
|
||||
local r, e = mkdirp(docpath)
|
||||
if not r then return e end
|
||||
|
||||
r, e = mkdirp(docpath.."/unclassified")
|
||||
if not r then return e end
|
||||
|
||||
r, e = mkdirp(docpath.."/cache")
|
||||
if not r then return e end
|
||||
|
||||
local db = sqlite._getdb(vfs.ospath(dbpath))
|
||||
if not db then
|
||||
return error("Unable to initialized database "..dbpath)
|
||||
end
|
||||
local sql
|
||||
-- check if table exists
|
||||
if sqlite.hasTable(db, "categories") == 0 then
|
||||
-- create the table
|
||||
sql = [[
|
||||
CREATE TABLE "categories" (
|
||||
"id" INTEGER,
|
||||
"name" TEXT NOT NULL,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
]]
|
||||
if sqlite.query(db, sql) ~= 1 then
|
||||
sqlite.dbclose(db)
|
||||
return error("Unable to create table categories")
|
||||
end
|
||||
-- insert unknown category
|
||||
sql = [[
|
||||
INSERT INTO categories("id","name") VALUES (0,'Uncategoried');
|
||||
]]
|
||||
if sqlite.query(db, sql) ~= 1 then
|
||||
sqlite.dbclose(db)
|
||||
return error("Unable to create default category")
|
||||
end
|
||||
end
|
||||
|
||||
if sqlite.hasTable(db, "owners") == 0 then
|
||||
-- create the table
|
||||
sql = [[
|
||||
CREATE TABLE "owners" (
|
||||
"id" INTEGER,
|
||||
"name" TEXT NOT NULL,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
]]
|
||||
if sqlite.query(db, sql) ~= 1 then
|
||||
sqlite.dbclose(db)
|
||||
return error("Unable to create table owners")
|
||||
end
|
||||
-- insert unknown category
|
||||
sql = [[
|
||||
INSERT INTO owners("id","name") VALUES (0,'None');
|
||||
]]
|
||||
if sqlite.query(db, sql) ~= 1 then
|
||||
sqlite.dbclose(db)
|
||||
return error("Unable to create default None owner")
|
||||
end
|
||||
end
|
||||
|
||||
if sqlite.hasTable(db, "docs") == 0 then
|
||||
-- create the table
|
||||
sql = [[
|
||||
CREATE TABLE "docs" (
|
||||
"id" INTEGER,
|
||||
"name" TEXT NOT NULL,
|
||||
"ctime" INTEGER,
|
||||
"day" INTEGER,
|
||||
"month" INTEGER,
|
||||
"year" INTEGER,
|
||||
"cid" INTEGER DEFAULT 0,
|
||||
"oid" INTEGER DEFAULT 0,
|
||||
"file" TEXT NOT NULL,
|
||||
"tags" TEXT,
|
||||
"note" TEXT,
|
||||
"mtime" INTEGER,
|
||||
FOREIGN KEY("oid") REFERENCES "owners"("id") ON DELETE SET DEFAULT ON UPDATE NO ACTION,
|
||||
FOREIGN KEY("cid") REFERENCES "categories"("id") ON DELETE SET DEFAULT ON UPDATE NO ACTION,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
]]
|
||||
if sqlite.query(db, sql) ~= 1 then
|
||||
sqlite.dbclose(db)
|
||||
return error("Unable to create table docs")
|
||||
end
|
||||
end
|
||||
sqlite.dbclose(db)
|
||||
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
|
||||
return error("Unable to get database "..dbpath)
|
||||
end
|
||||
local r = sqlite.select(db, table, "*", "1=1")
|
||||
sqlite.dbclose(db)
|
||||
if r == nil then
|
||||
return error("Unable to fetch data from "..table)
|
||||
else
|
||||
return result(r)
|
||||
end
|
||||
end
|
||||
|
||||
handle.insert = function(param)
|
||||
local db = sqlite._getdb(vfs.ospath(dbpath))
|
||||
if not db then
|
||||
return error("Unable to get database "..dbpath)
|
||||
end
|
||||
local keys = {}
|
||||
local values = {}
|
||||
for k,v in pairs(param.data) do
|
||||
if k ~= "id" then
|
||||
table.insert(keys,k)
|
||||
if type(v) == "number" then
|
||||
table.insert(values, v)
|
||||
elseif type(v) == "boolean" then
|
||||
table.insert( values, v and 1 or 0 )
|
||||
else
|
||||
local t = "\""..v:gsub('"', '""').."\""
|
||||
table.insert(values,t)
|
||||
end
|
||||
end
|
||||
end
|
||||
local sql = "INSERT INTO "..param.table.." ("..table.concat(keys,',')..') VALUES ('
|
||||
sql = sql..table.concat(values,',')..');'
|
||||
local r = sqlite.query(db, sql)
|
||||
sqlite.dbclose(db)
|
||||
if r == nil then
|
||||
return error("Unable to insert data to "..param.table)
|
||||
else
|
||||
return result("Data inserted")
|
||||
end
|
||||
end
|
||||
|
||||
handle.preview = function(path)
|
||||
-- convert -resize 300x500 noel.pdf[0] thumb.png
|
||||
local name = std.sha1(path:gsub(docpath,""))..".png"
|
||||
local name = enc.sha1(path:gsub(docpath,""))..".png"
|
||||
-- try to find the thumb
|
||||
local tpath = docpath.."/cache/"..name
|
||||
if not vfs.exists(tpath) then
|
||||
-- regenerate thumb
|
||||
local cmd = "convert -resize 250x500 \""..vfs.ospath(path).."\"[0] "..vfs.ospath(tpath)
|
||||
LOG_ERROR(cmd)
|
||||
os.execute(cmd)
|
||||
end
|
||||
|
||||
@ -248,57 +95,12 @@ handle.preview = function(path)
|
||||
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 ret, meta = vfs.fileinfo(r.file)
|
||||
if ret then
|
||||
r.fileinfo = meta
|
||||
end
|
||||
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)
|
||||
local newfile = docpath.."/unclassified/"..utils.basename(param.file)
|
||||
vfs.move(param.file, newfile)
|
||||
-- delete thumb file
|
||||
local thumb = docpath.."/cache/"..std.sha1(param.file:gsub(docpath,""))..".png"
|
||||
local thumb = docpath.."/cache/"..enc.sha1(param.file:gsub(docpath,""))..".png"
|
||||
if vfs.exists(thumb) then
|
||||
vfs.delete(thumb)
|
||||
end
|
||||
@ -306,20 +108,20 @@ handle.deletedoc = function(param)
|
||||
end
|
||||
|
||||
handle.updatedoc = function(param)
|
||||
local r = merge_files(param.data)
|
||||
local r = handle.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)
|
||||
local newfile = docpath.."/unclassified/"..utils.basename(param.rm)
|
||||
local cmd = "rm -f "..vfs.ospath(param.rm)
|
||||
os.execute(cmd)
|
||||
--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"
|
||||
local thumb = docpath.."/cache/"..enc.sha1(param.rm:gsub(docpath,""))..".png"
|
||||
local newwthumb = docpath.."/cache/"..enc.sha1(newfile:gsub(docpath, ""))..".png"
|
||||
if vfs.exists(thumb) then
|
||||
vfs.move(thumb, newwthumb)
|
||||
end
|
||||
@ -327,107 +129,16 @@ handle.updatedoc = function(param)
|
||||
param.data.file = r.result
|
||||
print(r.result)
|
||||
param.data.mtime = os.time(os.date("!*t"))
|
||||
return handle.update({
|
||||
table = "docs",
|
||||
data = param.data
|
||||
})
|
||||
return result(param.data)
|
||||
--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")
|
||||
end
|
||||
local db = sqlite._getdb(vfs.ospath(dbpath))
|
||||
if not db then
|
||||
return error("Unable to get database "..dbpath)
|
||||
end
|
||||
local lst = {}
|
||||
for k,v in pairs(param.data) do
|
||||
if(type(v)== "number") then
|
||||
table.insert(lst,k.."="..v)
|
||||
elseif type(v) == "boolean" then
|
||||
table.insert( lst, k.."="..(v and 1 or 0) )
|
||||
else
|
||||
table.insert(lst,k.."=\""..v:gsub('"', '""').."\"")
|
||||
end
|
||||
end
|
||||
|
||||
local sql = "UPDATE "..param.table.." SET "..table.concat(lst,",").." WHERE id="..param.data.id..";"
|
||||
local r = sqlite.query(db, sql)
|
||||
sqlite.dbclose(db)
|
||||
if r == nil then
|
||||
return error("Unable to update data to "..param.table)
|
||||
else
|
||||
return result("Data Updated")
|
||||
end
|
||||
end
|
||||
|
||||
handle.delete = function(param)
|
||||
if param.id == 0 then
|
||||
return error("Record with id = 0 cannot be deleted")
|
||||
end
|
||||
|
||||
local db = sqlite._getdb(vfs.ospath(dbpath))
|
||||
if not db then
|
||||
return error("Unable to get database "..dbpath)
|
||||
end
|
||||
local sql = "DELETE FROM "..param.table.." WHERE id="..param.id..";"
|
||||
local r = sqlite.query(db, sql)
|
||||
sqlite.dbclose(db)
|
||||
if r == nil then
|
||||
return error("Unable to delete data from "..param.table)
|
||||
else
|
||||
return result("Data deleted")
|
||||
end
|
||||
end
|
||||
|
||||
handle.printdoc = function(opt)
|
||||
local cmd = "lp "
|
||||
if opt.printer and opt.printer ~= "" then
|
||||
cmd = cmd .. " -d "..opt.printer
|
||||
end
|
||||
if opt.side == 0 then
|
||||
cmd = cmd.. " -o sides=one-sided"
|
||||
elseif opt.side == 1 then
|
||||
cmd = cmd.. " -o sides=two-sided-long-edge"
|
||||
elseif opt.side == 2 then
|
||||
cmd = cmd .. " -o sides=two-sided-short-edge"
|
||||
end
|
||||
-- orientation landscape
|
||||
if opt.orientation == 1 then
|
||||
cmd = cmd.." -o orientation-requested=5"
|
||||
end
|
||||
|
||||
if opt.range == 1 then
|
||||
cmd = cmd.." -P "..opt.pages
|
||||
end
|
||||
|
||||
cmd = cmd.. " "..vfs.ospath(opt.file)
|
||||
print(cmd)
|
||||
os.execute(cmd)
|
||||
return result("A print job has been posted on server. Check if it successes")
|
||||
end
|
||||
|
||||
|
||||
if arg.action and handle[arg.action] then
|
||||
-- check if the database exits
|
||||
docpath = arg.docpath
|
||||
dbpath = docpath.."/docify.db"
|
||||
|
||||
return handle[arg.action](arg.args)
|
||||
else
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,15 +1,68 @@
|
||||
{
|
||||
"pkgname": "Docify",
|
||||
"app":"Docify",
|
||||
"name":"Docify",
|
||||
"description":"Docify",
|
||||
"info":{
|
||||
"author": "",
|
||||
"email": ""
|
||||
"app": "Docify",
|
||||
"name": "Docify",
|
||||
"description": "Simple document manager",
|
||||
"info": {
|
||||
"author": "Dany LE",
|
||||
"email": "mrsang@iohub.dev"
|
||||
},
|
||||
"version":"0.0.9-b",
|
||||
"category":"Office",
|
||||
"iconclass":"bi bi-collection-fill",
|
||||
"mimes":["none"],
|
||||
"locale": {}
|
||||
"version": "0.1.0-b",
|
||||
"category": "Office",
|
||||
"iconclass": "bi bi-collection-fill",
|
||||
"mimes": [
|
||||
"none"
|
||||
],
|
||||
"dependencies": [
|
||||
"SQLiteDB@0.1.0-a"
|
||||
],
|
||||
"locale": {},
|
||||
"locales": {
|
||||
"en_GB": {
|
||||
"title": "title",
|
||||
"Day": "Day",
|
||||
"Month": "Month",
|
||||
"Year": "Year",
|
||||
"Files": "Files",
|
||||
"Note": "Note",
|
||||
"Owner": "Owner",
|
||||
"Tags": "Tags",
|
||||
"Save": "Save",
|
||||
"Document preview": "Document preview",
|
||||
"Ok": "Ok",
|
||||
"Unable to get owner data handle": "Unable to get owner data handle",
|
||||
"Name": "Name",
|
||||
"Do you realy want to delete: `{0}`": "Do you realy want to delete: `{0}`",
|
||||
"Unable to fetch owner list: {0}": "Unable to fetch owner list: {0}",
|
||||
"Please enter title": "Please enter title",
|
||||
"Please attach files to the entry": "Please attach files to the entry",
|
||||
"Unable to fetch unclassified file list: {0}": "Unable to fetch unclassified file list: {0}",
|
||||
"Options": "Options",
|
||||
"Owners": "Owners",
|
||||
"Preview": "Preview",
|
||||
"Change doc path": "Change doc path",
|
||||
"No configured docpath": "No configured docpath",
|
||||
"Unable to init database file: {0}": "Unable to init database file: {0}",
|
||||
"Unable to download: {0}": "Unable to download: {0}",
|
||||
"Unable to open file: {0}": "Unable to open file: {0}",
|
||||
"Category": "Category",
|
||||
"Unable to insert category: {0}": "Unable to insert category: {0}",
|
||||
"Unable delete category: {0}": "Unable delete category: {0}",
|
||||
"Unable to update category: {0}": "Unable to update category: {0}",
|
||||
"Unable to fetch document detail: {0}": "Unable to fetch document detail: {0}",
|
||||
"Please select a category": "Please select a category",
|
||||
"Unable to add document: {0}": "Unable to add document: {0}",
|
||||
"Do you really want to delete: `{0}`": "Do you really want to delete: `{0}`",
|
||||
"Unable to delete document: {0}": "Unable to delete document: {0}",
|
||||
"File uploaded": "File uploaded",
|
||||
"Unable to upload document: {0}": "Unable to upload document: {0}",
|
||||
"Unable to edit document metadata: {0}": "Unable to edit document metadata: {0}",
|
||||
"Unable to update document list: {0}": "Unable to update document list: {0}",
|
||||
"Unable to generate document thumbnail: {0}": "Unable to generate document thumbnail: {0}",
|
||||
"Please select a doc path": "Please select a doc path",
|
||||
"Error initialize database: {0}": "Error initialize database: {0}",
|
||||
"Categories": "Categories",
|
||||
"Documents": "Documents"
|
||||
}
|
||||
}
|
||||
}
|
@ -25,7 +25,6 @@
|
||||
<div style="text-align: right;" data-height="35" >
|
||||
<afx-button text="" iconclass="bi bi-arrow-up-right-square" data-id="btopen" ></afx-button>
|
||||
<afx-button text="" iconclass="bi bi-cloud-arrow-down" data-id="btdld" ></afx-button>
|
||||
<afx-button text="" iconclass = "bi bi-printer" data-id="btprint" ></afx-button>
|
||||
</div>
|
||||
</afx-vbox>
|
||||
</afx-hbox>
|
||||
|
Reference in New Issue
Block a user