koishi-plugin-chat-patch 2.4.0 → 2.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -168,7 +168,10 @@ export class MessageHandler {
168
168
 
169
169
  public async downloadAndCacheMedia(url: string, type: 'image' | 'media' | 'avatar') {
170
170
  try {
171
- if (!url || url.startsWith('data:') || url.startsWith('file:')) return url
171
+ if (!url || url.startsWith('data:')) return url
172
+
173
+ // 如果已经是 Vite @fs 路径,直接返回
174
+ if (url.includes('/vite/@fs/')) return url
172
175
 
173
176
  let folder = 'media'
174
177
  if (type === 'image') folder = 'images'
@@ -185,15 +188,17 @@ export class MessageHandler {
185
188
  const filename = `${hash}${ext}`
186
189
  const filePath = require('node:path').join(dir, filename)
187
190
 
188
- if (require('node:fs').existsSync(filePath)) {
189
- return require('node:url').pathToFileURL(filePath).href
191
+ if (!require('node:fs').existsSync(filePath)) {
192
+ const buffer = await this.ctx.http.get(url, { responseType: 'arraybuffer' })
193
+ require('node:fs').writeFileSync(filePath, Buffer.from(buffer))
190
194
  }
191
195
 
192
- const buffer = await this.ctx.http.get(url, { responseType: 'arraybuffer' })
193
- require('node:fs').writeFileSync(filePath, Buffer.from(buffer))
194
-
195
- return require('node:url').pathToFileURL(filePath).href
196
+ // 返回 Vite @fs 路径格式,让浏览器通过 Vite 开发服务器加载本地文件
197
+ // Windows 路径需要转换为正斜杠
198
+ const normalizedPath = filePath.replace(/\\/g, '/')
199
+ return `/vite/@fs/${normalizedPath}`
196
200
  } catch (e) {
201
+ this.logger.warn('下载并缓存媒体失败:', e)
197
202
  return url
198
203
  }
199
204
  }