LLOneBot/src/onebot11/utils.ts
linyuchen 3d0b90db35 fix: support get_status online
feat: seconds of auto delete file
refactor: file report
2024-03-01 21:43:05 +08:00

81 lines
2.4 KiB
TypeScript

import {CONFIG_DIR, isGIF} from "../common/utils";
import {v4 as uuidv4} from "uuid";
import * as path from 'path';
import {fileCache} from "../common/data";
const fs = require("fs").promises;
export async function uri2local(uri: string, fileName: string = null) {
if (!fileName) {
fileName = uuidv4();
}
let filePath = path.join(CONFIG_DIR, fileName)
let url = new URL(uri);
let res = {
success: false,
errMsg: "",
path: "",
isLocal: false
}
if (url.protocol == "base64:") {
// base64转成文件
let base64Data = uri.split("base64://")[1]
try {
const buffer = Buffer.from(base64Data, 'base64');
await fs.writeFile(filePath, buffer);
} catch (e: any) {
res.errMsg = `base64文件下载失败,` + e.toString()
return res
}
} else if (url.protocol == "http:" || url.protocol == "https:") {
// 下载文件
let fetchRes = await fetch(url)
if (!fetchRes.ok) {
res.errMsg = `${url}下载失败,` + fetchRes.statusText
return res
}
let blob = await fetchRes.blob();
let buffer = await blob.arrayBuffer();
try {
fileName = path.basename(url.pathname) || fileName
filePath = path.join(CONFIG_DIR, fileName)
await fs.writeFile(filePath, Buffer.from(buffer));
} catch (e: any) {
res.errMsg = `${url}下载失败,` + e.toString()
return res
}
} else {
let pathname: string;
if (url.protocol === "file:") {
// await fs.copyFile(url.pathname, filePath);
pathname = decodeURIComponent(url.pathname)
if (process.platform === "win32") {
filePath = pathname.slice(1)
} else {
filePath = pathname
}
}
else{
const cache = fileCache.get(uri)
if (cache) {
filePath = cache.filePath
}
else{
filePath = uri;
}
}
res.isLocal = true
}
// else{
// res.errMsg = `不支持的file协议,` + url.protocol
// return res
// }
if (isGIF(filePath) && !res.isLocal) {
await fs.rename(filePath, filePath + ".gif");
filePath += ".gif";
}
res.success = true
res.path = filePath
return res
}