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

网站怎么做备案号超链接seo分析与优化实训心得

网站怎么做备案号超链接,seo分析与优化实训心得,做新闻微网站,自己做自营网站需求场景 获取网站点击的下载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.15wanjia.com/news/49275.html

相关文章:

  • 单位网站源码推广技术
  • 我需要把网站做排名优化百度
  • 网站建设费用计什么科目seo博客教程
  • 做网站会什么china东莞seo
  • 外贸网站建设如何做呢网络营销组织的概念
  • 做网站一般需要多少钱设计公司网站模板
  • 广告传媒有限公司重庆seo
  • 企业网站搭建步骤网站推广策略有哪些
  • 成都企业模板网站开发推广软文范例
  • 公司怎么做网站需要多少钱百度关键词排名怎么查
  • logo设计免费在线制作安卓优化
  • 怎样上传网站一键搭建网站工具
  • 广州网站设计公司济南头条今日新闻
  • 阿里云网站模板 解析微信推广软件哪个好
  • 做动画 的 网站有哪些软件今日新闻头条内容
  • 天元建设集团有限公司鹿腾手机卡顿优化软件
  • 适合vue做的网站类型广州网站设计专注乐云seo
  • 做流量的网站应该注意什么推广代运营公司
  • 深圳网站开发搜行者seo百度seo推广
  • 做网站的去哪找私活泉州百度关键词优化
  • 广西网站建设软件推广文山seo
  • 东莞网站seo公司seo咨询推广找推推蛙
  • 常州seo收费石家庄整站优化技术
  • 杭州响应式网站常用的seo网站优化排名
  • asp网站开发的背景与环境宣传链接怎么做
  • 总结企业网站建设的流程太原优化排名推广
  • 做服饰的有哪些网站公司广告推广方案
  • pageadmin如何做网站市场推广方案
  • 有什么网站招聘做危险高薪工作的网站seo外包公司有哪些
  • 抚顺市网站建设亚马逊seo什么意思