mirror of
https://github.com/overleaf/overleaf.git
synced 2025-03-13 10:32:24 +00:00
[ContentCacheManager] write streams to disk atomically
Use an intermediate file for writing to disk, then rename to the target.
This commit is contained in:
parent
b456ea726d
commit
6b9c8bced6
1 changed files with 15 additions and 5 deletions
|
@ -100,17 +100,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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue