Docify: remove server side script, move all operation to client side
All checks were successful
gitea-sync/antosdk-apps/pipeline/head This commit looks good

This commit is contained in:
DanyLE 2023-06-27 22:27:09 +02:00
parent e8aa62735a
commit f8435cd87c
22 changed files with 236 additions and 342 deletions

View File

@ -2,6 +2,7 @@
Simple PDF document manager
## Change logs
- v0.1.1-b: move PDF merge and document thumbnail generation to client side, remove server side lua script
- 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

View File

@ -1,146 +0,0 @@
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
handle.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
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")
end
end
-- move the thumb file to the cache folder
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/"..enc.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.preview = function(path)
-- convert -resize 300x500 noel.pdf[0] thumb.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
if vfs.exists(tpath) then
local cmd = "chmod 777 "..vfs.ospath(tpath)
os.execute(cmd)
return result(tpath)
else
return error("do not exist")
end
end
handle.deletedoc = function(param)
-- move file to unclassified
local newfile = docpath.."/unclassified/"..utils.basename(param.file)
vfs.move(param.file, newfile)
-- delete thumb file
local thumb = docpath.."/cache/"..enc.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 = 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/"..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/"..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
end
param.data.file = r.result
print(r.result)
param.data.mtime = os.time(os.date("!*t"))
return result(param.data)
--return handle.update({
-- table = "docs",
-- data = param.data
--})
end
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

View File

@ -20,6 +20,7 @@
<afx-vbox>
<div data-id = "preview-container">
<canvas data-id="preview-canvas"></canvas>
<canvas data-id="tmp-canvas" style="display:none;"></canvas>
</div>
<afx-grid-view data-id="docgrid"></afx-grid-view>
<div style="text-align: right;" data-height="35" >

View File

@ -20,7 +20,7 @@
"name":"locale-gen",
"data": {
"src": "",
"exclude": ["build/", "api/", "css/", "coffees/"],
"exclude": ["build/", "css/", "coffees/"],
"locale": "en_GB",
"dest": "package.json"
}
@ -70,8 +70,8 @@
"data": {
"src": [
"assets/scheme.html",
"api/api.lua",
"package.json",
"css/main.css",
"README.md"
],
"dest": "build/debug"

View File

@ -2,6 +2,7 @@
Simple PDF document manager
## Change logs
- v0.1.1-b: move PDF merge and document thumbnail generation to client side, remove server side lua script
- 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

View File

@ -1,146 +0,0 @@
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
handle.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
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")
end
end
-- move the thumb file to the cache folder
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/"..enc.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.preview = function(path)
-- convert -resize 300x500 noel.pdf[0] thumb.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
if vfs.exists(tpath) then
local cmd = "chmod 777 "..vfs.ospath(tpath)
os.execute(cmd)
return result(tpath)
else
return error("do not exist")
end
end
handle.deletedoc = function(param)
-- move file to unclassified
local newfile = docpath.."/unclassified/"..utils.basename(param.file)
vfs.move(param.file, newfile)
-- delete thumb file
local thumb = docpath.."/cache/"..enc.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 = 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/"..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/"..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
end
param.data.file = r.result
print(r.result)
param.data.mtime = os.time(os.date("!*t"))
return result(param.data)
--return handle.update({
-- table = "docs",
-- data = param.data
--})
end
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

View File

@ -1,4 +1,3 @@
afx-app-window[data-id = "Docify"] .header .label-text
{
font-weight: bold;

File diff suppressed because one or more lines are too long

View File

@ -7,14 +7,16 @@
"author": "Dany LE",
"email": "mrsang@iohub.dev"
},
"version": "0.1.0-b",
"version": "0.1.1-b",
"category": "Office",
"iconclass": "bi bi-collection-fill",
"mimes": [
"none"
],
"dependencies": [
"SQLiteDB@0.1.0-a"
"SQLiteDB@0.1.0-a",
"libpdfjs@2.6.347-r",
"PDFLib@1.17.1"
],
"locale": {},
"locales": {

View File

@ -20,6 +20,7 @@
<afx-vbox>
<div data-id = "preview-container">
<canvas data-id="preview-canvas"></canvas>
<canvas data-id="tmp-canvas" style="display:none;"></canvas>
</div>
<afx-grid-view data-id="docgrid"></afx-grid-view>
<div style="text-align: right;" data-height="35" >

Binary file not shown.

View File

@ -7,14 +7,16 @@
"author": "Dany LE",
"email": "mrsang@iohub.dev"
},
"version": "0.1.0-b",
"version": "0.1.1-b",
"category": "Office",
"iconclass": "bi bi-collection-fill",
"mimes": [
"none"
],
"dependencies": [
"SQLiteDB@0.1.0-a"
"SQLiteDB@0.1.0-a",
"libpdfjs@2.6.347-r",
"PDFLib@1.17.1"
],
"locale": {},
"locales": {

View File

@ -1,5 +1,7 @@
namespace OS {
export namespace application {
declare var pdfjsLib;
declare var PDFLib;
export class Docify extends BaseApplication {
private catview: GUI.tag.ListViewTag;
@ -108,6 +110,7 @@ namespace OS {
main() {
if (!this.setting.printer) { this.setting.printer = ""; }
pdfjsLib.GlobalWorkerOptions.workerSrc = "pkg://libpdfjs/pdf.worker.js".asFileHandle().getlink();
this.catview = this.find("catview") as GUI.tag.ListViewTag;
this.docview = this.find("docview") as GUI.tag.ListViewTag;
@ -288,12 +291,13 @@ namespace OS {
const timestamp = Math.floor(Date.now() / 1000);
data.ctime = timestamp;
data.mtime = timestamp;
const r = await this.exec("merge_files", data);
if(r.error)
const r = await this.merge_files(data);
data.file = r.path;
/*if(r.error)
{
throw new Error(r.error.toString());
}
data.file = r.result;
data.file = r.result;*/
this.docdb.cache = data;
const d = await this.docdb.write(undefined);
if(d.error)
@ -326,12 +330,16 @@ namespace OS {
{
throw new Error(r.error.toString());
}
r = await this.exec("deletedoc", {file: item.data.file});
if(r.error)
{
throw new Error(r.error.toString());
// delete file
try {
await item.data.file.asFileHandle().remove();
const thumb = await this.get_thumb_path(item.data.file);
await thumb.asFileHandle().remove();
}
catch(e)
{
console.log(e);
}
this.notify(r.result.toString());
this.update_doclist(item.data.cid);
return this.clear_preview();
}
@ -362,22 +370,40 @@ namespace OS {
data.id = item.data.id;
const timestamp = Math.floor(Date.now() / 1000);
data.mtime = timestamp;
let d = await this.exec("updatedoc", {
data,
rm: !data.file.includes(item.data.file) ? item.data.file : false
});
if(d.error)
if(data.file.includes(item.data.file) && data.file.length == 1)
{
throw new Error(d.error);
// nothing changes
data.file = item.data.file;
}
else
{
if(!data.file.includes(item.data.file))
{
// remove old file
try {
console.log("remove old file", item.data.file);
await item.data.file.asFileHandle().remove();
const thumb = await this.get_thumb_path(item.data.file);
await thumb.asFileHandle().remove();
}
catch(e)
{
console.log(e);
}
}
// merge all PDF file
const merged_file = await this.merge_files(data);
data.file = merged_file.path;
}
data.mtime = Math.floor(Date.now() / 1000);
const handle = item.data.$vfs;
handle.cache = d.result;
d = await handle.write(undefined);
handle.cache = data;
const d = await handle.write(undefined);
if(d.error)
{
throw new Error(d.error);
}
if (d.result) { this.toast(d.result); }
this.toast(__("Document updated"));
this.update_doclist(catiem.data.id);
return this.clear_preview();
}
@ -389,6 +415,90 @@ namespace OS {
return this.initialize();
}
private async get_thumb_path(filepath: string| API.VFS.BaseFileHandle)
{
const path = filepath.asFileHandle().path;
let thumb_name = await this.sha1(path.replace(this.setting.docpath,""));
return `${this.setting.docpath}/cache/${thumb_name}.png`;
}
private async merge_files(data) {
const paths: string[] = data.file;
const cat = data.cid.toFixed(1).toString();
const dir = `${this.setting.docpath}/${cat}`.asFileHandle();
try{
await dir.onready();
}
catch(_)
{
const ret = await dir.parent().mk(cat);
if(ret.error)
{
throw new Error(ret.error.toString());
}
await dir.onready();
}
const des_file = `${dir.path}/${new Date(Date.now()).getTime().toString()}.pdf`.asFileHandle();
// concat the file
const pdfdoc = await PDFLib.PDFDocument.create();
let pages = [];
for(const path of paths)
{
const arr = await path.asFileHandle().read("binary");
const doc = await PDFLib.PDFDocument.load(arr, { ignoreEncryption: true });
const copiedpages = await pdfdoc.copyPages(doc, doc.getPageIndices());
pages = pages.concat(copiedpages);
}
for (let i = 0; i < pages.length; i++) {
await pdfdoc.insertPage(i, pages[i]);
}
const buffer = await pdfdoc.save();
des_file.cache = new Blob([buffer]);
const ret = await des_file.write("binary");
if(ret.error)
{
throw new Error(ret.error.toString());
}
// move thumb file
let src_tfile = await this.get_thumb_path(paths[0]);
const dest_tfile = await this.get_thumb_path(des_file);
try {
console.log("Move", src_tfile, "to", dest_tfile);
const ret = await src_tfile.asFileHandle().move(dest_tfile);
if(ret.error)
{
console.log(ret.error);
}
}
catch(e)
{
console.log(e);
}
// remove other file and thumb file
for(const path of paths)
{
try{
src_tfile = await this.get_thumb_path(path);
console.log("'Remove file", path, src_tfile);
let ret = await path.asFileHandle().remove();
if(ret.error)
{
console.log(ret);
}
ret = await src_tfile.asFileHandle().remove();
if(ret.error)
{
console.log(ret);
}
}
catch(e)
{
console.log(e);
}
}
return des_file;
}
private async update_doclist(cid: any) {
try
{
@ -399,7 +509,6 @@ namespace OS {
order: ["year$desc", "month$desc", "day$desc"]
});
// this.exec("select",{table: "docs", cond:`cid = ${cid} ORDER BY year DESC, month DESC, day DESC`});
if(d.error)
{
throw new Error(d.error);
@ -421,13 +530,71 @@ namespace OS {
return this.docgrid.rows = [];
}
private async sha1(str) {
const enc = new TextEncoder();
const hash = await crypto.subtle.digest('SHA-1', enc.encode(str));
return Array.from(new Uint8Array(hash))
.map(v => (v.toString(16) as any).padStart(2, '0'))
.join('');
}
private async genthumb(path: string)
{
/** try to search if the thumb file exists,
* if it does not exist, generate it using
* pdfjs library and an hidden canvas
*/
const tpath = await this.get_thumb_path(path);
const file = tpath.asFileHandle();
try {
await file.onready();
}
catch(e)
{
// generate thumb file
//data = await file.read("binary");
console.log("Try to generate thumb file for", path);
const canvas = this.find("tmp-canvas") as HTMLCanvasElement;
const context = canvas.getContext('2d');
const pdf = await pdfjsLib.getDocument(path.asFileHandle().getlink()).promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 0.33});
// Support HiDPI-screens.
const outputScale = window.devicePixelRatio || 1;
canvas.width = Math.floor(viewport.width * outputScale);
canvas.height = Math.floor(viewport.height * outputScale);
canvas.style.width = Math.floor(viewport.width) + "px";
canvas.style.height = Math.floor(viewport.height) + "px";
const transform = outputScale !== 1
? [outputScale, 0, 0, outputScale, 0, 0]
: null;
const renderContext = {
canvasContext: context,
transform: transform,
viewport: viewport
};
await page.render(renderContext).promise;
const url = canvas.toDataURL('image/png');
file.cache = url;
const ret = await file.write("base64");
if(ret.error)
{
throw new Error(ret.error.toString());
}
await file.onready();
}
return file;
}
async preview(path: any, canvas: HTMLCanvasElement) {
try {
const d = await this.exec("preview", path);
if (d.error) {
throw new Error(d.error);
}
const file = d.result.asFileHandle();
const file = await this.genthumb(path);
const data = await file.read("binary");
const img = new Image();
//($ me.view).append img
@ -435,7 +602,6 @@ namespace OS {
const context = canvas.getContext('2d');
canvas.height = img.height;
canvas.width = img.width;
//console.log canvas.width, canvas.height
return context.drawImage(img, 0, 0);
};
@ -492,18 +658,6 @@ namespace OS {
}
}
exec(action: string, args?: GenericObject<any>) {
const cmd = {
path: `${this.path()}/api.lua`,
parameters: {
action,
docpath: this.setting.docpath,
args
}
};
return this.call(cmd);
}
menu() {
return [
{
@ -538,6 +692,10 @@ namespace OS {
}
}
}
Docify.dependencies = ["pkg://SQLiteDB/libsqlite.js"];
Docify.dependencies = [
"pkg://SQLiteDB/libsqlite.js",
"pkg://libpdfjs/pdf.js",
"pkg://PDFLib/main.js"
];
}
}

Binary file not shown.

View File

@ -69,6 +69,16 @@
"dependencies": ["Antunnel@0.2.0-b"],
"download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/2.0.x/AntunnelPlugins/build/release/AntunnelPlugins.zip"
},
{
"pkgname": "AntunnelTestClient",
"name": "AntunnelTestClient",
"description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/2.0.x/AntunnelTestClient/README.md",
"category": "Development",
"author": "Dany LE",
"version": "0.1.0-a",
"dependencies": ["Antunnel@0.2.1-b"],
"download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/2.0.x/AntunnelTestClient/build/release/AntunnelTestClient.zip"
},
{
"pkgname": "Archive",
"name": "Archive",
@ -145,8 +155,8 @@
"description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/2.0.x/Docify/README.md",
"category": "Office",
"author": "Dany LE",
"version": "0.1.0-b",
"dependencies": ["SQLiteDB@0.1.0-a"],
"version": "0.1.1-b",
"dependencies": ["SQLiteDB@0.1.0-a","libpdfjs@2.6.347-r","PDFLib@1.17.1"],
"download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/2.0.x/Docify/build/release/Docify.zip"
},
{

View File

@ -41,7 +41,7 @@ end
local packages = {}
for i,v in ipairs(r) do
for i,v in pairs(r) do
if v.type == "dir" then
local ar_file = v.path.."/build/release/"..v.filename..".zip"
local meta_file = v.path.."/package.json"
@ -74,6 +74,12 @@ for i,v in ipairs(r) do
download = release_url..v.filename..".zip"
}
table.insert(packages, pkg)
--else
-- if not ulib.exists(ar_file) then
-- output("AR file not found "..ar_file)
-- else
-- output("Meta file not found "..meta_file)
-- end
end
end
end

View File

@ -2,6 +2,7 @@
Simple PDF document manager
## Change logs
- v0.1.1-b: move PDF merge and document thumbnail generation to client side, remove server side lua script
- 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

Binary file not shown.

Binary file not shown.

4
release/libjpeg.md Normal file
View File

@ -0,0 +1,4 @@
# libjpeg
Simple JPEG/DCT data decoder in JavaScript. Also this project includes JPEG 2000 and JBIG2 decoders.
Github page: [https://github.com/notmasteryet/jpgjs](https://github.com/notmasteryet/jpgjs)

BIN
release/libjpeg.zip Normal file

Binary file not shown.

File diff suppressed because one or more lines are too long