new(basic.gblib): FILL keyword can now template images and AS IMAGE can convert a DOCX to a PNG.

This commit is contained in:
rodrigorodriguez 2023-02-05 18:19:39 -03:00
parent d4845dc062
commit 8a5bdf3934
3 changed files with 1398 additions and 1394 deletions

View file

@ -364,7 +364,7 @@ export class GBVMService extends GBService {
sandbox['httpPs'] = ''; sandbox['httpPs'] = '';
sandbox['pid'] = pid; sandbox['pid'] = pid;
if (GBConfigService.get('VM3') === 'false') { if (GBConfigService.get('GBVM') === 'false') {
try { try {
const vm1 = new NodeVM({ const vm1 = new NodeVM({
allowAsync: true, allowAsync: true,

View file

@ -355,7 +355,7 @@ export class SystemKeywords {
const res = await client.api(`${baseUrl}/drive/root:/${tmpDocx}:/content?format=pdf`).get(); const res = await client.api(`${baseUrl}/drive/root:/${tmpDocx}:/content?format=pdf`).get();
await client.api(`${baseUrl}/drive/root:/${tmpDocx}:/content`).delete(); await client.api(`${baseUrl}/drive/root:/${tmpDocx}:/content`).delete();
const streamToString = stream => { const streamToBuffer = stream => {
const chunks = []; const chunks = [];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk)); stream.on('data', chunk => chunks.push(chunk));
@ -364,7 +364,7 @@ export class SystemKeywords {
}); });
}; };
gbfile.data = await streamToString(res); gbfile.data = await streamToBuffer(res);
// Converts the PDF to PNG. // Converts the PDF to PNG.
@ -1267,7 +1267,7 @@ export class SystemKeywords {
try { try {
const res = await client.api(`${baseUrl}/drive/root:/${srcPath}:/content?format=pdf`).get(); const res = await client.api(`${baseUrl}/drive/root:/${srcPath}:/content?format=pdf`).get();
const streamToString = stream => { const streamToBuffer = stream => {
const chunks = []; const chunks = [];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk)); stream.on('data', chunk => chunks.push(chunk));
@ -1276,7 +1276,7 @@ export class SystemKeywords {
}); });
}; };
const result = await streamToString(res); const result = await streamToBuffer(res);
await client.api(`${baseUrl}/drive/root:/${dstPath}:/content`).put(result); await client.api(`${baseUrl}/drive/root:/${dstPath}:/content`).put(result);
} catch (error) { } catch (error) {
@ -1387,27 +1387,27 @@ export class SystemKeywords {
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
const botId = this.min.instance.botId; const botId = this.min.instance.botId;
const gbaiName = `${botId}.gbai`; const gbaiName = `${botId}.gbai`;
const path = `/${botId}.gbai/${botId}.gbdata`; const path = `/${botId}.gbai/${botId}.gbdrive`;
// Downloads template from .gbdrive. // Downloads template from .gbdrive.
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(this.min); let { baseUrl, client } = await GBDeployer.internalGetDriveClient(this.min);
let template = await this.internalGetDocument(client, baseUrl, path, templateName); let template = await this.internalGetDocument(client, baseUrl, path, templateName);
let url = template['@microsoft.graph.downloadUrl']; let url = template['@microsoft.graph.downloadUrl'];
let localName = Path.join('work', gbaiName, 'cache', ``); const res = await fetch(url);
const response = await fetch(url); let localName = Path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.docx`);
Fs.writeFileSync(localName, Buffer.from(await response.arrayBuffer()), { encoding: null }); let buf = Buffer.from(await res.arrayBuffer());
Fs.writeFileSync(localName, buf, { encoding: null });
// Loads the file as binary content. // Loads the file as binary content.
let content = Fs.readFileSync(localName, 'binary'); let zip = new PizZip(buf);
let zip = new PizZip(content);
let doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true }); let doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
if (localName.endsWith('.pptx')) { if (localName.endsWith('.pptx')) {
doc.attachModule(pptxTemplaterModule); doc.attachModule(pptxTemplaterModule);
} }
// Replace image path on all elements of data.s // Replace image path on all elements of data.
const images = []; const images = [];
@ -1448,6 +1448,9 @@ export class SystemKeywords {
traverse(data, process); traverse(data, process);
doc.render(data); doc.render(data);
buf = doc.getZip().generate({ type: 'nodebuffer', compression: 'DEFLATE' });
Fs.writeFileSync(localName, buf, { encoding: null });
if (images) { if (images) {
// Replaces images within the document. // Replaces images within the document.

File diff suppressed because it is too large Load diff