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

网站建设价格制定的方法东游科技网站建设

网站建设价格制定的方法,东游科技网站建设,江西网站建设,旅游网页图片素材需求场景 获取网站点击的下载pdf#xff0c;并把pdf重命名再上传到COS云上面 技术使用 “puppeteer”: “^19.7.2”, “egg”: “^3.15.0”, // 服务期用egg搭的 文件服务使用COS腾讯云 核心思路 获取浏览器下载事件#xff0c;并把文件保存到本地 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): Promiseany {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 Promisevoid(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.zqtcl.cn/news/380395/

相关文章:

  • 怎么做网站源码wordpress的rss
  • wordpress能不能做企业网站软件技术和计算机网络技术哪个好
  • 甘肃省住房和城乡建设部网站首页ip怎么做网站
  • 怎么开一家网站开发公司百度推广一年大概需要多少钱
  • 小破站下载h5企业模板网站
  • 服务器怎么设置ip做网站凌云seo博客
  • 莱芜四大金刚是谁啊镇江网站优化推广
  • 上海门户网站开发企业号码查询系统
  • 西安做网站设计的公司golang 网站开发 教程
  • 做网站哪些公司专业做app软件开发公司
  • 蒙特网站建设湖北省建设厅网站上岗证查询
  • 宁波网站建设 联系哪家电子商务网站建设过程范文
  • 南宁商城网站建设网站建设的需求文档
  • dedeampz 部署wordpress 网站访问慢如何评价网站是否做的好处
  • 怎样建设个人影视网站设计学专业
  • 没有公司 接单做网站网站建设加盟合作
  • 如何将域名和网站绑定做网站找投资人
  • 网站开发 平台WordPress首页可见
  • 沧州做网站费用打开上海发布
  • 重庆潼南网站建设公司电话网站能调用一些字体
  • 摄影网站设计素材做彩票网站电话多少
  • 开网站公司企业管理网课
  • 相城高端网站建设施工建设集团网站
  • .电子商务网站的开发原则包括网络服务示范区创建情况
  • 网站如何做权重php做网站登陆验证
  • 昆山制造网站的地方网站建设 有聊天工具的吗
  • 自己做网站制作需要多少钱如何免费注册网站域名
  • 如何做网站美化怎样写网站文案
  • 做网站排名的wordpress 调整 行距
  • 三亚文明城市建设服务中心报名网站房地产活动策划网站