@@ -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 }
0 commit comments