Răsfoiți Sursa

goto working

Pi 4 luni în urmă
părinte
comite
d26e4e254b

+ 27 - 80
rest/pyapi.py

@@ -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 but save content unmodified
-        _ = 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}')

+ 10 - 5
util-sqlpage/goto.sql

@@ -1,7 +1,16 @@
+SET hash = CASE $hash = ''
+  WHEN TRUE THEN NULL
+  ELSE $hash
+END;
+SET redirect = COALESCE($go, '') = 'true' AND $hash IS NOT NULL;
+SELECT 'dynamic' AS component, sqlpage.run_sql('goto/redirect.sql') AS properties
+WHERE $redirect
+;
+
 SELECT 'shell' AS component
 , 'dark' AS theme
 , 'GoTo' AS title
-, 'https://shandan.one/static/goto/goto-favicon_square.svg' AS image
+, 'https://shandan.one/static/goto/chain-link2fr-3-2.svg' AS image
 , 'https://cdn.jsdelivr.net/npm/purecss@2.1.0/build/pure-min.css' AS css
 ;
 
@@ -10,10 +19,6 @@ SET tabler_color = 'google';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/Style.sql') AS properties;
 
 SET tool = 'goto';
-SET hash = CASE $hash = ''
-  WHEN TRUE THEN NULL
-  ELSE $hash
-END;
 SET inner = CASE COALESCE($content,'') <> '' AND COALESCE($action, '') = 'Shrtn It!'
   WHEN TRUE THEN 'goto/save.sql'
   ELSE CASE $hash IS NULL

+ 5 - 1
util-sqlpage/goto/form.sql

@@ -29,6 +29,10 @@ SELECT 'content' AS name
 , '' AS label
 , 'input' AS type
 , $content AS value
+, 'Paste URL here...' AS placeholder
 WHERE NOT $view
 ;
---TODO: preview
+
+SELECT 'dynamic' AS component, sqlpage.run_sql('goto/preview.sql') AS properties
+WHERE $content <> ''
+;

+ 1 - 1
util-sqlpage/goto/open.sql

@@ -1,7 +1,7 @@
 SELECT 'shell' AS component
 , 'dark' AS theme
 , 'Goto' AS title
-, 'https://shandan.one/static/goto/goto-favicon_square.svg' AS image
+, 'https://shandan.one/static/goto/chain-link2fr-3-2.svg' AS image
 , 'https://cdn.jsdelivr.net/npm/purecss@2.1.0/build/pure-min.css' AS css
 ;
 SELECT 'text' AS component

+ 15 - 0
util-sqlpage/goto/preview.sql

@@ -0,0 +1,15 @@
+SET content = COALESCE($content, '');
+SET url = 'https://shandan.one/preview?link=' || sqlpage.url_encode($content);
+SET api_results = sqlpage.fetch($url);
+SET title = $api_results->>'title';
+SET image = $api_results->>'img';
+SET domain = $api_results->>'domain';
+SELECT 'card' AS component
+, 1 AS columns
+;
+SELECT $content AS link
+, $title AS title
+, $image AS top_image
+, $domain AS description_md
+, $tabler_color AS color
+;

+ 12 - 0
util-sqlpage/goto/redirect.sql

@@ -0,0 +1,12 @@
+SET content = (SELECT content FROM goto WHERE hash = $hash);
+
+SET icon = 'error-404';
+SET status = '404';
+SET title = $status||' - Not found';
+SET description = 'No such '||$tool||': '||$hash;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/alert.sql') AS properties
+WHERE COALESCE($content,'') = ''
+;
+SELECT 'redirect' AS component
+, $content AS link
+;