mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
refactor: remove unnecessary caching from normalizeDataUrlsToFilePaths
- Function is now primarily used for testing - Production code uses dual-storage (images + imagesBase64) - Removed redundant cache since same hash always produces same file path - Simplified implementation and removed obsolete test
This commit is contained in:
parent
17ee88c802
commit
7ddfa4e529
2 changed files with 9 additions and 45 deletions
|
|
@ -118,23 +118,6 @@ describe("normalizeDataUrlsToFilePaths", () => {
|
|||
expect(result[1]).toMatch(/\.jpeg$/) // Correct extension from MIME type
|
||||
})
|
||||
|
||||
it("should reuse cached file paths for same data URL", async () => {
|
||||
const dataUrl = "data:image/png;base64,test123"
|
||||
|
||||
// First call - should write file
|
||||
const result1 = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath)
|
||||
expect(fs.writeFile).toHaveBeenCalledTimes(1)
|
||||
|
||||
// Mock file exists for second call
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined)
|
||||
|
||||
// Second call - should use cached path
|
||||
const result2 = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath)
|
||||
|
||||
expect(result1[0]).toBe(result2[0]) // Same path returned
|
||||
expect(fs.writeFile).toHaveBeenCalledTimes(1) // Not written again
|
||||
})
|
||||
|
||||
it("should handle errors gracefully", async () => {
|
||||
const dataUrl = "data:image/png;base64,test"
|
||||
|
||||
|
|
|
|||
|
|
@ -44,16 +44,13 @@ export async function normalizeImageRefsToDataUrls(imageRefs: string[]): Promise
|
|||
return results
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache mapping base64 data URLs to their temporary file paths
|
||||
* This prevents writing the same image multiple times
|
||||
*/
|
||||
const base64ToFilePathCache = new Map<string, string>()
|
||||
|
||||
/**
|
||||
* Converts base64 data URLs to file paths suitable for webview URIs.
|
||||
* Writes base64 images to temporary files and returns their paths.
|
||||
* This is the reverse of normalizeImageRefsToDataUrls.
|
||||
*
|
||||
* NOTE: This function is primarily used for testing. In production, the dual-storage
|
||||
* approach (images + imagesBase64 in ClineMessage) eliminates the need for this conversion.
|
||||
* No caching is implemented since this is a test utility.
|
||||
*/
|
||||
export async function normalizeDataUrlsToFilePaths(dataUrls: string[], globalStoragePath: string): Promise<string[]> {
|
||||
const results: string[] = []
|
||||
|
|
@ -75,22 +72,6 @@ export async function normalizeDataUrlsToFilePaths(dataUrls: string[], globalSto
|
|||
}
|
||||
|
||||
try {
|
||||
// Check cache first
|
||||
const hash = crypto.createHash("md5").update(dataUrl).digest("hex")
|
||||
let filePath = base64ToFilePathCache.get(hash)
|
||||
|
||||
if (filePath) {
|
||||
// Check if file still exists
|
||||
try {
|
||||
await fs.access(filePath)
|
||||
results.push(filePath)
|
||||
continue
|
||||
} catch {
|
||||
// File was deleted, need to recreate
|
||||
base64ToFilePathCache.delete(hash)
|
||||
}
|
||||
}
|
||||
|
||||
// Extract base64 data and MIME type
|
||||
const commaIndex = dataUrl.indexOf(",")
|
||||
if (commaIndex === -1) {
|
||||
|
|
@ -105,15 +86,15 @@ export async function normalizeDataUrlsToFilePaths(dataUrls: string[], globalSto
|
|||
const mimeMatch = header.match(/data:image\/([^;]+)/)
|
||||
const extension = mimeMatch ? `.${mimeMatch[1]}` : ".png"
|
||||
|
||||
// Create a unique hash for this image
|
||||
const hash = crypto.createHash("md5").update(dataUrl).digest("hex")
|
||||
|
||||
// Write to temp file
|
||||
filePath = path.join(tempDir, `${hash}${extension}`)
|
||||
const filePath = path.join(tempDir, `${hash}${extension}`)
|
||||
const buffer = Buffer.from(base64Data, "base64")
|
||||
await fs.writeFile(filePath, buffer)
|
||||
|
||||
// Cache the mapping
|
||||
base64ToFilePathCache.set(hash, filePath)
|
||||
|
||||
// Also cache in the image cache for reverse lookups
|
||||
// Cache the reverse mapping for image-cache lookups
|
||||
setImageBase64ForPath(filePath, dataUrl)
|
||||
|
||||
results.push(filePath)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue