new(basic.gblib): SEND FILE pdf as temporary images.
This commit is contained in:
		
							parent
							
								
									25245a010e
								
							
						
					
					
						commit
						0b10992f3e
					
				
					 2 changed files with 46 additions and 35 deletions
				
			
		| 
						 | 
					@ -1453,41 +1453,14 @@ export class DialogKeywords {
 | 
				
			||||||
    const element = filename._page ? filename._page : filename.screenshot ? filename : null;
 | 
					    const element = filename._page ? filename._page : filename.screenshot ? filename : null;
 | 
				
			||||||
    let url;
 | 
					    let url;
 | 
				
			||||||
    let nameOnly;
 | 
					    let nameOnly;
 | 
				
			||||||
 | 
					    let localName;
 | 
				
			||||||
    const gbaiName = GBUtil.getGBAIPath(min.botId);
 | 
					    const gbaiName = GBUtil.getGBAIPath(min.botId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (filename.endsWith('.pdf')) {
 | 
					 | 
				
			||||||
      const gbdriveName = GBUtil.getGBAIPath(min.botId, 'gbdrive');
 | 
					 | 
				
			||||||
      const pdf = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, filename);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      const pngs = await GBUtil.pdfPageAsImage(min, pdf, undefined);
 | 
					 | 
				
			||||||
      
 | 
					 | 
				
			||||||
      await CollectionUtil.asyncForEach(pngs, async png => {
 | 
					 | 
				
			||||||
        await GBUtil.sleep(2000);
 | 
					 | 
				
			||||||
        // Prepare a cache to be referenced by Bot Framework.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const contentType = mime.lookup(url);
 | 
					 | 
				
			||||||
        const reply = { type: ActivityTypes.Message, text: caption };
 | 
					 | 
				
			||||||
        reply['attachments'] = [];
 | 
					 | 
				
			||||||
        reply['attachments'].push({
 | 
					 | 
				
			||||||
          name: nameOnly,
 | 
					 | 
				
			||||||
          contentType: contentType,
 | 
					 | 
				
			||||||
          contentUrl: url
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (channel === 'omnichannel'  || channel === 'whatsapp' || !user) {
 | 
					 | 
				
			||||||
          await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption, undefined, true);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          await min.conversationalService['sendOnConversation'](min, user, reply);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      });          
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Web automation.
 | 
					    // Web automation.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (element) {
 | 
					    if (element) {
 | 
				
			||||||
      const localName = path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
 | 
					      localName = path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
 | 
				
			||||||
      nameOnly = path.basename(localName);
 | 
					      nameOnly = path.basename(localName);
 | 
				
			||||||
      await element.screenshot({ path: localName, fullPage: true });
 | 
					      await element.screenshot({ path: localName, fullPage: true });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1539,16 +1512,50 @@ export class DialogKeywords {
 | 
				
			||||||
        const driveUrl = template['@microsoft.graph.downloadUrl'];
 | 
					        const driveUrl = template['@microsoft.graph.downloadUrl'];
 | 
				
			||||||
        const res = await fetch(driveUrl);
 | 
					        const res = await fetch(driveUrl);
 | 
				
			||||||
        let buf: any = Buffer.from(await res.arrayBuffer());
 | 
					        let buf: any = Buffer.from(await res.arrayBuffer());
 | 
				
			||||||
        let localName1 = path.join(
 | 
					        localName = path.join(
 | 
				
			||||||
          'work',
 | 
					          'work',
 | 
				
			||||||
          gbaiName,
 | 
					          gbaiName,
 | 
				
			||||||
          'cache',
 | 
					          'cache',
 | 
				
			||||||
          `${fileOnly.replace(/\s/gi, '')}-${GBAdminService.getNumberIdentifier()}.${ext}`
 | 
					          `${fileOnly.replace(/\s/gi, '')}-${GBAdminService.getNumberIdentifier()}.${ext}`
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        await fs.writeFile(localName1, buf, { encoding: null });
 | 
					        await fs.writeFile(localName, buf, { encoding: null });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName1));
 | 
					        url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      else {
 | 
				
			||||||
 | 
					        const gbdriveName = GBUtil.getGBAIPath(min.botId, 'gbdrive');
 | 
				
			||||||
 | 
					        localName = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, filename);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (localName.endsWith('.pdf')) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const pngs = await GBUtil.pdfPageAsImage(min, localName, undefined);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await CollectionUtil.asyncForEach(pngs, async png => {
 | 
				
			||||||
 | 
					          await GBUtil.sleepRandom();
 | 
				
			||||||
 | 
					          // Prepare a cache to be referenced by Bot Framework.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          const contentType = mime.lookup(url);
 | 
				
			||||||
 | 
					          const reply = { type: ActivityTypes.Message, text: caption };
 | 
				
			||||||
 | 
					          reply['attachments'] = [];
 | 
				
			||||||
 | 
					          reply['attachments'].push({
 | 
				
			||||||
 | 
					            name: nameOnly,
 | 
				
			||||||
 | 
					            contentType: contentType,
 | 
				
			||||||
 | 
					            contentUrl: url
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          if (channel === 'omnichannel' || channel === 'whatsapp' || !user) {
 | 
				
			||||||
 | 
					            await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption, undefined, true);
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            await min.conversationalService['sendOnConversation'](min, user, reply);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!url) {
 | 
					    if (!url) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -281,4 +281,8 @@ export class GBUtil {
 | 
				
			||||||
    return generatedFiles.length > 0 ? generatedFiles : null;
 | 
					    return generatedFiles.length > 0 ? generatedFiles : null;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static async sleepRandom(min = 1, max = 5) {
 | 
				
			||||||
 | 
					    const randomDelay = Math.floor(Math.random() * (max - min + 1) + min) * 1000; 
 | 
				
			||||||
 | 
					    await new Promise(resolve => setTimeout(resolve, randomDelay));
 | 
				
			||||||
 | 
					  }  
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue