fix: self nickname

fix: @member msg report
fix: send file:// on Windows
ver: 3.0.2
This commit is contained in:
linyuchen
2024-02-13 18:37:01 +08:00
parent 6a8d67a8ae
commit 1936671cb3
8 changed files with 122 additions and 125 deletions

View File

@@ -6,6 +6,12 @@ const fs = require("fs").promises;
export async function uri2local(fileName: string, uri: string){
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]
@@ -13,51 +19,43 @@ export async function uri2local(fileName: string, uri: string){
const buffer = Buffer.from(base64Data, 'base64');
await fs.writeFile(filePath, buffer);
} catch (e: any) {
return {
success: false,
errMsg: `base64文件下载失败,` + e.toString(),
path: ""
}
res.errMsg = `base64文件下载失败,` + e.toString()
return res
}
} else if (url.protocol == "http:" || url.protocol == "https:") {
// 下载文件
let res = await fetch(url)
if (!res.ok) {
return {
success: false,
errMsg: `${url}下载失败,` + res.statusText,
path: ""
}
let fetchRes = await fetch(url)
if (!fetchRes.ok) {
res.errMsg = `${url}下载失败,` + fetchRes.statusText
return res
}
let blob = await res.blob();
let blob = await fetchRes.blob();
let buffer = await blob.arrayBuffer();
try {
await fs.writeFile(filePath, Buffer.from(buffer));
} catch (e: any) {
return {
success: false,
errMsg: `${url}下载失败,` + e.toString(),
path: ""
}
res.errMsg = `${url}下载失败,` + e.toString()
return res
}
} else if (url.protocol === "file:"){
await fs.copyFile(url.pathname, filePath);
// filePath = (await NTQQApi.uploadFile(url.pathname)).path;
// await fs.copyFile(url.pathname, filePath);
if (process.platform === "win32"){
filePath = url.pathname.slice(1)
}
else{
filePath = url.pathname
}
res.isLocal = true
}
else{
return {
success: false,
errMsg: `不支持的file协议,` + url.protocol,
path: ""
}
res.errMsg = `不支持的file协议,` + url.protocol
return res
}
if (isGIF(filePath)) {
if (isGIF(filePath) && !res.isLocal) {
await fs.rename(filePath, filePath + ".gif");
filePath += ".gif";
}
return {
success: true,
errMsg: "",
path: filePath
};
res.success = true
res.path = filePath
return res
}