Compare commits

...

4 Commits

1 changed files with 20 additions and 17 deletions

View File

@ -8,7 +8,7 @@ from collections import defaultdict
from Cryptodome.Cipher import AES
from Cryptodome.Util import Padding
from aiohttp import web
from aiohttp import web, hdrs
class AttrDict(dict):
@ -23,17 +23,17 @@ class AttrDict(dict):
def __getattr__(self, item):
return self.setdefault(item, AttrDict())
def cast_to_ad(d):
if not isinstance(d, AttrDict):
d = AttrDict(d)
for k, v in dict(d.items()).items():
if " " in k:
del d[k]
d[k.replace(" ", "_")] = v
if isinstance(v, dict):
d[k] = cast_to_ad(v)
return d
@staticmethod
def from_dict_recur(d):
if not isinstance(d, AttrDict):
d = AttrDict(d)
for k, v in dict(d.items()).items():
if " " in k:
del d[k]
d[k.replace(" ", "_")] = v
if isinstance(v, dict):
d[k] = AttrDict.from_dict_recur(v)
return d
def sizeof_fmt(num, suffix='B'):
@ -47,7 +47,7 @@ def sizeof_fmt(num, suffix='B'):
async def prepare(_, handler):
async def prepare_handler(req):
if 'acc' not in req.match_info:
return web.Response(text='internal server error', status=500)
return web.Response(text='bad request', status=400)
return await handler(req, req.match_info["acc"], file_db[req.match_info["acc"]])
return prepare_handler
@ -126,8 +126,10 @@ async def handle_download(req, acc, acc_db):
fhash = req.match_info.get('hash', '').split('.', 1)[0]
if fhash not in acc_db:
return web.Response(text='file not found', status=404)
return web.FileResponse(f"{conf.data_path}/{acc}/{fhash}_{acc_db[fhash]}", headers={
'CONTENT-DISPOSITION': f'inline;filename="{acc_db[fhash]}"'
hdrs.CACHE_CONTROL: "no-cache",
hdrs.CONTENT_DISPOSITION: f'inline;filename="{acc_db[fhash]}"'
})
@ -138,8 +140,9 @@ def main():
if not os.path.isdir(f'{conf.data_path}/{acc}'):
continue
for file in os.listdir(f"{conf.data_path}/{acc}"):
fhash, fname = file.split('_', 1)
file_db[acc][fhash] = fname
if "_" in file:
fhash, fname = file.split('_', 1)
file_db[acc][fhash] = fname
app = web.Application(middlewares=[prepare])
app.router.add_post(conf.prefix + '/post/{acc}', handle_upload)
@ -154,7 +157,7 @@ if __name__ == '__main__':
file_db = defaultdict(dict)
confname = sys.argv[1] if sys.argv[1:] and os.path.isfile(sys.argv[1]) else 'config.yaml'
with open(confname) as cf:
conf = cast_to_ad(yaml.load(cf))
conf = AttrDict.from_dict_recur(yaml.load(cf))
if conf.url_hash_len > 31:
raise ValueError('url_hash_len can\'t be bigger than 31')
if not set(conf.max_filesize.replace(' ', ''))\