Write user files and media atomically

This commit is contained in:
miruka
2020-03-13 04:35:51 -04:00
parent 9d3e2dbfc4
commit 190eb58187
4 changed files with 33 additions and 9 deletions

View File

@@ -13,11 +13,11 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any, DefaultDict, Dict, Optional
from urllib.parse import urlparse
import aiofiles
import nio
from PIL import Image as PILImage
from .utils import Size
import nio
from .utils import Size, atomic_write
if TYPE_CHECKING:
from .backend import Backend
@@ -138,7 +138,7 @@ class Media:
self.local_path.parent.mkdir(parents=True, exist_ok=True)
async with aiofiles.open(self.local_path, "wb") as file:
async with atomic_write(self.local_path, binary=True) as file:
await file.write(data)
return self.local_path
@@ -212,7 +212,7 @@ class Media:
media.local_path.parent.mkdir(parents=True, exist_ok=True)
if not media.local_path.exists() or overwrite:
async with aiofiles.open(media.local_path, "wb") as file:
async with atomic_write(media.local_path, binary=True) as file:
await file.write(data)
return media