fix: unique keyHash in download URL path + 2s reload delay for save callback
This commit is contained in:
@@ -147,11 +147,11 @@ router.get('/:templateId/config', (req, res) => {
|
||||
const docKey = generateDocKey(templateId, Date.now());
|
||||
const fileName = template.originalName || template.name + '.docx';
|
||||
|
||||
// Add file mtime as cache-buster to the document URL so ONLYOFFICE
|
||||
// always fetches the latest version from our backend.
|
||||
// The mtime changes every time the file is saved, so the URL is
|
||||
// different after each save, forcing ONLYOFFICE to re-download.
|
||||
const documentUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/download?mtime=${fileMtime}&cb=${Date.now()}`;
|
||||
// Add the doc key to the download URL path so ONLYOFFICE treats
|
||||
// every edit session as a completely fresh document URL.
|
||||
// Query params alone aren't enough — ONLYOFFICE may ignore them for caching.
|
||||
const keyHash = docKey.split('_').pop();
|
||||
const documentUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/download/${keyHash}?mtime=${fileMtime}&cb=${Date.now()}`;
|
||||
const callbackUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/callback`;
|
||||
|
||||
const config = {
|
||||
@@ -223,9 +223,35 @@ router.get('/:templateId/config', (req, res) => {
|
||||
|
||||
// ============================================================
|
||||
// ENDPOINT: GET /api/onlyoffice/:templateId/download
|
||||
// ONLYOFFICE fetches the .docx file from this URL
|
||||
// ONLYOFFICE fetches the .docx file from this URL.
|
||||
// The optional :keyHash path param provides per-session cache-busting.
|
||||
// ============================================================
|
||||
|
||||
router.get('/:templateId/download/:keyHash', (req, res) => {
|
||||
const templateId = req.params.templateId;
|
||||
const registry = loadRegistry();
|
||||
const template = registry.find(t => t.id === templateId);
|
||||
|
||||
if (!template) {
|
||||
return res.status(404).json({ error: 'Template not found' });
|
||||
}
|
||||
|
||||
if (!fs.existsSync(template.path)) {
|
||||
return res.status(404).json({ error: 'Template file missing' });
|
||||
}
|
||||
|
||||
const fileName = template.originalName || template.name + '.docx';
|
||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
|
||||
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
||||
res.setHeader('Pragma', 'no-cache');
|
||||
res.setHeader('Expires', '0');
|
||||
|
||||
const fileStream = fs.createReadStream(template.path);
|
||||
fileStream.pipe(res);
|
||||
});
|
||||
|
||||
// Fallback route without keyHash (backward compatibility)
|
||||
router.get('/:templateId/download', (req, res) => {
|
||||
const templateId = req.params.templateId;
|
||||
const registry = loadRegistry();
|
||||
|
||||
Reference in New Issue
Block a user