Ver Fonte

add buttons for direct download and creating new paste

Daniel Sheffield há 1 ano atrás
pai
commit
0fa3d41c80
3 ficheiros alterados com 66 adições e 20 exclusões
  1. 47 14
      app/rest/pyapi.py
  2. 4 1
      app/rest/templates/clip-form.tpl
  3. 15 5
      app/rest/templates/paste.tpl

+ 47 - 14
app/rest/pyapi.py

@@ -101,15 +101,38 @@ def tags(cur):
     return get_tags(cur, request.query)
 
 CLIP_SIZE_LIMIT = 65535
+SCHEME = "http://" #"https://"
+HOST = ""
+DOMAIN = "0.0.0.0" #"shandan.one"
+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 HTTPResponse(template('paste', form=form, link=f'{LOCATION}/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':
+        # TODO: return a form with timeout to a GET instead ?
+        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')

+ 4 - 1
app/rest/templates/clip-form.tpl

@@ -1,5 +1,6 @@
 % from app.data.filter import get_query_param
 % content = setdefault("content", "") or ""
+% disabled = (setdefault("disabled", False) or "") and 'disabled="true"'
 <form id="paste" method="{{ method }}" action="{{ action }}">
   <style>
   select::-webkit-scrollbar {
@@ -20,5 +21,7 @@ textarea::-webkit-scrollbar-thumb {
   border: 3px solid var(--scrollbarBG);
 }
   </style>
-  <textarea style="width: 80%" id="paste-text-area" name="paste" rows="30">{{ content }}</textarea>
+  <textarea style="width: 80%" id="paste-text-area" name="paste" rows="30" {{!disabled}}>{{ content }}</textarea>
 </form>
+<form id="new" method="get" action="{{ action }}">
+</form>

+ 15 - 5
app/rest/templates/paste.tpl

@@ -1,4 +1,7 @@
 % link = setdefault("link", "") or ""
+% disabled = setdefault("disabled", "") and 'disabled="true"'
+% download = setdefault("download", "") or ""
+% download_disabled = "" if download else 'disabled="true"'
 <!DOCTYPE html>
 <html>
   <head>
@@ -22,16 +25,23 @@ body {
     <div class="pure-g">
       % include('button-style')
       <div class="pure-u-1">
-        <button class="button-resize pure-button" type="submit" form="paste"> Paste </button>
+        <div class="pure-button-group" role="action" style="padding: 1em 0 0;">
+          <button class="button-resize pure-button" type="submit" form="new"> New </button>
+          <button class="button-resize pure-button" type="submit" form="paste" {{!disabled}}> Paste </button>
+          <button class="button-resize pure-button" type="submit" form="download" {{!download_disabled}}> Download </button>
+        </div>
       </div>
-      <div style="width: 100%; height: 1em"></div>
       <div class="pure-u-1">
-        <a href="{{ link }}">{{ link }}</a>
+        <p>
+          <a href="{{!link}}">{{ link }}</a>
+        </p>
       </div>
-      <div style="width: 100%; height: 1em"></div>
       <div class="pure-u-1">
 {{!form}}
+<form id="download" method="get" action="{{ download }}">
+<input name="raw" type="text" hidden="true" value="true" />
+</form>
       </div>
     </div>
   </body>
-</html>
+</html>