Compare commits
No commits in common. "529ebd23fc0b50e2606ecddc5e1199774dd18384" and "6312dfae47b09753675b038798cf38f00311e772" have entirely different histories.
529ebd23fc
...
6312dfae47
@ -1,55 +1,9 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import uuid
|
|
||||||
|
|
||||||
DEFAULT_SALT = "snekker-de-snek-"
|
DEFAULT_SALT = b"snekker-de-snek-"
|
||||||
DEFAULT_NS = "snekker-de-snek-"
|
|
||||||
|
|
||||||
|
|
||||||
class UIDNS:
|
async def hash(data, salt=DEFAULT_SALT):
|
||||||
def __init__(self, name: str) -> None:
|
|
||||||
"""Initialize UIDNS with a name."""
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def bytes(self) -> bytes:
|
|
||||||
"""Return the bytes representation of the name."""
|
|
||||||
return self.name.encode()
|
|
||||||
|
|
||||||
|
|
||||||
def uid(value: str = None, ns: str = DEFAULT_NS) -> str:
|
|
||||||
"""Generate a UUID based on the provided value and namespace.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
value (str): The value to generate the UUID from. If None, a new UUID is created.
|
|
||||||
ns (str): The namespace to use for UUID generation.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The generated UUID as a string.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
ns = ns.decode()
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
if not value:
|
|
||||||
value = str(uuid.uuid4())
|
|
||||||
try:
|
|
||||||
value = value.decode()
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return str(uuid.uuid5(UIDNS(ns), value))
|
|
||||||
|
|
||||||
|
|
||||||
async def hash(data: str, salt: str = DEFAULT_SALT) -> str:
|
|
||||||
"""Hash the given data with the specified salt using SHA-256.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
data (str): The data to hash.
|
|
||||||
salt (str): The salt to use for hashing.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The hexadecimal representation of the hashed data.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
data = data.encode(errors="ignore")
|
data = data.encode(errors="ignore")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -64,14 +18,5 @@ async def hash(data: str, salt: str = DEFAULT_SALT) -> str:
|
|||||||
return obj.hexdigest()
|
return obj.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
async def verify(string: str, hashed: str) -> bool:
|
async def verify(string: str, hashed: str):
|
||||||
"""Verify if the given string matches the hashed value.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
string (str): The string to verify.
|
|
||||||
hashed (str): The hashed value to compare against.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: True if the string matches the hashed value, False otherwise.
|
|
||||||
"""
|
|
||||||
return await hash(string) == hashed
|
return await hash(string) == hashed
|
||||||
|
|||||||
@ -18,6 +18,8 @@ from aiohttp import web
|
|||||||
|
|
||||||
from snek.system.view import BaseView
|
from snek.system.view import BaseView
|
||||||
|
|
||||||
|
UPLOAD_DIR = pathlib.Path("./drive")
|
||||||
|
|
||||||
|
|
||||||
class UploadView(BaseView):
|
class UploadView(BaseView):
|
||||||
|
|
||||||
@ -35,12 +37,8 @@ class UploadView(BaseView):
|
|||||||
reader = await self.request.multipart()
|
reader = await self.request.multipart()
|
||||||
files = []
|
files = []
|
||||||
|
|
||||||
user_uid = self.request.session.get("uid")
|
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
upload_dir = await self.services.user.get_home_folder(user_uid)
|
|
||||||
upload_dir = upload_dir.joinpath("upload")
|
|
||||||
upload_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
channel_uid = None
|
channel_uid = None
|
||||||
|
|
||||||
drive = await self.services.drive.get_or_create(
|
drive = await self.services.drive.get_or_create(
|
||||||
@ -70,17 +68,17 @@ class UploadView(BaseView):
|
|||||||
|
|
||||||
name = str(uuid.uuid4()) + pathlib.Path(filename).suffix
|
name = str(uuid.uuid4()) + pathlib.Path(filename).suffix
|
||||||
|
|
||||||
file_path = upload_dir.joinpath(name)
|
file_path = pathlib.Path(UPLOAD_DIR).joinpath(name)
|
||||||
files.append(file_path)
|
files.append(file_path)
|
||||||
|
|
||||||
async with aiofiles.open(str(file_path), "wb") as f:
|
async with aiofiles.open(str(file_path.absolute()), "wb") as f:
|
||||||
while chunk := await field.read_chunk():
|
while chunk := await field.read_chunk():
|
||||||
await f.write(chunk)
|
await f.write(chunk)
|
||||||
|
|
||||||
drive_item = await self.services.drive_item.create(
|
drive_item = await self.services.drive_item.create(
|
||||||
drive["uid"],
|
drive["uid"],
|
||||||
filename,
|
filename,
|
||||||
str(file_path),
|
str(file_path.absolute()),
|
||||||
file_path.stat().st_size,
|
file_path.stat().st_size,
|
||||||
file_path.suffix,
|
file_path.suffix,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user