-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstorage_helper.py
More file actions
36 lines (27 loc) · 829 Bytes
/
Copy pathstorage_helper.py
File metadata and controls
36 lines (27 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Copyright (c) 2022 - Itz-fork
from sys import getsizeof
from random import sample
from aiofiles import open
from os.path import splitext, isfile
from secrets import token_urlsafe
from ..config.storageConf import NX_Strg
async def gen_name(file):
while True:
name = "".join([
NX_Strg["path_to"],
token_urlsafe(NX_Strg["length"]),
splitext(file.filename)[1]])
if not isfile(name):
return name
async def write_file(file):
fn = await gen_name(file)
async with open(fn, mode="wb") as pp:
cnt = await file.read()
# Checks file size
if getsizeof(cnt) > NX_Strg["limit"]:
raise FileSizeIsTooLarge("Dammit, too large")
await pp.write(cnt)
return fn
# Errors
class FileSizeIsTooLarge(Exception):
pass