feat(screenshot): download board

This commit is contained in:
ShawnPhang 2024-04-19 14:00:26 +08:00
parent 61ee995313
commit 3301d0ef97
3 changed files with 2039 additions and 4 deletions

View File

@ -1,3 +1,4 @@
## Node截图服务
目录结构比较简单,主要就实现了三个接口,其中 `api/screenshots` 即是项目中所使用到的图片生成接口,在真实生产项目中可以把该服务单独部署,于内网调用,这样利于做一些鉴权之类的处理。
@ -16,7 +17,7 @@ ERROR: Failed to set up Chromium xxx! Set "PUPPETEER_SKIP_DOWNLOAD" env variable
不用慌,这是因为 puppeteer 会自动下载 Chromium国内会受到网络波动的影响。
如果跳过的话需要手动安装,比较麻烦所以并不推荐。解决方法是多尝试几次,或者更换国内的镜像源即可。
如果跳过的话需要手动安装,比较麻烦所以并不推荐。解决方法是**多尝试几次,或者更换国内的镜像源**即可。
### 启动项目并热更新
@ -44,6 +45,12 @@ website // 编辑器项目的地址
filePath // 生成图片保存的目录
```
### 多线程集群
本服务中实现多任务操作使用的是队列的处理方式,保留了 JavaScript 单线程的特点,线程安全并且性能高效,下限低、更稳定,但在高配置机器上可能无法充分利用多核 CPU 资源。
如果你希望在配置更高的机器上创建多线程集群,可以尝试使用 [puppeteer-cluster](https://github.com/thomasdondorf/puppeteer-cluster)。
### 生成 API 文档
`build:apidoc`

View File

@ -3,7 +3,7 @@
* @Date: 2020-07-22 20:13:14
* @Description:
* @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2023-12-07 12:23:57
* @LastEditTime: 2024-04-16 15:43:29
*/
const { saveScreenshot } = require('../utils/download-single.ts')
const uuid = require('../utils/uuid.ts')
@ -55,8 +55,9 @@ module.exports = {
* @apiParam {String} type , file正常截图返回cover封面生成file
* @apiParam {String} size ,
* @apiParam {String} quality ,
* @apiParam {String|Number} index ,
*/
let { id, tempid, tempType, width, height, screenshot_url, type = 'file', size, quality } = req.query
let { id, tempid, tempType, width, height, screenshot_url, type = 'file', size, quality, index = 0 } = req.query
const url = (screenshot_url || drawLink) + `${id ? '?id=' : '?tempid='}`
id = id || tempid
const path = filePath + `${id}-screenshot.png`
@ -67,7 +68,7 @@ module.exports = {
res.json({ code: 200, msg: '服务器表示顶不住啊,等等再来吧~' })
return
}
const targetUrl = url + id + `${tempType?'&tempType='+tempType:''}`
const targetUrl = url + id + `${tempType?'&tempType='+tempType:''}` + `&index=${index}`
queueRun(saveScreenshot, targetUrl, { width, height, path, thumbPath, size, quality })
.then(() => {
res.setHeader('Content-Type', 'image/jpg')

2027
screenshot/yarn.lock Normal file

File diff suppressed because it is too large Load Diff