mirror of
https://github.com/palxiao/poster-design.git
synced 2025-07-15 16:02:19 +08:00
Merge pull request #19 from houchaowei/main
fix: 网页截图接口(api/printscreen)并发请求时间戳重复问题
This commit is contained in:
commit
17593918c9
@ -2,10 +2,11 @@
|
||||
* @Author: ShawnPhang
|
||||
* @Date: 2020-07-22 20:13:14
|
||||
* @Description:
|
||||
* @LastEditors: ShawnPhang <site: book.palxp.com>
|
||||
* @LastEditTime: 2023-07-17 18:03:13
|
||||
* @LastEditors: 侯超委 houchaowei@zhihu.com
|
||||
* @LastEditTime: 2023-09-01 14:51:23
|
||||
*/
|
||||
const { saveScreenshot } = require('../utils/download-single.ts')
|
||||
const uuid = require('../utils/uuid.ts')
|
||||
const { filePath, upperLimit } = require('../configs.ts')
|
||||
const { queueRun, queueList } = require('../utils/node-queue.ts')
|
||||
// const path = require('path')
|
||||
@ -102,11 +103,11 @@ module.exports = {
|
||||
* @apiParam {Number} scale (可选) 针对移动端的设备像素比(DPR) 整型范围 1~4,默认1
|
||||
*/
|
||||
let { width = 375, height = 0, url, type = 'file', size, quality, prevent = false, ua, devices, scale, wait } = req.query
|
||||
const path = filePath + `screenshot_${new Date().getTime()}.png`
|
||||
const path = filePath + `screenshot_${new Date().getTime()}_${uuid()}.png`
|
||||
const thumbPath = type === 'cover' ? path.replace('.png', '.jpg') : null
|
||||
|
||||
if (url) {
|
||||
const sign = new Date().getTime() + ''
|
||||
const sign = `${new Date().getTime()}_${uuid(8)}`
|
||||
req._queueSign = sign
|
||||
// console.log(url + id, path, thumbPath);
|
||||
if (queueList.length > upperLimit) {
|
||||
@ -119,7 +120,9 @@ module.exports = {
|
||||
res.setHeader('Content-Type', 'image/jpg')
|
||||
// const stats = fs.statSync(path)
|
||||
// res.setHeader('Cache-Control', stats.size)
|
||||
type === 'file' ? res.sendFile(path) : res.sendFile(thumbPath)
|
||||
res.json({ code: 200, msg: '截图成功', data: { path, thumbPath } })
|
||||
} else {
|
||||
res.json({ code: 200, msg: 'ok' })
|
||||
}
|
||||
})
|
||||
.catch((e: any) => {
|
||||
|
16
screenshot/src/utils/uuid.ts
Normal file
16
screenshot/src/utils/uuid.ts
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* @Author: 侯超委 houchaowei@zhihu.com
|
||||
* @Date: 2023-09-01 14:33:23
|
||||
* @LastEditors: 侯超委 houchaowei@zhihu.com
|
||||
* @LastEditTime: 2023-09-01 14:56:38
|
||||
* @FilePath: /poster-design/screenshot/src/utils/uuid.ts
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
*/
|
||||
|
||||
const nodeCrypto = require('crypto');
|
||||
|
||||
module.exports = () =>
|
||||
// @ts-ignore
|
||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c: number) =>
|
||||
(c ^ (nodeCrypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user