mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-24 20:31:00 +00:00
Merge pull request #235 from overleaf/jpa-atomic-writes
[ContentCacheManager] write streams to disk atomically
This commit is contained in:
commit
e787106eed
1 changed files with 15 additions and 5 deletions
|
@ -121,11 +121,13 @@ 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 {
|
||||
try {
|
||||
for (const buffer of buffers) {
|
||||
await file.write(buffer)
|
||||
|
@ -133,6 +135,14 @@ async function writePdfStream(dir, hash, buffers) {
|
|||
} finally {
|
||||
await file.close()
|
||||
}
|
||||
await fs.promises.rename(atomicWriteFilename, filename)
|
||||
} catch (err) {
|
||||
try {
|
||||
await fs.promises.unlink(atomicWriteFilename)
|
||||
} catch (_) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue