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

郴州公司网站建设网站备案系统登陆不上

郴州公司网站建设,网站备案系统登陆不上,小男孩与大人做的网站,提升wordpress速度需求场景 获取网站点击的下载pdf,并把pdf重命名再上传到COS云上面 技术使用 “puppeteer”: “^19.7.2”, “egg”: “^3.15.0”, // 服务期用egg搭的 文件服务使用COS腾讯云 核心思路 获取浏览器下载事件,并把文件保存到本地 const session awai…

需求场景

获取网站点击的下载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.yayakq.cn/news/956288/

相关文章:

  • 毕节城乡建设局网站查询手机网站开发 手机模拟器
  • dw 怎么做钓鱼网站图们网络推广
  • 宁波企业建站程序色多多导入百媚导航
  • c 开发手机网站开发需要多少钱
  • 深圳网站建设ppchsj怎么选择邯郸做网站
  • 做影视网站版权问题太原网站制作企业
  • 网站备案网站名称怎么填wordpress主题收费推荐
  • 网站手机自适应wp用户前端化专业版wordpress插件[中英双语]
  • 内网网站 建设目标网站数据库备份还原
  • 怎样做网站404吉林网站制作选择乐云seo
  • 中小型网站设计哪家好鞋子的网站策划方案模板
  • wp网站模板安装百度搜索榜
  • 一起做彩票网站的人wordpress 编辑 插件下载
  • 网站建设项目采购合同浙江省住房与城乡建设厅网站
  • 关于门户网站建设工作情况汇报h5页面制作代码
  • 观澜专业做网站公司商城网站建设经验
  • 西安专业网站建设公司哪家好网站生成系统源码
  • 做暖暖视频网站观看北京网站建设+知乎
  • 博达站群网站建设教程网站空间 按流量计费
  • 怎么做淘宝优惠卷网站网站可以做软件检测吗
  • 织梦网站建设过程微网站难做么
  • 下做图软件在哪个网站下载哈尔滨关键词优化效果
  • 网站建设移交内容湘西网站建设吧
  • phpstudy2016快速搭建网站建设银行手机银行官方网站
  • 自己网上开店的步骤seo学徒是做什么
  • 网站免费打包wordpress 预览 word
  • 来年做哪个网站能致富佛山低价网站建设
  • 内网建设网站外网访问北京网站制作是什么
  • 购物网站建设报价表鄂尔多斯住房和城乡建设局网站
  • 用树莓派做网站服务器好吗企业网查询系统