mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
chore: uriToLocalFile
This commit is contained in:
@@ -175,10 +175,9 @@ export async function checkUriType(Uri: string) {
|
|||||||
return { Uri: Uri, Type: FileUriType.Unknown };
|
return { Uri: Uri, Type: FileUriType.Unknown };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function uriToLocalFile(dir: string, uri: string): Promise<Uri2LocalRes> {
|
export async function uriToLocalFile(dir: string, uri: string, filename: string = randomUUID(), headers?: Record<string, string>): Promise<Uri2LocalRes> {
|
||||||
const { Uri: HandledUri, Type: UriType } = await checkUriType(uri);
|
const { Uri: HandledUri, Type: UriType } = await checkUriType(uri);
|
||||||
|
|
||||||
const filename = randomUUID();
|
|
||||||
const filePath = path.join(dir, filename);
|
const filePath = path.join(dir, filename);
|
||||||
|
|
||||||
switch (UriType) {
|
switch (UriType) {
|
||||||
@@ -191,7 +190,7 @@ export async function uriToLocalFile(dir: string, uri: string): Promise<Uri2Loca
|
|||||||
}
|
}
|
||||||
|
|
||||||
case FileUriType.Remote: {
|
case FileUriType.Remote: {
|
||||||
const buffer = await httpDownload(HandledUri);
|
const buffer = await httpDownload({ url: HandledUri, headers: headers });
|
||||||
fs.writeFileSync(filePath, buffer, { flag: 'wx' });
|
fs.writeFileSync(filePath, buffer, { flag: 'wx' });
|
||||||
return { success: true, errMsg: '', fileName: filename, path: filePath };
|
return { success: true, errMsg: '', fileName: filename, path: filePath };
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ import { OneBotAction } from '@/onebot/action/OneBotAction';
|
|||||||
import { ActionName } from '@/onebot/action/router';
|
import { ActionName } from '@/onebot/action/router';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { join as joinPath } from 'node:path';
|
import { join as joinPath } from 'node:path';
|
||||||
import { calculateFileMD5, httpDownload } from '@/common/file';
|
import { calculateFileMD5, httpDownload, uriToLocalFile } from '@/common/file';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import { Static, Type } from '@sinclair/typebox';
|
import { Static, Type } from '@sinclair/typebox';
|
||||||
|
|
||||||
@@ -26,20 +26,20 @@ export default class GoCQHTTPDownloadFile extends OneBotAction<Payload, FileResp
|
|||||||
async _handle(payload: Payload): Promise<FileResponse> {
|
async _handle(payload: Payload): Promise<FileResponse> {
|
||||||
const isRandomName = !payload.name;
|
const isRandomName = !payload.name;
|
||||||
const name = payload.name || randomUUID();
|
const name = payload.name || randomUUID();
|
||||||
const filePath = joinPath(this.core.NapCatTempPath, name);
|
let result: Awaited<ReturnType<typeof uriToLocalFile>>;
|
||||||
|
|
||||||
if (payload.base64) {
|
if (payload.base64) {
|
||||||
fs.writeFileSync(filePath, payload.base64, 'base64');
|
result = await uriToLocalFile(this.core.NapCatTempPath, `base64://${payload.base64}`, name);
|
||||||
} else if (payload.url?.startsWith('file://')) {
|
|
||||||
const path = payload.url.substring(7);
|
|
||||||
fs.copyFileSync(path, filePath);
|
|
||||||
} else if (payload.url) {
|
} else if (payload.url) {
|
||||||
const headers = this.getHeaders(payload.headers);
|
const headers = this.getHeaders(payload.headers);
|
||||||
const buffer = await httpDownload({ url: payload.url, headers: headers });
|
result = await uriToLocalFile(this.core.NapCatTempPath, payload.url, name, headers);
|
||||||
fs.writeFileSync(filePath, Buffer.from(buffer), 'binary');
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('不存在任何文件, 无法下载');
|
throw new Error('不存在任何文件, 无法下载');
|
||||||
}
|
}
|
||||||
|
if (!result.success) {
|
||||||
|
throw new Error(result.errMsg);
|
||||||
|
}
|
||||||
|
const filePath = result.path;
|
||||||
if (fs.existsSync(filePath)) {
|
if (fs.existsSync(filePath)) {
|
||||||
|
|
||||||
if (isRandomName) {
|
if (isRandomName) {
|
||||||
|
Reference in New Issue
Block a user