BUILD: TEST

This commit is contained in:
手瓜一十雪
2024-09-11 23:18:38 +08:00
parent a9f5069649
commit 95bd74bb0d
8 changed files with 36 additions and 50 deletions

View File

@@ -160,8 +160,7 @@ type Uri2LocalRes = {
errMsg: string, errMsg: string,
fileName: string, fileName: string,
ext: string, ext: string,
path: string, path: string
isLocal: boolean
} }
export async function checkFileV2(filePath: string) { export async function checkFileV2(filePath: string) {
@@ -194,7 +193,6 @@ export async function checkUriType(Uri: string) {
return undefined; return undefined;
}, Uri); }, Uri);
if (LocalFileRet) return LocalFileRet; if (LocalFileRet) return LocalFileRet;
const OtherFileRet = await solveProblem((uri: string) => { const OtherFileRet = await solveProblem((uri: string) => {
//再判断是否是Http //再判断是否是Http
if (uri.startsWith('http://') || uri.startsWith('https://')) { if (uri.startsWith('http://') || uri.startsWith('https://')) {
@@ -206,13 +204,13 @@ export async function checkUriType(Uri: string) {
} }
if (uri.startsWith('file://')) { if (uri.startsWith('file://')) {
let filePath: string; let filePath: string;
// await fs.copyFile(url.pathname, filePath);
const pathname = decodeURIComponent(new URL(uri).pathname); const pathname = decodeURIComponent(new URL(uri).pathname);
if (process.platform === 'win32') { if (process.platform === 'win32') {
filePath = pathname.slice(1); filePath = pathname.slice(1);
} else { } else {
filePath = pathname; filePath = pathname;
} }
return { Uri: filePath, Type: FileUriType.Local }; return { Uri: filePath, Type: FileUriType.Local };
} }
if (uri.startsWith('data:')) { if (uri.startsWith('data:')) {
@@ -228,20 +226,26 @@ export async function checkUriType(Uri: string) {
export async function uri2local(dir: string, uri: string, filename: string | undefined = undefined): Promise<Uri2LocalRes> { export async function uri2local(dir: string, uri: string, filename: string | undefined = undefined): Promise<Uri2LocalRes> {
const { Uri: HandledUri, Type: UriType } = await checkUriType(uri); const { Uri: HandledUri, Type: UriType } = await checkUriType(uri);
//解析失败 //解析失败
const tempName = randomUUID();
if (!filename) filename = randomUUID();
//解析Http和Https协议
if (UriType == FileUriType.Unknown) { if (UriType == FileUriType.Unknown) {
return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '', isLocal: false }; return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '' };
} }
//解析File协议和本地文件 //解析File协议和本地文件
if (UriType == FileUriType.Local) { if (UriType == FileUriType.Local) {
const fileExt = path.extname(HandledUri); const fileExt = path.extname(HandledUri);
let filename = path.basename(HandledUri, fileExt); let filename = path.basename(HandledUri, fileExt);
filename += fileExt; filename += fileExt;
return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: HandledUri, isLocal: true }; //复制文件到临时文件并保持后缀
const filenameTemp = tempName + fileExt;
const filePath = path.join(dir, filenameTemp);
fs.copyFileSync(HandledUri, filePath);
console.log('复制文件到临时文件', HandledUri, filePath);
return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath };
} }
//接下来都要有文件名 //接下来都要有文件名
if (!filename) filename = randomUUID();
//解析Http和Https协议
if (UriType == FileUriType.Remote) { if (UriType == FileUriType.Remote) {
const pathInfo = path.parse(decodeURIComponent(new URL(HandledUri).pathname)); const pathInfo = path.parse(decodeURIComponent(new URL(HandledUri).pathname));
@@ -253,10 +257,10 @@ export async function uri2local(dir: string, uri: string, filename: string | und
} }
filename = filename.replace(/[/\\:*?"<>|]/g, '_'); filename = filename.replace(/[/\\:*?"<>|]/g, '_');
const fileExt = path.extname(HandledUri); const fileExt = path.extname(HandledUri);
const filePath = path.join(dir, filename); const filePath = path.join(dir, tempName + fileExt);
const buffer = await httpDownload(HandledUri); const buffer = await httpDownload(HandledUri);
fs.writeFileSync(filePath, buffer); fs.writeFileSync(filePath, buffer);
return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath, isLocal: false }; return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath };
} }
//解析Base64 //解析Base64
if (UriType == FileUriType.Base64) { if (UriType == FileUriType.Base64) {
@@ -271,7 +275,7 @@ export async function uri2local(dir: string, uri: string, filename: string | und
fileExt = ext; fileExt = ext;
filename = filename + '.' + ext; filename = filename + '.' + ext;
} }
return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath, isLocal: false }; return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath };
} }
return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '', isLocal: false }; return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '' };
} }

View File

@@ -71,7 +71,7 @@ export class NTQQFileApi {
file_uuid: '', file_uuid: '',
}); });
await this.copyFile(filePath, mediaPath!); await this.copyFile(filePath, mediaPath);
const fileSize = await this.getFileSize(filePath); const fileSize = await this.getFileSize(filePath);
return { return {
md5: fileMd5, md5: fileMd5,

View File

@@ -1,4 +1,4 @@
import { GroupMemberRole, Peer } from '@/core'; import { GroupMemberRole } from '@/core';
export interface Peer { export interface Peer {
chatType: ChatType; chatType: ChatType;

View File

@@ -19,26 +19,21 @@ export class OCRImage extends BaseAction<Payload, any> {
payloadSchema = SchemaData; payloadSchema = SchemaData;
async _handle(payload: Payload) { async _handle(payload: Payload) {
const { path, isLocal, success } = (await uri2local(this.core.NapCatTempPath, payload.image)); const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.image));
if (!success) { if (!success) {
throw `OCR ${payload.image}失败,image字段可能格式不正确`; throw `OCR ${payload.image}失败,image字段可能格式不正确`;
} }
if (path) { if (path) {
await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断 await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断
const ret = await this.core.apis.SystemApi.ocrImage(path); const ret = await this.core.apis.SystemApi.ocrImage(path);
if (!isLocal) { fs.unlink(path, () => { });
fs.unlink(path, () => {
});
}
if (!ret) { if (!ret) {
throw `OCR ${payload.file}失败`; throw `OCR ${payload.file}失败`;
} }
return ret.result; return ret.result;
} }
if (!isLocal) { fs.unlink(path, () => { });
fs.unlink(path, () => {
});
}
throw `OCR ${payload.file}失败,文件可能不存在`; throw `OCR ${payload.file}失败,文件可能不存在`;
} }
} }

View File

@@ -24,17 +24,16 @@ export default class SetAvatar extends BaseAction<Payload, null> {
} }
async _handle(payload: Payload): Promise<null> { async _handle(payload: Payload): Promise<null> {
const { path, isLocal, success } = (await uri2local(this.core.NapCatTempPath, payload.file)); const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.file));
if (!success) { if (!success) {
throw `头像${payload.file}设置失败,file字段可能格式不正确`; throw `头像${payload.file}设置失败,file字段可能格式不正确`;
} }
if (path) { if (path) {
await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断 await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断
const ret = await this.core.apis.UserApi.setQQAvatar(path); const ret = await this.core.apis.UserApi.setQQAvatar(path);
if (!isLocal) {
fs.unlink(path, () => { fs.unlink(path, () => {
}); });
}
if (!ret) { if (!ret) {
throw `头像${payload.file}设置失败,api无返回`; throw `头像${payload.file}设置失败,api无返回`;
} }
@@ -45,10 +44,8 @@ export default class SetAvatar extends BaseAction<Payload, null> {
throw `头像${payload.file}设置失败,未知的错误,${ret['result']}:${ret['errMsg']}`; throw `头像${payload.file}设置失败,未知的错误,${ret['result']}:${ret['errMsg']}`;
} }
} else { } else {
if (!isLocal) { fs.unlink(path, () => { });
fs.unlink(path, () => {
});
}
throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`; throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`;
} }
return null; return null;

View File

@@ -31,7 +31,6 @@ export class SendGroupNotice extends BaseAction<Payload, null> {
//公告图逻辑 //公告图逻辑
const { const {
path, path,
isLocal,
success, success,
} = (await uri2local(this.core.NapCatTempPath, payload.image)); } = (await uri2local(this.core.NapCatTempPath, payload.image));
if (!success) { if (!success) {
@@ -45,10 +44,10 @@ export class SendGroupNotice extends BaseAction<Payload, null> {
if (ImageUploadResult.errCode != 0) { if (ImageUploadResult.errCode != 0) {
throw `群公告${payload.image}设置失败,图片上传失败`; throw `群公告${payload.image}设置失败,图片上传失败`;
} }
if (!isLocal) {
unlink(path, () => { unlink(path, () => {
}); });
}
UploadImage = ImageUploadResult.picInfo; UploadImage = ImageUploadResult.picInfo;
} }

View File

@@ -25,17 +25,14 @@ export default class SetGroupPortrait extends BaseAction<Payload, any> {
} }
async _handle(payload: Payload): Promise<any> { async _handle(payload: Payload): Promise<any> {
const { path, isLocal, success } = (await uri2local(this.core.NapCatTempPath, payload.file)); const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.file));
if (!success) { if (!success) {
throw `头像${payload.file}设置失败,file字段可能格式不正确`; throw `头像${payload.file}设置失败,file字段可能格式不正确`;
} }
if (path) { if (path) {
await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断 await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断
const ret = await this.core.apis.GroupApi.setGroupAvatar(payload.group_id.toString(), path) as any; const ret = await this.core.apis.GroupApi.setGroupAvatar(payload.group_id.toString(), path) as any;
if (!isLocal) { fs.unlink(path, () => { });
fs.unlink(path, () => {
});
}
if (!ret) { if (!ret) {
throw `头像${payload.file}设置失败,api无返回`; throw `头像${payload.file}设置失败,api无返回`;
} }
@@ -46,10 +43,7 @@ export default class SetGroupPortrait extends BaseAction<Payload, any> {
} }
return ret; return ret;
} else { } else {
if (!isLocal) { fs.unlink(path, () => {});
fs.unlink(path, () => {
});
}
throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`; throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`;
} }
} }

View File

@@ -805,7 +805,6 @@ export class OneBotMsgApi {
} }
const { const {
path, path,
isLocal,
fileName, fileName,
errMsg, errMsg,
success, success,
@@ -816,9 +815,7 @@ export class OneBotMsgApi {
throw Error('文件下载失败' + errMsg); throw Error('文件下载失败' + errMsg);
} }
if (!isLocal) { // 只删除http和base64转过来的文件
deleteAfterSentFiles.push(path); deleteAfterSentFiles.push(path);
}
return { path, fileName: inputdata.name ?? fileName }; return { path, fileName: inputdata.name ?? fileName };
} }