当前位置: 首页 > news >正文

使用puppeteer完成监听浏览器下载文件并保存到自己本地或服务器上完成上传功能

需求场景

获取网站点击的下载pdf,并把pdf重命名再上传到COS云上面

技术使用

“puppeteer”: “^19.7.2”,
“egg”: “^3.15.0”, // 服务期用egg搭的
文件服务使用COS腾讯云

核心思路

获取浏览器下载事件,并把文件保存到本地

const session = await substitutePage.target().createCDPSession();await session.send('Page.setDownloadBehavior', {behavior: 'allow',downloadPath, // 指定文件保存路径回家});

在保存到本地前监听此文件夹,如果有文件则获取并上传
加timer做防抖是为了防止在文件写入时以及重命名文件时多次触发watch函数,导致出会出现0KB源文件脏数据

 let timer: any = null;fs.watch(downloadPath, async (_eventType, filename) => {if (timer !== null) {clearTimeout(timer);}timer = setTimeout(() => {// 防止出现下载的临时文件就触发if (filename.endsWith('.pdf')) {resolve({filename,});}}, 500);});

完整代码

    const session = await substitutePage.target().createCDPSession();await session.send('Page.setDownloadBehavior', {behavior: 'allow',downloadPath, // 指定文件保存路径回家});// res就是文件相关信息了const [ res ] = await this.downloadPdfHandler(substitutePage, downloadPath);// filePath就是自己本地的文件所在绝对路径const filePath = `${downloadPath}/${res.fileName}`;// uploadFile是cos文件上传相关实现, 我就不放了,有私密的keyconst pdfUriCode = await this.uploadFile(filePath, filePath);const pdfUri = decodeURIComponent(pdfUriCode);this.domainList = {pdfSize: res.pdfSize,pdfUri: pdfUri.substring(pdfUri.indexOf('root')),};

downloadPdfHandler函数实现

  downloadPdfHandler(page, downloadPath): Promise<any> {const uuidName = uuidv4();const fsWatchApi = () => {// 使用防抖是为了防止下载的文件没有写入完全就重命名,那样会产生一个脏文件let timer: any = null;return new Promise<{ filename: string }>(resolve => {fs.watch(downloadPath, async (_eventType, filename) => {if (timer !== null) {clearTimeout(timer);}timer = setTimeout(() => {// 防止出现下载的临时文件就触发if (filename.endsWith('.pdf')) {resolve({filename,});}}, 500);});});};function responseWatchApi() {return new Promise<void>(resolve => {page.on('response', async response => {// 检查响应是否为application/octet-stream且可能包含PDF(或你期望的其他文件类型)if (response.headers()['content-type'].startsWith('application/octet-stream')) {resolve();}});});}return new Promise(async (resolve, reject) => {try {const [ , { filename }] = await Promise.all([ responseWatchApi(), fsWatchApi() ]);const oldFilePath = path.join(downloadPath, filename);const newFilePath = path.join(downloadPath, `${uuidName}.pdf`);try {fs.renameSync(oldFilePath, newFilePath);this.logger.info(`文件名已经被修改完成:${uuidName}`);} catch (error) {this.logger.info(`文件名已经被修改完成:${uuidName}`);}await this.sleep(5 * 1000);const files = fs.readdirSync(downloadPath);// 创建一个数组,将文件名和其mtime(最后修改时间)一起存储const filesWithMtime = files.map(file => {const filePath = path.join(downloadPath, file);const stats = fs.statSync(filePath);return { fileName: file, mtime: stats.mtime, size: stats.size };});const newestFile = filesWithMtime.sort((a, b) => b.mtime.getTime() - a.mtime.getTime())[0];this.logger.info('newestFile: %o', {newestFile,});resolve({pdfSize: newestFile.size,fileName: newestFile.fileName,});} catch (e) {reject(e);}});}
http://www.lryc.cn/news/304564.html

相关文章:

  • 软件压力测试:测试方法与步骤详解
  • Oerlikon欧瑞康LPCVD system操作使用说明
  • pyspark统计指标计算
  • 2.22号qt
  • $attrs
  • OS X(MACOS) C/C++ 遍历系统所有的IP路由表配置。
  • 人工智能_普通服务器CPU_安装清华开源人工智能AI大模型ChatGlm-6B_003---人工智能工作笔记0098
  • 基于JAVA的实验室耗材管理系统 开源项目
  • NXP实战笔记(七):S32K3xx基于RTD-SDK在S32DS上配置ICU输入捕获
  • 左右联动布局效果
  • 【工具类】vscode ssh 远程免密登录开发
  • 【Antd】Form 表单获取不到 Input 的值
  • Encoder-decoder 与Decoder-only 模型之间的使用区别
  • 【STM32备忘录】【STM32WB系列的BLE低功耗蓝牙】一、测试广播配置搜不到信号的注意事项
  • ChatGPT 是什么
  • 4款好用的ai智能写作软件,为写作排忧解难!
  • js设计模式:计算属性模式
  • 2015-2024年考研数学(一)真题练习和解析——选择题
  • Git合并固定分支的某一部分至当前分支
  • Codeforces Round 928 (Div. 4) (A-E)
  • git远程操控gitee
  • 常见面试题:TCP的四次挥手和TCP的滑动窗口
  • 力扣随笔之两数之和 Ⅱ -输入有序数组(中等167)
  • 最优传输(Optimal Transport)
  • MIT-6.824-Lab2,Raft部分笔记|Use Go
  • 使用openeuler 22.03替代CentOS 7.9,建立虚拟机详细步骤
  • 代理技术引领出海征程
  • 谷粒商城篇章9 ---- P248-P261/P292-P294 ---- 消息队列【分布式高级篇六】
  • 【Spring连载】使用Spring Data访问 MongoDB(五)----生命周期事件
  • JavaSec 之 SQL 注入简单了解