From 32d8578b3bcb65885a294758d1cc9dc40c1f3327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=AF=E8=B6=85=E5=A7=94?= Date: Fri, 1 Sep 2023 15:06:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B9=B6=E5=8F=91=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=A4=9A=E6=97=B6,=E6=97=B6=E9=97=B4=E6=88=B3=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E9=97=AE=E9=A2=98=20&=20ab=E6=B5=8B=E8=AF=95=E5=A4=9A?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E8=AF=B7=E6=B1=82=E6=97=B6failed=20request?= =?UTF-8?q?=E6=95=B0=E8=BF=87=E5=A4=9A=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- screenshot/src/service/screenshots.ts | 13 ++++++++----- screenshot/src/utils/uuid.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 screenshot/src/utils/uuid.ts diff --git a/screenshot/src/service/screenshots.ts b/screenshot/src/service/screenshots.ts index d1f8677..f0c26e3 100644 --- a/screenshot/src/service/screenshots.ts +++ b/screenshot/src/service/screenshots.ts @@ -2,10 +2,11 @@ * @Author: ShawnPhang * @Date: 2020-07-22 20:13:14 * @Description: - * @LastEditors: ShawnPhang - * @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) => { diff --git a/screenshot/src/utils/uuid.ts b/screenshot/src/utils/uuid.ts new file mode 100644 index 0000000..ca5edc7 --- /dev/null +++ b/screenshot/src/utils/uuid.ts @@ -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) + );