Przeglądaj źródła

write qr code when saving content

Daniel Sheffield 1 rok temu
rodzic
commit
470b53d1f1

+ 23 - 9
app/rest/pyapi.py

@@ -17,7 +17,6 @@ from psycopg import Cursor, connect
 from psycopg.rows import TupleRow
 from linkpreview import link_preview
 
-from .qr import get_qr_code
 from .validate import get_file_mimetype, get_file_size, get_filename, validate, validate_file, validate_parameter, validate_url
 from .hash_util import normalize_base32
 from .route_decorators import normalize, poison, cursor
@@ -165,8 +164,7 @@ def get_clip(filename):
     path = f'{filename}/{filename}.{ext}'
     
     if ext == 'qr':
-        mimetype = 'image/svg+xml'
-        return static_file(path, root='app/rest/static', mimetype=mimetype)
+        return static_file(path, root='app/rest/static', mimetype='image/svg+xml')
     
     filename = filename and normalize_base32(filename)
     if not request.params.raw.lower() == 'true':
@@ -198,12 +196,11 @@ def upload():
         response.content_type = 'text/html; charset=utf-8'
         disabled = True if _hash else False
         form = template('file-form', action='/upload', method='post', disabled=disabled)
-        svg = get_qr_code(link)
         
         return template(
             'upload',
             form=form,
-            svg=svg,
+            qr=f'{LOCATION}/upload/{_hash}.qr' if _hash else f'{LOCATION}/grocery/static/upload-qr.svg',
             link=link,
             mimetype=mimetype,
             disabled=disabled
@@ -218,13 +215,22 @@ def upload():
             if len(upload.file.read()) == 0:
                 return abort(400, "File is empty")
 
-        _b32 = save_upload(upload.raw_filename , upload.file, root='app/rest/static')
+        _b32 = save_upload(upload.raw_filename, LOCATION, upload.file, root='app/rest/static')
         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)
+    
     filename = filename and normalize_base32(filename)
+    path = f'{filename}/{filename}.{ext}'
+    
+    if ext == 'qr':
+        return static_file(path, root='app/rest/static', mimetype='image/svg+xml')
+    
     download = True
     mimetype = True
     if request.params.download == "false":
@@ -250,7 +256,6 @@ def goto():
             return redirect(target)
 
         link = f'{LOCATION}/goto/{_hash}' if content else f'{LOCATION}/goto'
-        svg = get_qr_code(content, fallback=link)
         disabled = True if content else False
         response.content_type = 'text/html; charset=utf-8'
         form = template(
@@ -274,7 +279,7 @@ def goto():
         return template(
             'goto',
             form=form,
-            svg=svg,
+            qr=f'{LOCATION}/goto/{_hash}.qr' if content else f'{LOCATION}/grocery/static/goto-qr.svg',
             link=link,
             disabled=disabled,
             preview=preview,
@@ -282,7 +287,7 @@ def goto():
 
     if request.method == 'POST':
         content = validate_parameter(request, 'url')
-        _b32 = save(content)
+        _b32 = save(content, LOCATION)
 
         # validate but save content unmodified
         _ = validate_url(content.decode('utf-8'))
@@ -291,7 +296,16 @@ def goto():
 
 @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='app/rest/static', mimetype='image/svg+xml')
+    
     return redirect(f'/goto?hash={filename}&go=true')
 
 @route('/<any>/', method='GET')

+ 4 - 1
app/rest/save.py

@@ -42,7 +42,7 @@ def save_filename(name: str, _b32: str, directory: str):
         f.write(name)
 
 
-def save_upload(name: str, content: BufferedRandom, root='app/rest/static') -> str:
+def save_upload(name: str, location: str, content: BufferedRandom, root='app/rest/static') -> str:
     tmpdir = '/tmp/upload'
     try:
         os.mkdir(tmpdir, mode=0o700, dir_fd=None)
@@ -76,5 +76,8 @@ def save_upload(name: str, content: BufferedRandom, root='app/rest/static') -> s
 
     os.replace(f'{tmpdir}/{unique.hex}', f'{directory}/{_b32}.file')
     save_filename(name, _b32, directory)
+    link = f'{location}/clip/{_b32}'
+    svg = get_qr_code(link, fallback=link)
+    save_qr(svg, _b32, directory)
 
     return _b32

Plik diff jest za duży
+ 1 - 0
app/rest/static/goto-qr.svg


Plik diff jest za duży
+ 1 - 0
app/rest/static/upload-qr.svg


+ 2 - 2
app/rest/templates/goto.tpl

@@ -13,7 +13,7 @@ body {
   background-color: #080808;
   color: #cccccc;
 }
-svg {
+img {
   background-color: floralwhite;
   color: black;
   max-height: min(100vh, calc(100vw * 9 / 16));
@@ -41,7 +41,7 @@ svg {
         </div>
       </div>
       <div class="pure-u-1">
-        <p><details><summary> Show QR code ...</summary>{{!svg}}</details></p>
+        <p><details><summary> Show QR code ...</summary><img src="{{qr}}"/></details></p>
 {{!form}}
       </div>
       <div class="pure-u-1-3"></div>

+ 2 - 2
app/rest/templates/upload.tpl

@@ -14,7 +14,7 @@ body {
   background-color: #080808;
   color: #cccccc;
 }
-svg {
+img {
   background-color: floralwhite;
   color: black;
   max-height: min(100vh, calc(100vw * 9 / 16));
@@ -56,7 +56,7 @@ object.text {
         </div>
       </div>
       <div class="pure-u-1">
-        <p><details><summary> Show QR code ...</summary>{{!svg}}</details></p>
+        <p><details><summary> Show QR code ...</summary><img src="{{qr}}"/></details></p>
 {{!form}}
 <form id="download" method="get" action="{{link}}"></form>
         % if mimetype and mimetype.startswith('text'):

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików