|
@@ -101,15 +101,38 @@ def tags(cur):
|
|
|
return get_tags(cur, request.query)
|
|
|
|
|
|
CLIP_SIZE_LIMIT = 65535
|
|
|
+SCHEME = "http://"
|
|
|
+HOST = ""
|
|
|
+DOMAIN = "0.0.0.0"
|
|
|
+PORT = 6772
|
|
|
+LOCATION = SCHEME + (f"{HOST}." if HOST else "") + DOMAIN + (f":{PORT}" if PORT else "")
|
|
|
|
|
|
@route('/clip', method=['GET', 'POST'])
|
|
|
+@route('/clip/', method=['GET', 'POST'])
|
|
|
def clip():
|
|
|
if request.method == 'GET':
|
|
|
- if request.query:
|
|
|
- return redirect('/clip')
|
|
|
+ _hash = request.params.hash
|
|
|
+ if _hash:
|
|
|
+ _hash = normalize_base32(_hash)
|
|
|
+ content = validate(_hash).decode('utf-8')
|
|
|
+ else:
|
|
|
+ content = None
|
|
|
+ link = f'{LOCATION}/clip/{_hash}' if content else f'{LOCATION}/clip'
|
|
|
response.content_type = 'text/html; charset=utf-8'
|
|
|
- form = template('clip-form', action='/clip', method='post', content=None)
|
|
|
- return template('paste', form=form, link=None)
|
|
|
+ form = template(
|
|
|
+ 'clip-form',
|
|
|
+ action='/clip',
|
|
|
+ method='post',
|
|
|
+ content=content,
|
|
|
+ disabled=True if content else False
|
|
|
+ )
|
|
|
+ return template(
|
|
|
+ 'paste',
|
|
|
+ form=form,
|
|
|
+ link=link,
|
|
|
+ disabled=True if content else False,
|
|
|
+ download=f'/clip/{_hash}' if content else None
|
|
|
+ )
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
if 'paste' not in request.params:
|
|
@@ -130,24 +153,34 @@ def clip():
|
|
|
|
|
|
form = template('clip-form', action='/clip', method='post', content=content)
|
|
|
response.content_type = 'text/html; charset=utf-8'
|
|
|
- return HTTPResponse(template('paste', form=form, link=f'/clip/{_b32}'), 201)
|
|
|
+
|
|
|
+ return redirect(f'/clip?hash={_b32}')
|
|
|
|
|
|
-@route('/clip/<filename:path>', method='GET')
|
|
|
-def get_clip(filename):
|
|
|
- f = normalize_base32(filename)
|
|
|
-
|
|
|
- ret = static_file('/'.join([f,]*2) + '.file', root='app/rest/static')
|
|
|
+def validate(filename: str) -> bytes:
|
|
|
+ ret = static_file('/'.join([filename,]*2) + '.file', root='app/rest/static')
|
|
|
if isinstance(ret, HTTPError):
|
|
|
- return ret
|
|
|
+ return abort(404, f"No such paste: {filename}")
|
|
|
|
|
|
if ret.content_length > CLIP_SIZE_LIMIT:
|
|
|
return abort(418, f"Paste size exceeds {CLIP_SIZE_LIMIT}")
|
|
|
|
|
|
- content: str = ret.body.read() if isinstance(ret.body, BufferedReader) else ret.body.encode('utf-8')
|
|
|
+ content: bytes = ret.body.read() if isinstance(ret.body, BufferedReader) else ret.body.encode('utf-8')
|
|
|
|
|
|
_bytes = blake(content, person='clip'.encode('utf-8'))
|
|
|
_b32 = bytes_to_base32(_bytes)
|
|
|
- if _b32 != f:
|
|
|
+ if _b32 != filename:
|
|
|
return abort(410, f"Paste content differs")
|
|
|
+ return content
|
|
|
+
|
|
|
+@route('/clip/<filename:path>', method='GET')
|
|
|
+def get_clip(filename):
|
|
|
+ filename = normalize_base32(filename)
|
|
|
+ if not request.params.raw.lower() == 'true':
|
|
|
+
|
|
|
+ return redirect(f'/clip?hash={filename}')
|
|
|
+
|
|
|
+ ret = validate(filename)
|
|
|
+ if isinstance(ret, HTTPError):
|
|
|
+ return ret
|
|
|
|
|
|
- return HTTPResponse(content, 200)
|
|
|
+ return static_file('/'.join([filename,]*2) + '.file', root='app/rest/static')
|