|
@@ -25,6 +25,22 @@ DOMAIN = "shandan.one"
|
|
|
PORT = ""
|
|
|
LOCATION = SCHEME + (f"{HOST}." if HOST else "") + DOMAIN + (f":{PORT}" if PORT else "")
|
|
|
|
|
|
+@route('/preview', method=['GET'])
|
|
|
+def get_preview():
|
|
|
+ link = request.params.link
|
|
|
+ if not link:
|
|
|
+ return dumps(None)
|
|
|
+ try:
|
|
|
+ page = link_preview(link, parser="lxml")
|
|
|
+ return dumps({
|
|
|
+ 'title': page.title,
|
|
|
+ 'img': page.absolute_image,
|
|
|
+ 'domain': page.site_name,
|
|
|
+ 'link': link,
|
|
|
+ })
|
|
|
+ except:
|
|
|
+ return dumps(None)
|
|
|
+
|
|
|
@route('/hash', method=['POST'])
|
|
|
def get_hash():
|
|
|
data = dict(map(
|
|
@@ -52,21 +68,21 @@ def get_qr():
|
|
|
def send_static(filename):
|
|
|
return static_file(filename, root='rest/static')
|
|
|
|
|
|
-@route('/<route:re:(upload|goto)>/open')
|
|
|
-def _get_clip(route):
|
|
|
- return template('form-open', tool=route, action=f'/{route}', method='get')
|
|
|
+@route('/upload/open')
|
|
|
+def _get_clip():
|
|
|
+ return template('form-open', tool='upload', action=f'/upload', method='get')
|
|
|
|
|
|
-@route('/<route:clip/open')
|
|
|
+@route('/<route:re:(clip|goto)>/open')
|
|
|
def _get_clip(route):
|
|
|
- return redirect(f'/clip/open.sql')
|
|
|
+ return redirect(f'/{route}/open.sql')
|
|
|
|
|
|
-@route('/clip', method=['GET', 'POST'])
|
|
|
-def clip():
|
|
|
- return redirect(f'/clip.sql')
|
|
|
+@route('/<route:re:(clip|goto)>', method=['GET', 'POST'])
|
|
|
+def clip(route):
|
|
|
+ return redirect(f'/{route}.sql')
|
|
|
|
|
|
-@route('/clip/<filename:path>', method='GET')
|
|
|
-def get_clip(filename):
|
|
|
- return redirect(f'/clip.sql?hash={filename}')
|
|
|
+@route('/<route:re:(clip|goto)>/<filename:path>', method='GET')
|
|
|
+def get_clip(route, filename):
|
|
|
+ return redirect(f'/{route}.sql?hash={filename}&go=true')
|
|
|
|
|
|
@route('/upload', method=['GET', 'POST'])
|
|
|
def upload():
|
|
@@ -131,74 +147,5 @@ def get_upload(filename):
|
|
|
return validate_file(filename, root='rest/static/files', download=download, mimetype=mimetype)
|
|
|
|
|
|
|
|
|
-@route('/goto', method=['GET', 'POST'])
|
|
|
-def goto():
|
|
|
-
|
|
|
- if request.method == 'GET':
|
|
|
- _hash = request.params.hash
|
|
|
- if _hash:
|
|
|
- _hash = normalize_base32(_hash)
|
|
|
- content = validate(_hash, 'goto', root='rest/static/files').decode('utf-8')
|
|
|
- else:
|
|
|
- content = None
|
|
|
-
|
|
|
- if content and request.params.go == 'true':
|
|
|
- target = validate_url(content)
|
|
|
- return redirect(target)
|
|
|
-
|
|
|
- link = f'{LOCATION}/goto/{_hash}' if content else f'{LOCATION}/goto'
|
|
|
- disabled = True if content else False
|
|
|
- response.content_type = 'text/html; charset=utf-8'
|
|
|
- form = template(
|
|
|
- 'form-goto',
|
|
|
- action='/goto',
|
|
|
- method='post',
|
|
|
- content=content,
|
|
|
- disabled=disabled
|
|
|
- )
|
|
|
- preview = dict()
|
|
|
- if content:
|
|
|
- try:
|
|
|
- page = link_preview(link, parser="lxml")
|
|
|
- preview['title'] = page.title
|
|
|
- preview['img'] = page.absolute_image
|
|
|
- preview['domain'] = page.site_name
|
|
|
- preview['link'] = content
|
|
|
- except:
|
|
|
- pass
|
|
|
-
|
|
|
- return template(
|
|
|
- 'goto',
|
|
|
- form=form,
|
|
|
- qr=f'{LOCATION}/goto/{_hash}.qr' if content else f'{LOCATION}/static/goto/qr.svg',
|
|
|
- link=link,
|
|
|
- disabled=disabled,
|
|
|
- preview=preview,
|
|
|
- )
|
|
|
-
|
|
|
- if request.method == 'POST':
|
|
|
- content = validate_parameter(request, 'url')
|
|
|
- _b32 = save(content, 'goto', LOCATION, root='rest/static/files')
|
|
|
-
|
|
|
-
|
|
|
- _ = validate_url(content.decode('utf-8'))
|
|
|
- return redirect(f'/goto?hash={_b32}')
|
|
|
-
|
|
|
-
|
|
|
-@route('/goto/<filename:path>', method='GET')
|
|
|
-def redirect_goto(filename):
|
|
|
- ext = 'file'
|
|
|
- if filename and filename.endswith('.qr'):
|
|
|
- filename, ext = filename.split('.', 1)
|
|
|
-
|
|
|
- filename = filename and normalize_base32(filename)
|
|
|
- path = f'{filename}/{filename}.{ext}'
|
|
|
-
|
|
|
- if ext == 'qr':
|
|
|
- return static_file(path, root='rest/static/files', mimetype='image/svg+xml')
|
|
|
-
|
|
|
- return redirect(f'/goto?hash={filename}&go=true')
|
|
|
-
|
|
|
-
|
|
|
@route('/<any>/', method='GET')
|
|
|
def redirect_trailing_slash(any): return redirect(f'/{any}')
|