Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
update textWeb
Browse files Browse the repository at this point in the history
  • Loading branch information
DSFdsfWxp committed Jul 27, 2024
1 parent eb0bd53 commit 916ccd3
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 10 deletions.
38 changes: 37 additions & 1 deletion ext/built-in/textweb/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,27 @@ async function newPage(Url, updateCb, doneCb, nowPageId, maxPageId) {
updateCb('[Processing content]', maxPageId, maxPageId, []);
try {
//pr = await sharedLib.process({ context: rr, opt: processPicOpt,svgOpt:processSvgPicOption , url: `${url.protocol}//${url.host}/${url.pathname}${url.search}` }, processServerHost);
let o = [];
extraData.pics.forEach(c=>{
if (c.startsWith('http://')){
// http://127.0.0.1:2233/url
let t = c.split('/');
if (t.length<4){
o.push(c);
}else{
o.push(sharedLib.encode.decode(t[3]));
}
}else{
o.push(c);
}
});
pr = {
title: rr.title,
txt: rr.txt,
suggest: rr.suggest,
links: extraData.links,
pics: extraData.pics
pics: extraData.pics,
rawPics: o
}
} catch (e) {
//console.log(e);
Expand Down Expand Up @@ -353,6 +368,25 @@ export class application extends extType.application.base {
doneCb();
return;
}
if ((/^p[0-9]+$/g).test(um.text)){
if (typeof __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.rawPics[parseInt(um.text.slice(1))] == 'string'){
updateCb(`[Loading pic]`, __classPrivateFieldGet(this, _application_nowPageId, "f"), __classPrivateFieldGet(this, _application_page, "f").length - 1, []);
try{
await sharedLib.viewPic(__classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.rawPics[parseInt(um.text.slice(1))]);
}catch(e){
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));
doneCb();
return;
}
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));
doneCb();
return;
}else{
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));
doneCb();
return;
}
}
if (!((/[^0-9]/g).test(um.text))) {
if (typeof __classPrivateFieldGet(this, _application_page, "f")[__classPrivateFieldGet(this, _application_nowPageId, "f")].processResult.links[parseInt(um.text)] == 'string') {
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);
Expand Down Expand Up @@ -474,6 +508,8 @@ export class application extends extType.application.base {
} catch (e) {
console.log('[picProcessServer] picProcessServerError', e);
try {
res.writeHead(200, 'OK', { 'content-type': 'image/png', 'content-length': `${picLoadFailTip.length}` });
res.write(picLoadFailTip);
res.end();
} catch (ee) { }
}
Expand Down
17 changes: 9 additions & 8 deletions ext/built-in/textweb/src/libPic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import * as http from "node:http";
import * as childProcess from "node:child_process";
import * as fs from "node:fs";

async function ffmpegDecodeToPng(data,extname){
export async function ffmpegDecodeToPng(data,extname){
return new Promise((resolve,reject)=>{
var fn = sharedLib.random.randomUUID();
fs.writeFileSync(`/ydpsys/tmp/${fn}.${extname}`,data);
var ffmpeg = childProcess.spawn('chroot',['.','ffmpeg','-i',`/tmp/${fn}.${extname}`,`/tmp/${fn}.png`],{cwd:'/ydpsys'});
fs.writeFileSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.${extname}`,data);
var ffmpeg = childProcess.spawn('chroot',['.','ffmpeg','-i',`/tmp/${fn}.${extname}`,`/tmp/${fn}.png`],{cwd:`${process.env['ydpSysRootPath']}`});
var outTxt = '';
ffmpeg.stdout.on('data',(d)=>{
outTxt += String(d);
Expand All @@ -24,13 +24,13 @@ async function ffmpegDecodeToPng(data,extname){
}else{
console.log('[libPic] ffmpeg exit with code',code,'. ');
}
if (!fs.existsSync(`/ydpsys/tmp/${fn}.png`)){
if (!fs.existsSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.png`)){
reject(outTxt);
return;
}
var res = fs.readFileSync(`/ydpsys/tmp/${fn}.png`);
fs.unlinkSync(`/ydpsys/tmp/${fn}.png`);
fs.unlinkSync(`/ydpsys/tmp/${fn}.${extname}`);
var res = fs.readFileSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.png`);
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.png`);
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/${fn}.${extname}`);
resolve(res);
});
});
Expand Down Expand Up @@ -139,6 +139,7 @@ export async function processPic(path) {
data = await ffmpegDecodeToPng(data,'pic');
img = await imageJs.Image.load(data);
}catch(ee){
console.log('[libPic] try ffmpeg failed. \n',ee);
return {
type: 'text/plain',
buff:Buffer.from(`${url}\n${JSON.stringify(opt)}\n${ee}`)
Expand Down Expand Up @@ -172,7 +173,7 @@ export async function processPic(path) {
type: 'image/png'
};
}
export async function process(Url, nowUrl, processServerHost, opt,svgOpt) {
export async function processLagacy(Url, nowUrl, processServerHost, opt,svgOpt) {
return new Promise((resolve, reject) => {
try {
/*
Expand Down
143 changes: 142 additions & 1 deletion ext/built-in/textweb/src/sharedLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as random from "./libRandom.js";
import * as zlib from "node:zlib";
import * as fs from "node:fs";
import * as dns from "node:dns";
import * as child from "node:child_process";

export {md,pic,encode,random};
const html2mdFix = html2md.default;
Expand Down Expand Up @@ -214,7 +215,7 @@ export async function lagacyProcess(input, processServerHost) {
break;
}
case 'pic': {
let o = await pic.process(c.data, input.url, processServerHost, input.opt,input.svgOpt);
let o = await pic.processLagacy(c.data, input.url, processServerHost, input.opt,input.svgOpt);
if (!pics.includes(o[0].data)) pics.push(o[0].data);
out = out.concat(o);
break;
Expand Down Expand Up @@ -406,4 +407,144 @@ export function loadCookie(host,cookieDir){
//thost.shift();
}
return out;
}

export async function viewPic(url){
console.log('[viewPic] view',url);
var id = random.randomUUID();
var fn = (url.startsWith('https://') || url.startsWith('http://')) ? `file:///tmp/${id}.png` : url;
var code = `
import QtQuick 2.15
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.15
ApplicationWindow {
id: root
visible: true
width: 320
height: 170
title: "Image Viewer"
color: "black"
flags: Qt.FramelessWindowHint
Rectangle {
id: imageArea
color: "black"
width: parent.width*0.9
height: parent.height
x: parent.width*0.1
y:0
Image {
id: imageViewer
x:0
y:0
scale: 1
source: "${fn}"
fillMode: Image.PreserveAspectFit
}
MouseArea {
id: dragArea
anchors.fill: parent
drag.target: imageViewer
drag.axis: Drag.XAndYAxis
}
}
Rectangle {
color: "black"
width: parent.width*0.1
height: parent.height
Button {
text: "×"
x: parent.width * 0.05
y: parent.width * 0.05
width: parent.width * 0.9
height: parent.width * 0.9
onClicked: Qt.quit()
}
Button {
text: "C"
x: parent.width * 0.05
y: parent.height - parent.width * 0.05*3 - parent.width * 0.9*3
width: parent.width * 0.9
height: parent.width * 0.9
onClicked: {
imageViewer.x = imageViewer.width * (imageViewer.scale-1) / 2;
imageViewer.y = imageViewer.height * (imageViewer.scale-1) / 2;
}
}
Button {
text: "+"
x: parent.width * 0.05
y: parent.height - parent.width * 0.05*2 - parent.width * 0.9*2
width: parent.width * 0.9
height: parent.width * 0.9
onClicked: {
imageViewer.scale = imageViewer.scale + 0.02;
}
}
Button {
text: "-"
x: parent.width * 0.05
y: parent.height - parent.width * 0.05 - parent.width * 0.9
width: parent.width * 0.9
height: parent.width * 0.9
onClicked: {
if (imageViewer.scale - 0.02 > 0){
imageViewer.scale = imageViewer.scale - 0.02;
}else{
imageViewer.scale = 0;
}
}
}
}
}
`;
fs.writeFileSync(`${process.env['ydpSysRootPath']}/tmp/image.qml`,code);
if ((url.startsWith('https://') || url.startsWith('http://'))){
var req = {
context: {
url: url,
ua: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
cookie: '',
refer: url
},
body: null,
method: 'GET',
header: {
'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',
'Sec-Ch-Ua': '"Chromium";v="117", "Not)A;Brand";v="8", "Google Chrome";v="117"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Linux"',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'Accept-Language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
'Cache-Control': 'max-age=0'
}
};
var data = await fetch(req);
if (data.body==null) throw 'not any data received';
fs.writeFileSync(`${process.env['ydpSysRootPath']}/tmp/${id}.png`,await pic.ffmpegDecodeToPng(data.body,'pic'));
}
var imageViewer = child.spawn('chroot',['.','qml',`/tmp/image.qml`],{cwd:`${process.env['ydpSysRootPath']}`});
var outTxt = '';
imageViewer.on('close',(c)=>{
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/${id}.png`);
fs.unlinkSync(`${process.env['ydpSysRootPath']}/tmp/image.qml`);
console.log('[viewPic] process exit with code',c,'.\n',outTxt);
});
imageViewer.on('error',(e)=>{
console.log('[viewPic] err\n',e);
});
imageViewer.stdout.on('data',(d)=>{
outTxt += String(d);
});
imageViewer.stderr.on('data',(d)=>{
outTxt += String(d);
});
}

0 comments on commit 916ccd3

Please sign in to comment.