From a1c64eeefc1e1acc59fe3f3a431a170c933f2855 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:52:42 -0700 Subject: [PATCH] Web: Fix renderer plugin assets fail to load on secondary profiles (#10903) --- .../NoteBodyViewer/hooks/useRerenderHandler.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/app-mobile/components/NoteBodyViewer/hooks/useRerenderHandler.ts b/packages/app-mobile/components/NoteBodyViewer/hooks/useRerenderHandler.ts index bb5a3f8e70..b3cefd5283 100644 --- a/packages/app-mobile/components/NoteBodyViewer/hooks/useRerenderHandler.ts +++ b/packages/app-mobile/components/NoteBodyViewer/hooks/useRerenderHandler.ts @@ -11,6 +11,7 @@ import Setting from '@joplin/lib/models/Setting'; import shim from '@joplin/lib/shim'; import Resource from '@joplin/lib/models/Resource'; import { ResourceEntity } from '@joplin/lib/services/database/types'; +import resolvePathWithinDir from '@joplin/lib/utils/resolvePathWithinDir'; export interface ResourceInfo { @@ -179,9 +180,19 @@ const useRerenderHandler = (props: Props) => { } }, readAssetBlob: (assetPath: string) => { - const assetsDir = `${Setting.value('resourceDir')}/`; - const path = shim.fsDriver().resolveRelativePathWithinDir(assetsDir, assetPath); - return shim.fsDriver().fileAtPath(path); + // Built-in assets are in resourceDir, external plugin assets are in cacheDir. + const assetsDirs = [Setting.value('resourceDir'), Setting.value('cacheDir')]; + + let resolvedPath = null; + for (const assetDir of assetsDirs) { + resolvedPath ??= resolvePathWithinDir(assetDir, assetPath); + if (resolvedPath) break; + } + + if (!resolvedPath) { + throw new Error(`Failed to load asset at ${assetPath} -- not in any of the allowed asset directories: ${assetsDirs.join(',')}.`); + } + return shim.fsDriver().fileAtPath(resolvedPath); }, createEditPopupSyntax,