Skip to content

Commit 006811e

Browse files
committed
fix: preserve filename in uri2local; add file preview
Add an optional fileName parameter to uri2local and propagate it when downloading remote files so a cached/original filename is preserved. When resolving Unknown URIs, treat a match-by-name as the filename and pass it into the recursive uri2local call. Also add a preview string for ElementType.File in MessageBuilding to show the file name in message previews.
1 parent cd672f6 commit 006811e

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/common/utils/file.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type Uri2LocalRes = {
100100
isLocal: boolean
101101
}
102102

103-
export async function uri2local(ctx: Context, uri: string, needExt?: boolean): Promise<Uri2LocalRes> {
103+
export async function uri2local(ctx: Context, uri: string, needExt?: boolean, fileName?: string): Promise<Uri2LocalRes> {
104104
const { type } = checkUriType(uri)
105105

106106
if (type === FileUriType.FileURL) {
@@ -120,17 +120,23 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
120120
if (type === FileUriType.RemoteURL) {
121121
try {
122122
const res = await fetchFile(uri)
123-
let fileName = randomUUID()
124-
let filePath = path.join(TEMP_DIR, fileName)
125-
await fsPromise.writeFile(filePath, res.data)
126-
if (needExt) {
127-
const ext = (await getFileType(filePath)).ext
128-
fileName += `.${ext}`
129-
const newPath = `${filePath}.${ext}`
130-
await fsPromise.rename(filePath, newPath)
131-
filePath = newPath
123+
if (fileName) {
124+
const filePath = path.join(TEMP_DIR, randomUUID())
125+
await fsPromise.writeFile(filePath, res.data)
126+
return { success: true, errMsg: '', fileName, path: filePath, isLocal: false }
127+
} else {
128+
let fileName = randomUUID()
129+
let filePath = path.join(TEMP_DIR, fileName)
130+
await fsPromise.writeFile(filePath, res.data)
131+
if (needExt) {
132+
const ext = (await getFileType(filePath)).ext
133+
fileName += `.${ext}`
134+
const newPath = `${filePath}.${ext}`
135+
await fsPromise.rename(filePath, newPath)
136+
filePath = newPath
137+
}
138+
return { success: true, errMsg: '', fileName, path: filePath, isLocal: false }
132139
}
133-
return { success: true, errMsg: '', fileName, path: filePath, isLocal: false }
134140
} catch (e) {
135141
const errMsg = `${uri} 下载失败, ${(e as Error).message}`
136142
return { success: false, errMsg, fileName: '', path: '', isLocal: false }
@@ -170,12 +176,16 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
170176
}
171177

172178
if (type === FileUriType.Unknown) {
179+
let fileName
173180
// uri可能是文件名
174181
let fileCache = await ctx.store.getFileCacheById(uri)
175-
if (!fileCache?.length) {
182+
if (!fileCache.length) {
176183
fileCache = await ctx.store.getFileCacheByName(uri)
184+
if (fileCache.length) {
185+
fileName = uri
186+
}
177187
}
178-
if (fileCache?.length) {
188+
if (fileCache.length) {
179189
const isGroup = fileCache[0].chatType === ChatType.Group
180190
let url
181191
if (fileCache[0].elementType === ElementType.Pic) {
@@ -188,7 +198,7 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
188198
url = (await ctx.ntFileApi.getFileUrl(fileCache[0].fileUuid, isGroup, isGroup ? +fileCache[0].peerUid : undefined)).url
189199
}
190200
if (url) {
191-
return await uri2local(ctx, url, needExt)
201+
return await uri2local(ctx, url, needExt, fileName)
192202
} else {
193203
return { success: false, errMsg: `不支持的文件类型: ${fileCache[0].elementType}`, fileName: '', path: '', isLocal: false }
194204
}

src/ntqqapi/helper/messageBuilding.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ export class MessageBuilding {
272272
preview = '[合并转发]'
273273
} else if (curr.elementType === ElementType.Reply) {
274274
preview = ''
275+
} else if (curr.elementType === ElementType.File) {
276+
preview = `[文件] ${curr.fileElement.fileName}`
275277
}
276278
return acc + preview
277279
}, '')

0 commit comments

Comments
 (0)