@@ -7,6 +7,7 @@ import { TEMP_DIR } from '../globalVars'
77import { randomUUID , createHash } from 'node:crypto'
88import { fileURLToPath } from 'node:url'
99import { Context } from 'cordis'
10+ import { ChatType , ElementType } from '@/ntqqapi/types'
1011
1112// 定义一个异步函数来检查文件是否存在
1213export function checkFileReceived ( path : string , timeout : number = 3000 ) : Promise < void > {
@@ -122,7 +123,7 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
122123 let filePath = path . join ( TEMP_DIR , fileName )
123124 await fsPromise . writeFile ( filePath , res . data )
124125 if ( needExt ) {
125- const ext = ( await ctx . ntFileApi . getFileType ( filePath ) ) . ext
126+ const ext = ( await getFileType ( filePath ) ) . ext
126127 fileName += `.${ ext } `
127128 const newPath = `${ filePath } .${ ext } `
128129 await fsPromise . rename ( filePath , newPath )
@@ -141,7 +142,7 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
141142 const base64 = uri . replace ( / ^ b a s e 6 4 : \/ \/ / , '' )
142143 await fsPromise . writeFile ( filePath , base64 , 'base64' )
143144 if ( needExt ) {
144- const ext = ( await ctx . ntFileApi . getFileType ( filePath ) ) . ext
145+ const ext = ( await getFileType ( filePath ) ) . ext
145146 filename += `.${ ext } `
146147 await fsPromise . rename ( filePath , `${ filePath } .${ ext } ` )
147148 filePath = `${ filePath } .${ ext } `
@@ -158,7 +159,7 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
158159 let filePath = path . join ( TEMP_DIR , filename )
159160 await fsPromise . writeFile ( filePath , base64 , 'base64' )
160161 if ( needExt ) {
161- const ext = ( await ctx . ntFileApi . getFileType ( filePath ) ) . ext
162+ const ext = ( await getFileType ( filePath ) ) . ext
162163 filename += `.${ ext } `
163164 await fsPromise . rename ( filePath , `${ filePath } .${ ext } ` )
164165 filePath = `${ filePath } .${ ext } `
@@ -174,15 +175,21 @@ export async function uri2local(ctx: Context, uri: string, needExt?: boolean): P
174175 fileCache = await ctx . store . getFileCacheByName ( uri )
175176 }
176177 if ( fileCache ?. length ) {
177- const downloadPath = await ctx . ntFileApi . downloadMedia (
178- fileCache [ 0 ] . msgId ,
179- fileCache [ 0 ] . chatType ,
180- fileCache [ 0 ] . peerUid ,
181- fileCache [ 0 ] . elementId ,
182- '' ,
183- '' ,
184- )
185- return { success : true , errMsg : '' , fileName : fileCache [ 0 ] . fileName , path : downloadPath , isLocal : true }
178+ const isGroup = fileCache [ 0 ] . chatType === ChatType . Group
179+ let url
180+ if ( fileCache [ 0 ] . elementType === ElementType . Pic ) {
181+ const originImageUrl = `/download?appid=${ isGroup ? 1407 : 1406 } &fileid=${ fileCache [ 0 ] . fileUuid } &spec=0`
182+ url = await ctx . ntFileApi . getImageUrl ( originImageUrl , fileCache [ 0 ] . md5HexStr )
183+ } else if ( fileCache [ 0 ] . elementType === ElementType . Video ) {
184+ url = await ctx . ntFileApi . getVideoUrl ( fileCache [ 0 ] . fileUuid , isGroup )
185+ } else if ( fileCache [ 0 ] . elementType === ElementType . Ptt ) {
186+ url = await ctx . ntFileApi . getPttUrl ( fileCache [ 0 ] . fileUuid , isGroup )
187+ }
188+ if ( url ) {
189+ return await uri2local ( ctx , url , needExt )
190+ } else {
191+ return { success : false , errMsg : `不支持的文件类型: ${ fileCache [ 0 ] . elementType } ` , fileName : '' , path : '' , isLocal : false }
192+ }
186193 }
187194 }
188195
0 commit comments