mirror of
https://github.com/webadderallorg/Recordly.git
synced 2026-11-09 07:23:14 +00:00
fix: resolve packaged wallpaper assets
This commit is contained in:
1 parent
4b5af6fd28
commit
ec0826fb01
3 files changed
+55
-13
No files matched your search
@@ -2,10 +2,10 @@ import { existsSync } from "node:fs";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { ipcMain } from "electron";
|
||||
import { app, ipcMain } from "electron";
|
||||
import { USER_DATA_PATH } from "../../appPaths";
|
||||
import { normalizePath } from "../utils";
|
||||
import { getAssetRootPath } from "../project/manager";
|
||||
import { normalizePath } from "../utils";
|
||||
|
||||
export function registerAssetHandlers() {
|
||||
async function resolveReadableLocalFilePath(filePath: string) {
|
||||
@@ -73,6 +73,10 @@ export function registerAssetHandlers() {
|
||||
|
||||
// Return base path for assets so renderer can resolve file:// paths in production
|
||||
ipcMain.handle("get-asset-base-path", () => {
|
||||
if (!app.isPackaged) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const assetPath = getAssetRootPath();
|
||||
return pathToFileURL(`${assetPath}${path.sep}`).toString();
|
||||
|
||||
@@ -1,5 +1,44 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getExportableVideoUrl, getRenderableAssetUrl, getRenderableVideoUrl } from "./assetPath";
|
||||
import {
|
||||
getAssetPath,
|
||||
getExportableVideoUrl,
|
||||
getRenderableAssetUrl,
|
||||
getRenderableVideoUrl,
|
||||
} from "./assetPath";
|
||||
|
||||
describe("getAssetPath", () => {
|
||||
beforeEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("uses the packaged asset directory from an HTTP renderer", async () => {
|
||||
vi.stubGlobal("window", {
|
||||
location: { protocol: "http:" },
|
||||
electronAPI: {
|
||||
getAssetBasePath: vi.fn(
|
||||
async () => "file:///Applications/Recordly.app/Contents/Resources/assets/",
|
||||
),
|
||||
},
|
||||
});
|
||||
|
||||
await expect(getAssetPath("wallpapers/tahoe-light.jpg")).resolves.toBe(
|
||||
"file:///Applications/Recordly.app/Contents/Resources/assets/wallpapers/tahoe-light.jpg",
|
||||
);
|
||||
});
|
||||
|
||||
it("uses root-relative assets when the dev server has no packaged asset base", async () => {
|
||||
vi.stubGlobal("window", {
|
||||
location: { protocol: "http:" },
|
||||
electronAPI: {
|
||||
getAssetBasePath: vi.fn(async () => null),
|
||||
},
|
||||
});
|
||||
|
||||
await expect(getAssetPath("wallpapers/tahoe-light.jpg")).resolves.toBe(
|
||||
"/wallpapers/tahoe-light.jpg",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getRenderableAssetUrl", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
+9
-10
@@ -59,19 +59,13 @@ export async function getAssetPath(relativePath: string): Promise<string> {
|
||||
const isWebContext =
|
||||
typeof window !== "undefined" && Boolean(window.location?.protocol?.startsWith("http"));
|
||||
|
||||
if (isWebContext) {
|
||||
return `/${encodedRelativePath}`;
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof window !== "undefined") {
|
||||
if (typeof window.electronAPI?.getAssetBasePath === "function") {
|
||||
const base = await window.electronAPI.getAssetBasePath();
|
||||
if (!base) {
|
||||
throw new Error(`Failed to resolve asset base path for ${relativePath}`);
|
||||
if (base) {
|
||||
return new URL(encodedRelativePath, ensureTrailingSlash(base)).toString();
|
||||
}
|
||||
|
||||
return new URL(encodedRelativePath, ensureTrailingSlash(base)).toString();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -80,8 +74,13 @@ export async function getAssetPath(relativePath: string): Promise<string> {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for web/dev server: public/wallpapers are served at '/wallpapers/...'
|
||||
return `/${encodedRelativePath}`;
|
||||
if (isWebContext) {
|
||||
// Dev and browser contexts serve public assets from the site root. Packaged
|
||||
// Electron windows resolve above to the single extraResources asset copy.
|
||||
return `/${encodedRelativePath}`;
|
||||
}
|
||||
|
||||
throw new Error(`Failed to resolve asset base path for ${relativePath}`);
|
||||
}
|
||||
|
||||
const BASE64_CHUNK_SIZE = 0x8000;
|
||||
|
||||
Reference in new issue
Block a user