Ver código fonte

fix path to static files and make persist

Pi 1 ano atrás
pai
commit
b60b2a7a06
5 arquivos alterados com 33 adições e 23 exclusões
  1. 2 0
      docker-compose.yml
  2. 1 1
      grocery-transactions
  3. 19 14
      rest/pyapi.py
  4. 2 2
      rest/save.py
  5. 9 6
      rest/validate.py

+ 2 - 0
docker-compose.yml

@@ -66,6 +66,8 @@ services:
     build:
       context: .
       dockerfile: rest/Dockerfile
+    volumes:
+      - ./files:/usr/src/app/rest/static/files
     expose:
       - 6772
     networks:

+ 1 - 1
grocery-transactions

@@ -1 +1 @@
-Subproject commit 470b53d1f1a44f60729d2b2140d7c70dbc5b0155
+Subproject commit 755bb20ec4bf01f4b372c52e7ffdffa6090ef4d2

+ 19 - 14
rest/pyapi.py

@@ -23,6 +23,10 @@ DOMAIN = "shandan.one"
 PORT = ""
 LOCATION = SCHEME + (f"{HOST}." if HOST else "") + DOMAIN + (f":{PORT}" if PORT else "")
 
+@route('/static/<filename:path>')
+def send_static(filename):
+    return static_file(filename, root='rest/static')
+
 
 @route('/clip', method=['GET', 'POST'])
 def clip():
@@ -47,7 +51,7 @@ def clip():
         return template(
             'paste',
             form=form,
-            qr=f'{LOCATION}/clip/{_hash}.qr' if content else f'{LOCATION}/grocery/static/clip-qr.svg',
+            qr=f'{LOCATION}/clip/{_hash}.qr' if content else f'{LOCATION}/static/clip-qr.svg',
             link=link,
             disabled=True if content else False,
             download=f'/clip/{_hash}' if content else None
@@ -56,7 +60,7 @@ def clip():
     if request.method == 'POST':
         content = validate_parameter(request, 'paste')
         if request.params.copy != 'true':
-            _b32 = save(content, LOCATION)
+            _b32 = save(content, LOCATION, root='rest/static/files')
             return redirect(f'/clip?hash={_b32}')
 
         response.content_type = 'text/html; charset=utf-8'
@@ -71,7 +75,7 @@ def clip():
         return template(
             'paste',
             form=form,
-            qr=f'{LOCATION}/grocery/static/clip-qr.svg',
+            qr=f'{LOCATION}/static/clip-qr.svg',
             link=link,
             disabled=False,
             download=None
@@ -88,7 +92,7 @@ def get_clip(filename):
     path = f'{filename}/{filename}.{ext}'
     
     if ext == 'qr':
-        return static_file(path, root='rest/static', mimetype='image/svg+xml')
+        return static_file(path, root='rest/static/files', mimetype='image/svg+xml')
     
     filename = filename and normalize_base32(filename)
     if not request.params.raw.lower() == 'true':
@@ -99,7 +103,7 @@ def get_clip(filename):
     if isinstance(ret, HTTPError):
         return ret
 
-    return static_file(path, root='rest/static')
+    return static_file(path, root='rest/static/files')
 
 
 @route('/upload', method=['GET', 'POST'])
@@ -113,8 +117,9 @@ def upload():
             size = get_file_size(_hash)
             name = get_filename(_hash)
             mimetype = get_file_mimetype(name)
+            validate_file(_hash, root='rest/static/files', download=True, mimetype=mimetype)
             if mimetype is not True and mimetype.startswith('text'):
-                mimetype = None if size > CLIP_SIZE_LIMIT else mimetype
+                mimetype = None if size and size > CLIP_SIZE_LIMIT else mimetype
         
         link = f'{LOCATION}/upload/{_hash}' if _hash else f'{LOCATION}/upload'
         response.content_type = 'text/html; charset=utf-8'
@@ -124,7 +129,7 @@ def upload():
         return template(
             'upload',
             form=form,
-            qr=f'{LOCATION}/upload/{_hash}.qr' if _hash else f'{LOCATION}/grocery/static/upload-qr.svg',
+            qr=f'{LOCATION}/upload/{_hash}.qr' if _hash else f'{LOCATION}/static/upload-qr.svg',
             link=link,
             mimetype=mimetype,
             disabled=disabled
@@ -139,7 +144,7 @@ def upload():
             if len(upload.file.read()) == 0:
                 return abort(400, "File is empty")
 
-        _b32 = save_upload(upload.raw_filename, LOCATION, upload.file, root='rest/static')
+        _b32 = save_upload(upload.raw_filename, LOCATION, upload.file, root='rest/static/files')
         return redirect(f'/upload?hash={_b32}')
 
 
@@ -153,7 +158,7 @@ def get_upload(filename):
     path = f'{filename}/{filename}.{ext}'
     
     if ext == 'qr':
-        return static_file(path, root='rest/static', mimetype='image/svg+xml')
+        return static_file(path, root='rest/static/files', mimetype='image/svg+xml')
     
     download = True
     mimetype = True
@@ -161,7 +166,7 @@ def get_upload(filename):
         download = False
     mimetype = request.params.mimetype or None
 
-    return validate_file(filename, download=download, mimetype=mimetype)
+    return validate_file(filename, root='rest/static/files', download=download, mimetype=mimetype)
 
 
 @route('/goto', method=['GET', 'POST'])
@@ -171,7 +176,7 @@ def goto():
         _hash = request.params.hash
         if _hash:
             _hash = normalize_base32(_hash)
-            content = validate(_hash).decode('utf-8')
+            content = validate(_hash, root='rest/static/files').decode('utf-8')
         else:
             content = None
 
@@ -203,7 +208,7 @@ def goto():
         return template(
             'goto',
             form=form,
-            qr=f'{LOCATION}/goto/{_hash}.qr' if content else f'{LOCATION}/grocery/static/goto-qr.svg',
+            qr=f'{LOCATION}/goto/{_hash}.qr' if content else f'{LOCATION}/static/goto-qr.svg',
             link=link,
             disabled=disabled,
             preview=preview,
@@ -211,7 +216,7 @@ def goto():
 
     if request.method == 'POST':
         content = validate_parameter(request, 'url')
-        _b32 = save(content, LOCATION)
+        _b32 = save(content, LOCATION, root='rest/static/files')
 
         # validate but save content unmodified
         _ = validate_url(content.decode('utf-8'))
@@ -228,7 +233,7 @@ def redirect_goto(filename):
     path = f'{filename}/{filename}.{ext}'
     
     if ext == 'qr':
-        return static_file(path, root='rest/static', mimetype='image/svg+xml')
+        return static_file(path, root='rest/static/files', mimetype='image/svg+xml')
     
     return redirect(f'/goto?hash={filename}&go=true')
 

+ 2 - 2
rest/save.py

@@ -19,7 +19,7 @@ def save_qr(qr: bytes, _b32:str , directory: str) -> str:
         f.write(qr)
 
 
-def save(content: bytes, location: str, root='rest/static') -> str:
+def save(content: bytes, location: str, root='rest/static/files') -> str:
     _bytes = blake(content, person='clip'.encode('utf-8'))
     _b32 = bytes_to_base32(_bytes)
     directory = f'{root}/{_b32}'
@@ -43,7 +43,7 @@ def save_filename(name: str, _b32: str, directory: str):
         f.write(name)
 
 
-def save_upload(name: str, location: str, content: BufferedRandom, root='rest/static') -> str:
+def save_upload(name: str, location: str, content: BufferedRandom, root='rest/static/files') -> str:
     tmpdir = '/tmp/upload'
     try:
         os.mkdir(tmpdir, mode=0o700, dir_fd=None)

+ 9 - 6
rest/validate.py

@@ -43,8 +43,8 @@ URL_MUST_ESCAPE = bytes([
 URL_SAFE = bytes(( i for i in range(int('0xff',0)+1) if i not in map(int, URL_MUST_ESCAPE) ))
 
 CLIP_SIZE_LIMIT = 65535
-def validate(filename: str) -> bytes:
-    ret = static_file('/'.join([filename,]*2) + '.file', root='rest/static')
+def validate(filename: str, root='rest/static/files') -> bytes:
+    ret = static_file('/'.join([filename,]*2) + '.file', root=root)
     if isinstance(ret, HTTPError):
         return abort(404, f"No such paste: {filename}")
 
@@ -60,7 +60,7 @@ def validate(filename: str) -> bytes:
     return content
 
 
-def get_filename(filename: str, root: str = 'rest/static'):
+def get_filename(filename: str, root: str = 'rest/static/files'):
     path = '/'.join([filename,]*2)
     try:
         with open(f'{root}/{path}.name', "r") as f:
@@ -69,15 +69,18 @@ def get_filename(filename: str, root: str = 'rest/static'):
     except:
         pass
 
-def get_file_size(filename: str, root: str = 'rest/static'):
+def get_file_size(filename: str, root: str = 'rest/static/files'):
     path = '/'.join([filename,]*2)
-    return os.stat(f'{root}/{path}.file').st_size
+    try:
+        return os.stat(f'{root}/{path}.file').st_sizea
+    except:
+        pass
 
 def get_file_mimetype(name):
     mimetype = mimetypes.guess_type(name, strict=False)[0] if name else True
     return mimetype
 
-def validate_file(filename: str, root: str = 'rest/static', download=True, mimetype=True) -> HTTPResponse:
+def validate_file(filename: str, root: str = 'rest/static/files', download=True, mimetype=True) -> HTTPResponse:
     path = '/'.join([filename,]*2)
 
     name = get_filename(filename)