Write user files and media atomically
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user