Selaa lähdekoodia

download from sqlite

Daniel Sheffield 4 kuukautta sitten
vanhempi
säilyke
7f3dd392bc
3 muutettua tiedostoa jossa 63 lisäystä ja 16 poistoa
  1. 1 0
      rest/hash_util.py
  2. 61 16
      rest/pyapi.py
  3. 1 0
      rest/requirements.txt

+ 1 - 0
rest/hash_util.py

@@ -9,6 +9,7 @@ DIGEST_SIZE_SIGNED_TO_UNSIGNED_BITMASK = 0x1ffffff
 DIGEST_SIZE_SIGNED_TO_UNSIGNED_BIT = 0x1000000
 DIGEST_SIZE_NIBBLES = DIGEST_SIZE_BYTES * 2
 B64ALTCHARS = b'.-'
+B32REGEX = r'[0-9a-tv-zA-TV-Z]'
 
 def sha1hash(data: str):
     return sha1(data.encode("utf-8"), usedforsecurity=False).hexdigest()[:DIGEST_SIZE_BYTES]

+ 61 - 16
rest/pyapi.py

@@ -4,19 +4,29 @@
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
 from io import BytesIO
+import time
 from bottle import (
+    Bottle,
+    default_app,
     route, request, response,
     redirect, abort,
     template, static_file,
+    HTTPResponse,
 )
 from linkpreview import link_preview
 
 from .validate import CLIP_SIZE_LIMIT, get_file_mimetype, get_file_size, get_filename, validate_file
-from .hash_util import normalize_base32, blake, bytes_to_base32
+from .hash_util import B32REGEX, normalize_base32, blake, bytes_to_base32
 from .save import save_upload
 from .qr import get_qr_code
 from json import dumps, load
 
+app = default_app()
+from bottle.ext import sqlite
+from sqlite3 import Row
+plugin = sqlite.Plugin(dbfile='/tmp/test.db')
+app.install(plugin)
+
 SCHEME = "https://"
 HOST = ""
 DOMAIN = "shandan.one"
@@ -82,6 +92,41 @@ def clip(route):
 def get_clip(route, filename):
     return redirect(f'/{route}.sql?hash={filename}&go=true')
 
+@app.route(f'/upload/<hash:re:{B32REGEX}{{1,5}}>', method='GET')
+def get_upload(hash, db=None):
+    hash = hash and normalize_base32(hash)
+    ret: Row = db.execute(f"""
+SELECT content
+FROM clip
+WHERE hash = '{hash}'
+LIMIT 1;
+""").fetchall()[0]
+    # if len(ret):
+    #     print(ret[0].keys())
+   
+    download = True
+    mimetype = True
+    if request.params.download == "false":
+        download = False
+    mimetype = request.params.mimetype or None
+    response.body = ret
+    headers = {}
+    headers['Content-Length'] = len(ret)
+    fname = 'noddy'
+    headers['Content-Disposition'] = 'attachment; filename="%s"' % fname
+    headers['Content-Encoding'] = 'application/octet-stream'
+    # if mimetype == 'auto':
+    #     mimetype, encoding = mimetypes.guess_type(filename)
+    #     if encoding: headers['Content-Encoding'] = encoding
+
+    # if mimetype:
+    #     if mimetype[:5] == 'text/' and charset and 'charset' not in mimetype:
+    #         mimetype += '; charset=%s' % charset
+    #     headers['Content-Type'] = mimetype
+    #lm = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(created))
+    #headers['Last-Modified'] = lm
+    return HTTPResponse(ret[0].encode('utf8'), **headers)
+
 @route('/upload', method=['GET', 'POST'])
 def upload():
 
@@ -124,25 +169,25 @@ def upload():
         return redirect(f'/upload?hash={_b32}')
 
 
-@route('/upload/<filename:path>', method='GET')
-def get_upload(filename):
-    ext = 'file'
-    if filename and filename.endswith('.qr'):
-        filename, ext = filename.split('.', 1)
+# @route('/upload/<filename:path>', method='GET')
+# def get_upload(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}'
+#     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')
+#     if ext == 'qr':
+#         return static_file(path, root='rest/static/files', mimetype='image/svg+xml')
     
-    download = True
-    mimetype = True
-    if request.params.download == "false":
-        download = False
-    mimetype = request.params.mimetype or None
+#     download = True
+#     mimetype = True
+#     if request.params.download == "false":
+#         download = False
+#     mimetype = request.params.mimetype or None
 
-    return validate_file(filename, root='rest/static/files', download=download, mimetype=mimetype)
+#     return validate_file(filename, root='rest/static/files', download=download, mimetype=mimetype)
 
 
 @route('/<any>/', method='GET')

+ 1 - 0
rest/requirements.txt

@@ -1,4 +1,5 @@
 bottle
+bottle-sqlite
 cherrypy
 base32-lib
 lxml