mirror of
https://git.ugnet.gay/CrossTalk/azul.git
synced 2026-11-25 08:40:05 +00:00
0.6 final
This commit is contained in:
1 parent
7c0581eb97
commit
b89e1932bb
160 files changed
+20534
-4664
No files matched your search
+34
-13
@@ -6,9 +6,10 @@ from pathlib import Path
|
||||
from typing import Optional, Tuple
|
||||
|
||||
STORAGE_BASE = Path('storage/dp')
|
||||
PHOTO_STORAGE_BASE = Path('storage/photos')
|
||||
THUMB_SIZE = (21, 21)
|
||||
MSN_SIZE = (96, 96)
|
||||
1
|
||||
IM_SIZE = (96, 96)
|
||||
|
||||
def compute_md5(data: bytes) -> str:
|
||||
return hashlib.md5(data).hexdigest()[:16]
|
||||
|
||||
@@ -33,30 +34,50 @@ def store_avatar_file(uuid: str, data: bytes, ext: str) -> str:
|
||||
|
||||
full_path = avatar_dir / f'{md5}.{ext}'
|
||||
thumb_path = avatar_dir / f'{md5}_thumb.png'
|
||||
msn_path = avatar_dir / f'{md5}_msn.png'
|
||||
im_path = avatar_dir / f'{md5}_im.png'
|
||||
|
||||
if not full_path.exists():
|
||||
full_path.write_bytes(data)
|
||||
|
||||
if not thumb_path.exists() or not msn_path.exists():
|
||||
if not thumb_path.exists() or not im_path.exists():
|
||||
try:
|
||||
img = Image.open(BytesIO(data))
|
||||
if not thumb_path.exists():
|
||||
img.resize(THUMB_SIZE).save(str(thumb_path))
|
||||
if not msn_path.exists():
|
||||
img.resize(MSN_SIZE).save(str(msn_path))
|
||||
if not im_path.exists():
|
||||
img.resize(IM_SIZE).save(str(im_path))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return md5
|
||||
|
||||
|
||||
def store_photo_file(uuid: str, data: bytes, ext: str) -> str:
|
||||
photo_dir = PHOTO_STORAGE_BASE / uuid
|
||||
photo_dir.mkdir(parents=True, exist_ok=True)
|
||||
name = f'{compute_md5(data)}.{ext}'
|
||||
path = photo_dir / name
|
||||
if not path.exists():
|
||||
path.write_bytes(data)
|
||||
return name
|
||||
|
||||
def get_photo_data(uuid: str, name: str) -> Optional[Tuple[bytes, str]]:
|
||||
path = PHOTO_STORAGE_BASE / uuid / name
|
||||
if not path.is_file():
|
||||
return None
|
||||
try:
|
||||
data = path.read_bytes()
|
||||
except OSError:
|
||||
return None
|
||||
return data, 'image/' + path.suffix.lstrip('.').lower()
|
||||
|
||||
|
||||
def find_avatar_file(
|
||||
uuid: str,
|
||||
md5: str,
|
||||
*,
|
||||
small: bool = False,
|
||||
msn: bool = False,
|
||||
im: bool = False,
|
||||
) -> Optional[Path]:
|
||||
avatar_dir = get_avatar_dir(uuid)
|
||||
if not avatar_dir.is_dir():
|
||||
@@ -67,10 +88,10 @@ def find_avatar_file(
|
||||
if thumb.is_file():
|
||||
return thumb
|
||||
|
||||
elif msn:
|
||||
msn_file = avatar_dir / f'{md5}_msn.png'
|
||||
if msn_file.is_file():
|
||||
return msn_file
|
||||
elif im:
|
||||
im_file = avatar_dir / f'{md5}_im.png'
|
||||
if im_file.is_file():
|
||||
return im_file
|
||||
|
||||
for p in avatar_dir.iterdir():
|
||||
if p.is_file() and p.stem == md5:
|
||||
@@ -84,12 +105,12 @@ def get_avatar_data(
|
||||
md5: Optional[str],
|
||||
*,
|
||||
small: bool = False,
|
||||
msn: bool = False,
|
||||
im: bool = False,
|
||||
) -> Optional[Tuple[bytes, str]]:
|
||||
if not md5:
|
||||
return None
|
||||
|
||||
path = find_avatar_file(uuid, md5, small=small, msn=msn)
|
||||
path = find_avatar_file(uuid, md5, small=small, im=im)
|
||||
if path is None:
|
||||
return None
|
||||
|
||||
|
||||
Reference in new issue
Block a user