Fix native draft media filename encoding

The previous approach replaced both / and : with _, but the reverse
transformation couldn't distinguish between them. This caused cache
misses for paths containing both characters.

Use encodeURIComponent/decodeURIComponent for a proper reversible
encoding.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-01-27 19:30:54 +02:00
parent 57d7223e42
commit 6d46a70b7a
@@ -13,8 +13,8 @@ function getMediaDirectory(): Directory {
} }
function getMediaFile(localRefPath: string): File { function getMediaFile(localRefPath: string): File {
// Use localRefPath as filename (replace unsafe chars) // Use localRefPath as filename (URL-encoded for filesystem safety)
const safeFilename = localRefPath.replace(/[/:]/g, '_') const safeFilename = encodeURIComponent(localRefPath)
return new File(getMediaDirectory(), safeFilename) return new File(getMediaDirectory(), safeFilename)
} }
@@ -123,8 +123,8 @@ function populateCacheInternal(): Promise<void> {
if (dir.exists) { if (dir.exists) {
const items = dir.list() const items = dir.list()
for (const item of items) { for (const item of items) {
// Reverse the safe filename transformation // Reverse the URL encoding to get the original localRefPath
const localRefPath = item.name.replace(/_/g, ':').replace(/_/g, '/') const localRefPath = decodeURIComponent(item.name)
mediaExistsCache.set(localRefPath, true) mediaExistsCache.set(localRefPath, true)
} }
} }