This commit is contained in:
手瓜一十雪
2024-08-25 22:36:53 +08:00
5 changed files with 25 additions and 35 deletions

View File

@@ -187,22 +187,27 @@ export enum FileUriType {
export async function checkUriType(Uri: string) {
const LocalFileRet = await solveProblem((Uri) => { if (fs.existsSync(Uri)) return { Uri: Uri, Type: FileUriType.Local }; });
const LocalFileRet = await solveProblem((uri: string) => {
if (fs.existsSync(uri)) {
return { Uri: uri, Type: FileUriType.Local };
}
return undefined;
}, Uri);
if (LocalFileRet) return LocalFileRet;
const OtherFileRet = await solveProblem((Uri) => {
const OtherFileRet = await solveProblem((uri: string) => {
//再判断是否是Http
if (Uri.startsWith('http://') || Uri.startsWith('https://')) {
return { Uri: Uri, Type: FileUriType.Remote };
if (uri.startsWith('http://') || uri.startsWith('https://')) {
return { Uri: uri, Type: FileUriType.Remote };
}
//再判断是否是Base64
if (Uri.startsWith('base64://')) {
return { Uri: Uri, Type: FileUriType.Base64 };
if (uri.startsWith('base64://')) {
return { Uri: uri, Type: FileUriType.Base64 };
}
if (Uri.startsWith('file://')) {
if (uri.startsWith('file://')) {
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') {
filePath = pathname.slice(1);
} else {
@@ -210,7 +215,7 @@ export async function checkUriType(Uri: string) {
}
return { Uri: filePath, Type: FileUriType.Local };
}
});
}, Uri);
if (OtherFileRet) return OtherFileRet;
return { Uri: Uri, Type: FileUriType.Unknown };

View File

@@ -3,10 +3,10 @@ import fs from 'fs';
import os from 'node:os';
import { QQLevel } from '@/core';
export async function solveProblem<T extends (...arg: any[]) => any>(func: T): Promise<ReturnType<T> | undefined> {
return new Promise<ReturnType<T> | undefined>(async (resolve) => {
export async function solveProblem<T extends (...arg: any[]) => any>(func: T, ...args: Parameters<T>): Promise<ReturnType<T> | undefined> {
return new Promise<ReturnType<T> | undefined>((resolve) => {
try {
const result = func();
const result = func(...args);
resolve(result);
} catch (e) {
resolve(undefined);
@@ -14,10 +14,10 @@ export async function solveProblem<T extends (...arg: any[]) => any>(func: T): P
});
}
export async function solveAsyncProblem<T extends (...arg: any[]) => Promise<any>>(func: T): Promise<Awaited<ReturnType<T>> | undefined> {
export async function solveAsyncProblem<T extends (...args: any[]) => Promise<any>>(func: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>> | undefined> {
return new Promise<Awaited<ReturnType<T>> | undefined>(async (resolve) => {
try {
const result = await func();
const result = await func(...args);
resolve(result);
} catch (e) {
resolve(undefined);