Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion routes/create/controllers/h5pPreviewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ router.get('/core/h5p-core.js', asyncHandler(async (req, res) => {
res.type('application/javascript').sendFile(corePath);
}));

/**
* GET /libs/* — Serve H5P library files (JS, CSS, fonts, images).
* Uses the existing /api/create/h5p-preview/ route so no nginx config is needed.
*/
router.get('/libs/*', (req, res) => {
const requestedPath = req.params[0];

// Security: prevent directory traversal
if (requestedPath.includes('..')) {
return res.status(400).send('Invalid path');
}

const filePath = path.join(H5P_LIBS_DIR, requestedPath);
res.sendFile(filePath, (err) => {
if (err) {
res.status(404).send('Library file not found');
}
});
});

/**
* GET /quiz/:quizId/render — Render quiz questions as real H5P content in-browser.
* Each question gets its own numbered header + H5P.newRunnable() instance.
Expand Down Expand Up @@ -169,7 +189,9 @@ body { margin:0; padding:40px; font-family:-apple-system,BlinkMacSystemFont,"Seg
// Read directly from h5p-libs (no temp dir or symlinks needed).
const { cssFiles, jsFiles } = await resolveLibraryDependencies(syntheticH5pJson);

const libBasePath = '/h5p-libs';
// Serve library files through the existing /api route — works on all environments
// without needing extra nginx/proxy configuration.
const libBasePath = '/api/create/h5p-preview/libs';
const cssTags = cssFiles.map(f => ` <link rel="stylesheet" href="${libBasePath}/${f}">`).join('\n');
const jsTags = jsFiles.map(f => ` <script src="${libBasePath}/${f}" onerror="window.__h5pLoadErrors=(window.__h5pLoadErrors||[]);window.__h5pLoadErrors.push('${f.replace(/'/g, "\\'")}')"></script>`).join('\n');

Expand Down
3 changes: 0 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ app.post('/Shibboleth.sso/SAML2/POST', (req, res) => {
// Static serving for extracted H5P preview files (before API routes to avoid rate limiting)
app.use('/h5p-preview-files', express.static(path.join(__dirname, 'routes', 'create', 'uploads', 'h5p-preview')));

// Static serving for H5P library files (used by quiz preview rendering)
app.use('/h5p-libs', express.static(path.join(__dirname, 'routes', 'create', 'h5p-libs')));

// Mount the API router FIRST (before static files)
app.use('/api/create', createRoutes);

Expand Down
5 changes: 0 additions & 5 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ export default defineConfig(({ mode }) => ({
target: 'http://localhost:8051',
changeOrigin: true,
secure: false,
},
'/h5p-libs': {
target: 'http://localhost:8051',
changeOrigin: true,
secure: false,
}
}
},
Expand Down