Skip to content
This repository was archived by the owner on Apr 16, 2025. It is now read-only.

Commit 916ccd3

Browse files
committed
update textWeb
1 parent eb0bd53 commit 916ccd3

3 files changed

Lines changed: 188 additions & 10 deletions

File tree

ext/built-in/textweb/src/index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,27 @@ async function newPage(Url, updateCb, doneCb, nowPageId, maxPageId) {
244244
updateCb('[Processing content]', maxPageId, maxPageId, []);
245245
try {
246246
//pr = await sharedLib.process({ context: rr, opt: processPicOpt,svgOpt:processSvgPicOption , url: `${url.protocol}//${url.host}/${url.pathname}${url.search}` }, processServerHost);
247+
let o = [];
248+
extraData.pics.forEach(c=>{
249+
if (c.startsWith('http://')){
250+
// http://127.0.0.1:2233/url
251+
let t = c.split('/');
252+
if (t.length<4){
253+
o.push(c);
254+
}else{
255+
o.push(sharedLib.encode.decode(t[3]));
256+
}
257+
}else{
258+
o.push(c);
259+
}
260+
});
247261
pr = {
248262
title: rr.title,
249263
txt: rr.txt,
250264
suggest: rr.suggest,
251265
links: extraData.links,
252-
pics: extraData.pics
266+
pics: extraData.pics,
267+
rawPics: o
253268
}
254269
} catch (e) {
255270
//console.log(e);
@@ -353,6 +368,25 @@ export class application extends extType.application.base {
353368
doneCb();
354369
return;
355370
}
371+
if ((/^p[0-9]+$/g).test(um.text)){
372+
if (typeof __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.rawPics[parseInt(um.text.slice(1))] == 'string'){
373+
updateCb(`[Loading pic]`, __classPrivateFieldGet(this, _application_nowPageId, "f"), __classPrivateFieldGet(this, _application_page, "f").length - 1, []);
374+
try{
375+
await sharedLib.viewPic(__classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.rawPics[parseInt(um.text.slice(1))]);
376+
}catch(e){
377+
updateCb(`[Error] error happened while loading pic ${um.text}. \n\`\`\`\n${e.code}\n${e.message}\n${e.cause}\n${e.stack}\n\`\`\`\n`, __classPrivateFieldGet(this, _application_nowPageId, "f"), __classPrivateFieldGet(this, _application_page, "f").length - 1, __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.suggest.concat(cmds));
378+
doneCb();
379+
return;
380+
}
381+
updateCb(`[done]`, __classPrivateFieldGet(this, _application_nowPageId, "f"), __classPrivateFieldGet(this, _application_page, "f").length - 1, __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.suggest.concat(cmds));
382+
doneCb();
383+
return;
384+
}else{
385+
updateCb(`[Error] the page ${__classPrivateFieldGet(this, _application_nowPageId, "f")} does not has pic ${um.text}.`, __classPrivateFieldGet(this, _application_nowPageId, "f"), __classPrivateFieldGet(this, _application_page, "f").length - 1, __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.suggest.concat(cmds));
386+
doneCb();
387+
return;
388+
}
389+
}
356390
if (!((/[^0-9]/g).test(um.text))) {
357391
if (typeof __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.links[parseInt(um.text)] == 'string') {
358392
let r = await newPage(__classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.links[parseInt(um.text)], updateCb, doneCb, __classPrivateFieldGet(this, _application_page, "f").length, __classPrivateFieldGet(this, _application_page, "f").length);
@@ -474,6 +508,8 @@ export class application extends extType.application.base {
474508
} catch (e) {
475509
console.log('[picProcessServer] picProcessServerError', e);
476510
try {
511+
res.writeHead(200, 'OK', { 'content-type': 'image/png', 'content-length': `${picLoadFailTip.length}` });
512+
res.write(picLoadFailTip);
477513
res.end();
478514
} catch (ee) { }
479515
}

ext/built-in/textweb/src/libPic.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import * as http from "node:http";
66
import * as childProcess from "node:child_process";
77
import * as fs from "node:fs";
88

9-
async function ffmpegDecodeToPng(data,extname){
9+
export async function ffmpegDecodeToPng(data,extname){
1010
return new Promise((resolve,reject)=>{
1111
var fn = sharedLib.random.randomUUID();
12-
fs.writeFileSync(`/ydpsys/tmp/${fn}.${extname}`,data);
13-
var ffmpeg = childProcess.spawn('chroot',['.','ffmpeg','-i',`/tmp/${fn}.${extname}`,`/tmp/${fn}.png`],{cwd:'/ydpsys'});
12+
fs.writeFileSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.${extname}`,data);
13+
var ffmpeg = childProcess.spawn('chroot',['.','ffmpeg','-i',`/tmp/${fn}.${extname}`,`/tmp/${fn}.png`],{cwd:`${process.env['ydpSysRootPath']}`});
1414
var outTxt = '';
1515
ffmpeg.stdout.on('data',(d)=>{
1616
outTxt += String(d);
@@ -24,13 +24,13 @@ async function ffmpegDecodeToPng(data,extname){
2424
}else{
2525
console.log('[libPic] ffmpeg exit with code',code,'. ');
2626
}
27-
if (!fs.existsSync(`/ydpsys/tmp/${fn}.png`)){
27+
if (!fs.existsSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.png`)){
2828
reject(outTxt);
2929
return;
3030
}
31-
var res = fs.readFileSync(`/ydpsys/tmp/${fn}.png`);
32-
fs.unlinkSync(`/ydpsys/tmp/${fn}.png`);
33-
fs.unlinkSync(`/ydpsys/tmp/${fn}.${extname}`);
31+
var res = fs.readFileSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.png`);
32+
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.png`);
33+
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.${extname}`);
3434
resolve(res);
3535
});
3636
});
@@ -139,6 +139,7 @@ export async function processPic(path) {
139139
data = await ffmpegDecodeToPng(data,'pic');
140140
img = await imageJs.Image.load(data);
141141
}catch(ee){
142+
console.log('[libPic] try ffmpeg failed. \n',ee);
142143
return {
143144
type: 'text/plain',
144145
buff:Buffer.from(`${url}\n${JSON.stringify(opt)}\n${ee}`)
@@ -172,7 +173,7 @@ export async function processPic(path) {
172173
type: 'image/png'
173174
};
174175
}
175-
export async function process(Url, nowUrl, processServerHost, opt,svgOpt) {
176+
export async function processLagacy(Url, nowUrl, processServerHost, opt,svgOpt) {
176177
return new Promise((resolve, reject) => {
177178
try {
178179
/*

ext/built-in/textweb/src/sharedLib.js

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as random from "./libRandom.js";
88
import * as zlib from "node:zlib";
99
import * as fs from "node:fs";
1010
import * as dns from "node:dns";
11+
import * as child from "node:child_process";
1112

1213
export {md,pic,encode,random};
1314
const html2mdFix = html2md.default;
@@ -214,7 +215,7 @@ export async function lagacyProcess(input, processServerHost) {
214215
break;
215216
}
216217
case 'pic': {
217-
let o = await pic.process(c.data, input.url, processServerHost, input.opt,input.svgOpt);
218+
let o = await pic.processLagacy(c.data, input.url, processServerHost, input.opt,input.svgOpt);
218219
if (!pics.includes(o[0].data)) pics.push(o[0].data);
219220
out = out.concat(o);
220221
break;
@@ -406,4 +407,144 @@ export function loadCookie(host,cookieDir){
406407
//thost.shift();
407408
}
408409
return out;
410+
}
411+
412+
export async function viewPic(url){
413+
console.log('[viewPic] view',url);
414+
var id = random.randomUUID();
415+
var fn = (url.startsWith('https://') || url.startsWith('http://')) ? `file:///tmp/${id}.png` : url;
416+
var code = `
417+
import QtQuick 2.15
418+
import QtQuick.Controls 1.0
419+
import QtQuick.Layouts 1.15
420+
421+
ApplicationWindow {
422+
id: root
423+
visible: true
424+
width: 320
425+
height: 170
426+
title: "Image Viewer"
427+
color: "black"
428+
flags: Qt.FramelessWindowHint
429+
Rectangle {
430+
id: imageArea
431+
color: "black"
432+
width: parent.width*0.9
433+
height: parent.height
434+
x: parent.width*0.1
435+
y:0
436+
Image {
437+
id: imageViewer
438+
x:0
439+
y:0
440+
scale: 1
441+
source: "${fn}"
442+
fillMode: Image.PreserveAspectFit
443+
}
444+
MouseArea {
445+
id: dragArea
446+
anchors.fill: parent
447+
drag.target: imageViewer
448+
drag.axis: Drag.XAndYAxis
449+
450+
}
451+
}
452+
Rectangle {
453+
color: "black"
454+
width: parent.width*0.1
455+
height: parent.height
456+
Button {
457+
text: "×"
458+
x: parent.width * 0.05
459+
y: parent.width * 0.05
460+
width: parent.width * 0.9
461+
height: parent.width * 0.9
462+
onClicked: Qt.quit()
463+
464+
}
465+
Button {
466+
text: "C"
467+
x: parent.width * 0.05
468+
y: parent.height - parent.width * 0.05*3 - parent.width * 0.9*3
469+
width: parent.width * 0.9
470+
height: parent.width * 0.9
471+
onClicked: {
472+
imageViewer.x = imageViewer.width * (imageViewer.scale-1) / 2;
473+
imageViewer.y = imageViewer.height * (imageViewer.scale-1) / 2;
474+
}
475+
476+
}
477+
Button {
478+
text: "+"
479+
x: parent.width * 0.05
480+
y: parent.height - parent.width * 0.05*2 - parent.width * 0.9*2
481+
width: parent.width * 0.9
482+
height: parent.width * 0.9
483+
onClicked: {
484+
imageViewer.scale = imageViewer.scale + 0.02;
485+
}
486+
487+
}
488+
Button {
489+
text: "-"
490+
x: parent.width * 0.05
491+
y: parent.height - parent.width * 0.05 - parent.width * 0.9
492+
width: parent.width * 0.9
493+
height: parent.width * 0.9
494+
onClicked: {
495+
if (imageViewer.scale - 0.02 > 0){
496+
imageViewer.scale = imageViewer.scale - 0.02;
497+
}else{
498+
imageViewer.scale = 0;
499+
}
500+
}
501+
}
502+
}
503+
}
504+
`;
505+
fs.writeFileSync(`${process.env['ydpSysRootPath']}/tmp/image.qml`,code);
506+
if ((url.startsWith('https://') || url.startsWith('http://'))){
507+
var req = {
508+
context: {
509+
url: url,
510+
ua: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
511+
cookie: '',
512+
refer: url
513+
},
514+
body: null,
515+
method: 'GET',
516+
header: {
517+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
518+
'Sec-Ch-Ua': '"Chromium";v="117", "Not)A;Brand";v="8", "Google Chrome";v="117"',
519+
'Sec-Ch-Ua-Mobile': '?0',
520+
'Sec-Ch-Ua-Platform': '"Linux"',
521+
'Sec-Fetch-Dest': 'document',
522+
'Sec-Fetch-Mode': 'navigate',
523+
'Sec-Fetch-Site': 'none',
524+
'Sec-Fetch-User': '?1',
525+
'Upgrade-Insecure-Requests': '1',
526+
'Accept-Language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
527+
'Cache-Control': 'max-age=0'
528+
}
529+
};
530+
var data = await fetch(req);
531+
if (data.body==null) throw 'not any data received';
532+
fs.writeFileSync(`${process.env['ydpSysRootPath']}/tmp/${id}.png`,await pic.ffmpegDecodeToPng(data.body,'pic'));
533+
}
534+
var imageViewer = child.spawn('chroot',['.','qml',`/tmp/image.qml`],{cwd:`${process.env['ydpSysRootPath']}`});
535+
var outTxt = '';
536+
imageViewer.on('close',(c)=>{
537+
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/${id}.png`);
538+
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/image.qml`);
539+
console.log('[viewPic] process exit with code',c,'.\n',outTxt);
540+
});
541+
imageViewer.on('error',(e)=>{
542+
console.log('[viewPic] err\n',e);
543+
});
544+
imageViewer.stdout.on('data',(d)=>{
545+
outTxt += String(d);
546+
});
547+
imageViewer.stderr.on('data',(d)=>{
548+
outTxt += String(d);
549+
});
409550
}

0 commit comments

Comments
 (0)