mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-24 20:41:01 +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,17 +121,27 @@ async function writePdfStream(dir, hash, buffers) {
|
||||||
// ETags used for client side caching via browser internals.
|
// ETags used for client side caching via browser internals.
|
||||||
return false
|
return false
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
const file = await fs.promises.open(filename, 'w')
|
const atomicWriteFilename = filename + '~'
|
||||||
|
const file = await fs.promises.open(atomicWriteFilename, 'w')
|
||||||
if (Settings.enablePdfCachingDark) {
|
if (Settings.enablePdfCachingDark) {
|
||||||
// Write an empty file in dark mode.
|
// Write an empty file in dark mode.
|
||||||
buffers = []
|
buffers = []
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
for (const buffer of buffers) {
|
try {
|
||||||
await file.write(buffer)
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue