|
@@ -27,6 +27,7 @@ import os
|
|
|
from bottle import static_file, response, HTTPError, abort, LocalRequest, HTTPResponse
|
|
|
from urllib.parse import urlparse, quote, quote_plus
|
|
|
from .hash_util import blake_file, bytes_to_base32, blake
|
|
|
+import bottle
|
|
|
|
|
|
|
|
|
URL_MUST_ESCAPE = bytes([
|
|
@@ -43,24 +44,34 @@ URL_MUST_ESCAPE = bytes([
|
|
|
URL_SAFE = bytes(( i for i in range(int('0xff',0)+1) if i not in map(int, URL_MUST_ESCAPE) ))
|
|
|
|
|
|
CLIP_SIZE_LIMIT = 65535
|
|
|
-def validate(filename: str, tool: str, root='rest/static/files') -> bytes:
|
|
|
+def _validate(filename: str, tool: str, root='rest/static/files') -> bytes:
|
|
|
ret = static_file('/'.join([filename,]*2) + '.file', root=root)
|
|
|
if isinstance(ret, HTTPError):
|
|
|
- return abort(404, f"No such `{tool.title()}`: {filename}")
|
|
|
+ return (
|
|
|
+ 'abort', 404, f"No such `{tool.title()}`: {filename}"
|
|
|
+ )
|
|
|
if ret.status_code == 304:
|
|
|
return ret
|
|
|
|
|
|
if ret.content_length > CLIP_SIZE_LIMIT:
|
|
|
- return abort(418, f"{tool.title()} size exceeds {CLIP_SIZE_LIMIT}")
|
|
|
+ return (
|
|
|
+ 'abort', 418, f"{tool.title()} size exceeds {CLIP_SIZE_LIMIT}"
|
|
|
+ )
|
|
|
|
|
|
content: bytes = ret.body.read() if isinstance(ret.body, BufferedReader) else ret.body.encode('utf-8')
|
|
|
|
|
|
_bytes = blake(content, person=tool.encode('utf-8'))
|
|
|
_b32 = bytes_to_base32(_bytes)
|
|
|
if _b32 != filename:
|
|
|
- return abort(410, f"{tool.title()} content differs")
|
|
|
+ return (
|
|
|
+ 'abort', 410, f"{tool.title()} content differs"
|
|
|
+ )
|
|
|
return content
|
|
|
|
|
|
+def validate(filename: str, tool: str, root='rest/static/files') -> bytes:
|
|
|
+ ret = _validate(filename, tool, root)
|
|
|
+ return bottle.get(ret[0])(*ret[1:]) if isinstance(ret, tuple) else ret
|
|
|
+
|
|
|
|
|
|
def get_filename(filename: str, root: str = 'rest/static/files'):
|
|
|
path = '/'.join([filename,]*2)
|