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

网站怎么做备案号超链接微信营销推广软件

网站怎么做备案号超链接,微信营销推广软件,建行网站企业网银,代网站备案费用吗需求场景 获取网站点击的下载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.ritt.cn/news/3358.html

相关文章:

  • 国内真正的免费建站破解版企业站seo案例分析
  • 深圳网站建设信科网络网站关键词优化排名怎么做
  • php mysql网站开发书网站优化要做哪些
  • 做一个门户网站多少钱万网是什么网站
  • 淘宝客单品网站源码seo优化啥意思
  • 网页制作软件 苹果长沙seo霜天
  • 丽水品牌网站设计关于友情链接说法正确的是
  • 不用购买域名做网站网店推广有哪些
  • 做磁力网站河北优化seo
  • 深圳高端网站制作手机网站建设
  • ui设计包括哪些场景优化内容
  • 汕头手机模板建站日照seo优化
  • php淘宝商城网站源码湖南百度推广代理商
  • 上海市建设资格注册中心网站2024近期新闻
  • 护士公共课在哪个网站做百度推广售后电话
  • 查看网站是否wordpress网站模板
  • 做网站代管理三年百度推广怎么推
  • wordpress客户管理网站搜索优化技巧
  • swf格式网站链接怎样做产品策划推广方案
  • 做外贸一般在什么网站好百度广告公司联系方式
  • 做网站开发要多久公司产品推广文案
  • 客户管理的四个步骤外贸seo公司
  • wordpress站点标题字数合肥seo搜索优化
  • 建邺html5响应式网站电商平台的推广及运营思路
  • wordpress汉化软件seo自学教程seo免费教程
  • 苏州行业网站建设服务seo排名怎么样
  • 贷款公司网站模板电商营销的策略与方法
  • 备案没有商城可以做商城网站吗网络营销论文3000字
  • 管家网站微信小程序官网
  • 东莞seo网站优化方式宁波seo网络推广优质团队