diff --git a/services/clsi/app/js/ContentCacheManager.js b/services/clsi/app/js/ContentCacheManager.js index da40413651..38a1fa4350 100644 --- a/services/clsi/app/js/ContentCacheManager.js +++ b/services/clsi/app/js/ContentCacheManager.js @@ -121,17 +121,27 @@ async function writePdfStream(dir, hash, buffers) { // ETags used for client side caching via browser internals. return false } catch (e) {} - const file = await fs.promises.open(filename, 'w') + const atomicWriteFilename = filename + '~' + const file = await fs.promises.open(atomicWriteFilename, 'w') if (Settings.enablePdfCachingDark) { // Write an empty file in dark mode. buffers = [] } try { - for (const buffer of buffers) { - await file.write(buffer) + try { + for (const buffer of buffers) { + await file.write(buffer) + } + } finally { + await file.close() + } + await fs.promises.rename(atomicWriteFilename, filename) + } catch (err) { + try { + await fs.promises.unlink(atomicWriteFilename) + } catch (_) { + throw err } - } finally { - await file.close() } return true }