refactor: UUID

This commit is contained in:
手瓜一十雪 2024-07-24 10:44:55 +08:00
parent 37bd51e138
commit f784363696
5 changed files with 8 additions and 10 deletions

View File

@ -33,7 +33,6 @@
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",
"@types/node": "^20.11.30", "@types/node": "^20.11.30",
"@types/qrcode-terminal": "^0.12.2", "@types/qrcode-terminal": "^0.12.2",
"@types/uuid": "^10.0.0",
"@types/ws": "^8.5.10", "@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^7.4.0", "@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0", "@typescript-eslint/parser": "^7.4.0",
@ -66,7 +65,6 @@
"log4js": "^6.9.1", "log4js": "^6.9.1",
"qrcode-terminal": "^0.12.0", "qrcode-terminal": "^0.12.0",
"silk-wasm": "^3.6.1", "silk-wasm": "^3.6.1",
"uuid": "^10.0.0",
"ws": "^8.16.0" "ws": "^8.16.0"
} }
} }

View File

@ -3,7 +3,7 @@ import { encode, getDuration, getWavFileInfo, isWav, isSilk } from 'silk-wasm';
import fsPromise from 'fs/promises'; import fsPromise from 'fs/promises';
import { log, logError } from './log'; import { log, logError } from './log';
import path from 'node:path'; import path from 'node:path';
import { v4 as uuidv4 } from 'uuid'; import { randomUUID } from 'crypto';
import { spawn } from 'node:child_process'; import { spawn } from 'node:child_process';
import { getTempDir } from '@/common/utils/file'; import { getTempDir } from '@/common/utils/file';
@ -64,7 +64,7 @@ export async function encodeSilk(filePath: string) {
try { try {
const file = await fsPromise.readFile(filePath); const file = await fsPromise.readFile(filePath);
const pttPath = path.join(TEMP_DIR, uuidv4()); const pttPath = path.join(TEMP_DIR, randomUUID());
if (!isSilk(file)) { if (!isSilk(file)) {
log(`语音文件${filePath}需要转换成silk`); log(`语音文件${filePath}需要转换成silk`);
const _isWav = isWav(file); const _isWav = isWav(file);

View File

@ -5,7 +5,7 @@ import util from 'util';
import path from 'node:path'; import path from 'node:path';
import { log, logError } from './log'; import { log, logError } from './log';
import * as fileType from 'file-type'; import * as fileType from 'file-type';
import { v4 as uuidv4 } from 'uuid'; import { randomUUID } from 'crypto';
import { napCatCore } from '@/core'; import { napCatCore } from '@/core';
export const getNapCatDir = () => { export const getNapCatDir = () => {
@ -191,7 +191,7 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr
isLocal: false isLocal: false
}; };
if (!fileName) { if (!fileName) {
fileName = uuidv4(); fileName = randomUUID();
} }
let filePath = path.join(getTempDir(), fileName); let filePath = path.join(getTempDir(), fileName);
let url = null; let url = null;
@ -234,7 +234,7 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr
} }
fileName = fileName.replace(/[/\\:*?"<>|]/g, '_'); fileName = fileName.replace(/[/\\:*?"<>|]/g, '_');
res.fileName = fileName; res.fileName = fileName;
filePath = path.join(getTempDir(), uuidv4() + fileName); filePath = path.join(getTempDir(), randomUUID() + fileName);
fs.writeFileSync(filePath, buffer); fs.writeFileSync(filePath, buffer);
} catch (e: any) { } catch (e: any) {
res.errMsg = `${url}下载失败,` + e.toString(); res.errMsg = `${url}下载失败,` + e.toString();

View File

@ -1,7 +1,7 @@
import os from 'node:os'; import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import { networkInterfaces } from 'os'; import { networkInterfaces } from 'os';
import { v4 as uuidv4 } from 'uuid'; import { randomUUID } from 'crypto';
// 缓解Win7设备兼容性问题 // 缓解Win7设备兼容性问题
let osName: string; let osName: string;
@ -30,7 +30,7 @@ export async function getMachineId(): Promise<string> {
if (!machineId) { if (!machineId) {
machineId = (async () => { machineId = (async () => {
const id = await getMacMachineId(); const id = await getMacMachineId();
return id || uuidv4(); // fallback, generate a UUID return id || randomUUID(); // fallback, generate a UUID
})(); })();
} }

View File

@ -10,7 +10,7 @@ import fs from 'node:fs';
import babel from 'vite-plugin-babel'; import babel from 'vite-plugin-babel';
import { version } from 'os'; import { version } from 'os';
// "@rollup/plugin-babel": "^6.0.4", // "@rollup/plugin-babel": "^6.0.4",
const external = ['silk-wasm', 'ws', 'express', 'uuid', 'fluent-ffmpeg', 'log4js', 'qrcode-terminal']; const external = ['silk-wasm', 'ws', 'express', 'fluent-ffmpeg', 'log4js', 'qrcode-terminal'];
const nodeModules = [...builtinModules, builtinModules.map(m => `node:${m}`)].flat(); const nodeModules = [...builtinModules, builtinModules.map(m => `node:${m}`)].flat();
// let nodeModules = ["fs", "path", "events", "buffer", "url", "crypto", "fs/promise", "fsPromise", "os", "http", "net"] // let nodeModules = ["fs", "path", "events", "buffer", "url", "crypto", "fs/promise", "fsPromise", "os", "http", "net"]