44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { normalizeWorkspaceRelativePath } from './mindspace-pages.mjs';
|
|
import { resolvePublishDir } from './user-publish.mjs';
|
|
|
|
export { normalizeWorkspaceRelativePath };
|
|
|
|
export async function readWorkspacePublishHtml(h5Root, userId, relativePath) {
|
|
const normalized = normalizeWorkspaceRelativePath(relativePath);
|
|
if (!h5Root || !userId || !normalized) return null;
|
|
const filePath = path.join(resolvePublishDir(h5Root, { id: userId }), ...normalized.split('/'));
|
|
try {
|
|
return await fs.promises.readFile(filePath, 'utf8');
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function resolvePageWorkspaceRelativePath(snapshot) {
|
|
if (!snapshot || typeof snapshot !== 'object') return null;
|
|
return normalizeWorkspaceRelativePath(snapshot.relative_path) || null;
|
|
}
|
|
|
|
export function resolveAssetWorkspaceRelativePath({
|
|
categoryCode = null,
|
|
originalFilename = null,
|
|
explicitPath = null,
|
|
} = {}) {
|
|
const explicit = normalizeWorkspaceRelativePath(explicitPath);
|
|
if (explicit) return explicit;
|
|
const filename = String(originalFilename ?? '')
|
|
.replace(/\\/g, '/')
|
|
.replace(/^\/+/, '');
|
|
if (!filename) return null;
|
|
if (/^(?:public|oa)\//i.test(filename)) {
|
|
return normalizeWorkspaceRelativePath(filename);
|
|
}
|
|
const category = String(categoryCode ?? '').trim();
|
|
if (category === 'public' || category === 'oa') {
|
|
return normalizeWorkspaceRelativePath(`${category}/${filename}`);
|
|
}
|
|
return null;
|
|
}
|