Merge pull request #19 from houchaowei/main

fix: 网页截图接口(api/printscreen)并发请求时间戳重复问题
This commit is contained in:
Shawn Phang 2023-09-01 15:55:21 +08:00 committed by GitHub
commit 17593918c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -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~41
*/
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) => {

View 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)
);