build: 181test

This commit is contained in:
手瓜一十雪 2024-08-06 11:07:37 +08:00
parent 1070278eaf
commit 8fa6a12a7c
8 changed files with 29 additions and 26 deletions

View File

@ -181,7 +181,7 @@ type Uri2LocalRes = {
isLocal: boolean isLocal: boolean
} }
export async function uri2local(uri: string, fileName: string | null = null): Promise<Uri2LocalRes> { export async function uri2local(UriOrPath: string, fileName: string | null = null): Promise<Uri2LocalRes> {
const res = { const res = {
success: false, success: false,
errMsg: '', errMsg: '',
@ -190,26 +190,29 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr
path: '', path: '',
isLocal: false isLocal: false
}; };
if (!fileName) { if (!fileName) fileName = randomUUID();
fileName = randomUUID(); let filePath = path.join(getTempDir(), fileName);//临时目录
}
let filePath = path.join(getTempDir(), fileName);
let url = null; let url = null;
//区分path和uri
try { try {
url = new URL(uri); if (fs.existsSync(UriOrPath)) url = new URL("file://" + UriOrPath);
} catch (e: any) { } catch (error: any) { }
res.errMsg = `uri ${uri} 解析失败,` + e.toString() + ` 可能${uri}不存在`; try {
url = new URL(UriOrPath);
} catch (error: any) { }
//验证url
if (!url) {
res.errMsg = `UriOrPath ${UriOrPath} 解析失败,可能${UriOrPath}不存在`;
return res; return res;
} }
// log("uri protocol", url.protocol, uri);
if (url.protocol == 'base64:') { if (url.protocol == 'base64:') {
// base64转成文件 // base64转成文件
const base64Data = uri.split('base64://')[1]; const base64Data = UriOrPath.split('base64://')[1];
try { try {
const buffer = Buffer.from(base64Data, 'base64'); const buffer = Buffer.from(base64Data, 'base64');
fs.writeFileSync(filePath, buffer); fs.writeFileSync(filePath, buffer);
} catch (e: any) { } catch (e: any) {
res.errMsg = 'base64文件下载失败,' + e.toString(); res.errMsg = 'base64文件下载失败,' + e.toString();
return res; return res;
@ -218,7 +221,7 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr
// 下载文件 // 下载文件
let buffer: Buffer | null = null; let buffer: Buffer | null = null;
try { try {
buffer = await httpDownload(uri); buffer = await httpDownload(UriOrPath);
} catch (e: any) { } catch (e: any) {
res.errMsg = `${url}下载失败,` + e.toString(); res.errMsg = `${url}下载失败,` + e.toString();
return res; return res;
@ -252,6 +255,7 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr
} }
} }
else { else {
// 26702执行forword file文件操作 不应该在这里乱来
// const cache = await dbUtil.getFileCacheByName(uri); // const cache = await dbUtil.getFileCacheByName(uri);
// if (cache) { // if (cache) {
// filePath = cache.path; // filePath = cache.path;
@ -259,7 +263,6 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr
// filePath = uri; // filePath = uri;
// } // }
} }
res.isLocal = true; res.isLocal = true;
} }
// else{ // else{

View File

@ -20,8 +20,8 @@ export class OCRImage extends BaseAction<Payload, any> {
actionName = ActionName.OCRImage; actionName = ActionName.OCRImage;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const { path, isLocal, errMsg } = (await uri2local(payload.image)); const { path, isLocal, errMsg,success } = (await uri2local(payload.image));
if (errMsg) { if (!success) {
throw `OCR ${payload.image}失败,image字段可能格式不正确`; throw `OCR ${payload.image}失败,image字段可能格式不正确`;
} }
if (path) { if (path) {

View File

@ -26,8 +26,8 @@ export default class SetGroupHeader extends BaseAction<Payload, any> {
}; };
} }
protected async _handle(payload: Payload): Promise<any> { protected async _handle(payload: Payload): Promise<any> {
const { path, isLocal, errMsg } = (await uri2local(payload.file)); const { path, isLocal, errMsg,success } = (await uri2local(payload.file));
if (errMsg) { if (!success) {
throw `头像${payload.file}设置失败,file字段可能格式不正确`; throw `头像${payload.file}设置失败,file字段可能格式不正确`;
} }
if (path) { if (path) {

View File

@ -24,8 +24,8 @@ export default class SetAvatar extends BaseAction<Payload, null> {
}; };
} }
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const { path, isLocal, errMsg } = (await uri2local(payload.file)); const { path, isLocal, errMsg,success } = (await uri2local(payload.file));
if (errMsg) { if (!success) {
throw `头像${payload.file}设置失败,file字段可能格式不正确`; throw `头像${payload.file}设置失败,file字段可能格式不正确`;
} }
if (path) { if (path) {

View File

@ -7,7 +7,7 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts';
const SchemaData = { const SchemaData = {
type: 'object', type: 'object',
properties: { properties: {
group_id: { type: [ 'number' , 'string' ] }, group_id: { type: ['number', 'string'] },
content: { type: 'string' }, content: { type: 'string' },
image: { type: 'string' }, image: { type: 'string' },
pinned: { type: 'number' }, pinned: { type: 'number' },
@ -24,8 +24,8 @@ export class SendGroupNotice extends BaseAction<Payload, null> {
let UploadImage: { id: string, width: number, height: number } | undefined = undefined; let UploadImage: { id: string, width: number, height: number } | undefined = undefined;
if (payload.image) { if (payload.image) {
//公告图逻辑 //公告图逻辑
const { errMsg, path, isLocal } = (await uri2local(payload.image)); const { errMsg, path, isLocal, success } = (await uri2local(payload.image));
if (errMsg) { if (!success) {
throw `群公告${payload.image}设置失败,image字段可能格式不正确`; throw `群公告${payload.image}设置失败,image字段可能格式不正确`;
} }
if (!path) { if (!path) {

View File

@ -34,7 +34,7 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
file = `file://${file}`; file = `file://${file}`;
} }
const downloadResult = await uri2local(file); const downloadResult = await uri2local(file);
if (downloadResult.errMsg) { if (!downloadResult.success) {
throw new Error(downloadResult.errMsg); throw new Error(downloadResult.errMsg);
} }
const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name, payload.folder_id); const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name, payload.folder_id);

View File

@ -41,7 +41,7 @@ export default class GoCQHTTPUploadPrivateFile extends BaseAction<Payload, null>
file = `file://${file}`; file = `file://${file}`;
} }
const downloadResult = await uri2local(file); const downloadResult = await uri2local(file);
if (downloadResult.errMsg) { if (!downloadResult.success) {
throw new Error(downloadResult.errMsg); throw new Error(downloadResult.errMsg);
} }
const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name); const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name);

View File

@ -27,9 +27,9 @@ async function handleOb11FileLikeMessage(
{ deleteAfterSentFiles }: MessageContext { deleteAfterSentFiles }: MessageContext
) { ) {
//有的奇怪的框架将url作为参数 而不是file 此时优先url //有的奇怪的框架将url作为参数 而不是file 此时优先url
const { path, isLocal, fileName, errMsg } = (await uri2local(inputdata?.url || inputdata.file)); const { path, isLocal, fileName, errMsg,success } = (await uri2local(inputdata?.url || inputdata.file));
if (errMsg) { if (!success) {
logError('文件下载失败', errMsg); logError('文件下载失败', errMsg);
throw Error('文件下载失败' + errMsg); throw Error('文件下载失败' + errMsg);
} }