Compare commits
5 commits
ada/better
...
main
Author | SHA1 | Date | |
---|---|---|---|
103479668d | |||
e61eaa291b | |||
6cee95c077 | |||
3448294314 | |||
ed90e12227 |
7 changed files with 39 additions and 10 deletions
11
README.md
Normal file
11
README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# A code share paste
|
||||||
|
|
||||||
|
## Run
|
||||||
|
Require redis and python, install python requirements with `pip install -r requirements.txt`. Run with `flask --app paste run` (add `--debug` for devlopement)
|
||||||
|
You can configure with multiple environnement variables.
|
||||||
|
- PASTE_REDIS_HOST: redis host (default localhost)
|
||||||
|
- PASTE_REDIS_PORT: redis port (default 6379)
|
||||||
|
- PASTE_REDIS_USER: redis username (default none)
|
||||||
|
- PASTE_REDIS_PASSWORD: redis password (default none)
|
||||||
|
- PASTE_URL_LENGTH: length for url used by paste
|
||||||
|
- PASTE_SECRET_KEY: secret key for flask, don't use the default value (generate a safe value with `openssl rand -hex 16`).
|
|
@ -2,7 +2,7 @@
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
||||||
REDIS_HOST = getenv("PASTE_REDIS_HOST") or "localhost"
|
REDIS_HOST = getenv("PASTE_REDIS_HOST") or "localhost"
|
||||||
REDIS_PORT = getenv("PASTE_REDIS_HOST") or "6379"
|
REDIS_PORT = getenv("PASTE_REDIS_PORT") or "6379"
|
||||||
REDIS_USER = getenv("PASTE_REDIS_USER") or None
|
REDIS_USER = getenv("PASTE_REDIS_USER") or None
|
||||||
REDIS_PASSWORD = getenv("PASTE_REDIS_PASSWORD") or None
|
REDIS_PASSWORD = getenv("PASTE_REDIS_PASSWORD") or None
|
||||||
URL_LENGTH = getenv("PASTE_URL_LENGTH") or 4
|
URL_LENGTH = getenv("PASTE_URL_LENGTH") or 4
|
||||||
|
|
|
@ -31,7 +31,7 @@ def insert_content_db(url_id: str, expiration: int, content: str, secret: str) -
|
||||||
|
|
||||||
:param secret: Secret key for delete url
|
:param secret: Secret key for delete url
|
||||||
:param url_id: key for access to content.
|
:param url_id: key for access to content.
|
||||||
:param expiration: Content expiration in second.
|
:param expiration: Content expiration in second. Set 0 if no expiration.
|
||||||
:param content: Paste content
|
:param content: Paste content
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
@ -40,6 +40,7 @@ def insert_content_db(url_id: str, expiration: int, content: str, secret: str) -
|
||||||
data = {"content": content, "secret": secret}
|
data = {"content": content, "secret": secret}
|
||||||
for key, key_content in data.items():
|
for key, key_content in data.items():
|
||||||
db.hset(url_id, key, key_content)
|
db.hset(url_id, key, key_content)
|
||||||
db.expire(url_id, expiration)
|
if expiration != 0:
|
||||||
|
db.expire(url_id, expiration)
|
||||||
else:
|
else:
|
||||||
logging.error(f"Key : {url_id} already exist") # noqa: G004
|
logging.error(f"Key : {url_id} already exist") # noqa: G004
|
||||||
|
|
|
@ -12,7 +12,7 @@ from flask import (
|
||||||
from werkzeug import Response
|
from werkzeug import Response
|
||||||
|
|
||||||
from paste.db import check_content_exist, connect_redis, insert_content_db
|
from paste.db import check_content_exist, connect_redis, insert_content_db
|
||||||
from paste.utils import generate_id, generate_secret
|
from paste.utils import generate_id, generate_secret, get_expiration_time
|
||||||
|
|
||||||
home = Blueprint("home", __name__, url_prefix="/")
|
home = Blueprint("home", __name__, url_prefix="/")
|
||||||
|
|
||||||
|
@ -49,11 +49,12 @@ def create() -> Response:
|
||||||
"""
|
"""
|
||||||
content = request.form.get("content")
|
content = request.form.get("content")
|
||||||
time = request.form.get("time")
|
time = request.form.get("time")
|
||||||
time = 86400 if time == "" else int(time)
|
time = 0 if time == "" else int(time)
|
||||||
|
|
||||||
flash_data = create_paste(content, time)
|
flash_data = create_paste(content, time)
|
||||||
flash(flash_data["url_id"], category="create")
|
flash(flash_data["url_id"], category="create")
|
||||||
flash(flash_data["secret"], category="create")
|
flash(flash_data["secret"], category="create")
|
||||||
|
|
||||||
return redirect(url_for("home.homepage"))
|
return redirect(url_for("home.homepage"))
|
||||||
|
|
||||||
|
|
||||||
|
@ -74,7 +75,7 @@ def delete_paste(path: str) -> Response:
|
||||||
|
|
||||||
|
|
||||||
@home.route("/<path:path>")
|
@home.route("/<path:path>")
|
||||||
def get_content(path: str) -> str:
|
def get_content(path: str) -> Response | str:
|
||||||
"""
|
"""
|
||||||
Return paste for asked url.
|
Return paste for asked url.
|
||||||
:param path: Requested path.
|
:param path: Requested path.
|
||||||
|
@ -84,6 +85,13 @@ def get_content(path: str) -> str:
|
||||||
data = db.hgetall(path)
|
data = db.hgetall(path)
|
||||||
if check_content_exist(path):
|
if check_content_exist(path):
|
||||||
flash(data["content"])
|
flash(data["content"])
|
||||||
return render_template("content.html.j2")
|
cache_time = get_expiration_time(path)
|
||||||
|
current_response = Response(render_template("content.html.j2"))
|
||||||
|
if cache_time == -1:
|
||||||
|
current_response.headers["Cache-Control"] = "public"
|
||||||
|
|
||||||
|
else:
|
||||||
|
current_response.headers["Cache-Control"] = f"public, {cache_time}"
|
||||||
|
return current_response
|
||||||
|
|
||||||
return abort(404)
|
return abort(404)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import secrets
|
||||||
import string
|
import string
|
||||||
|
|
||||||
from paste import config
|
from paste import config
|
||||||
|
from paste.db import connect_redis
|
||||||
|
|
||||||
|
|
||||||
def generate_id() -> str:
|
def generate_id() -> str:
|
||||||
|
@ -24,3 +25,13 @@ def generate_secret() -> str:
|
||||||
:return: The secret.
|
:return: The secret.
|
||||||
"""
|
"""
|
||||||
return secrets.token_hex()
|
return secrets.token_hex()
|
||||||
|
|
||||||
|
|
||||||
|
def get_expiration_time(key: str) -> int:
|
||||||
|
"""
|
||||||
|
Get key expiration time
|
||||||
|
:param key: Key to check
|
||||||
|
:return: Expiration time in second.
|
||||||
|
"""
|
||||||
|
db = connect_redis()
|
||||||
|
return db.ttl(key)
|
||||||
|
|
|
@ -59,7 +59,6 @@ exclude = [
|
||||||
"instances",
|
"instances",
|
||||||
"schema.sql"
|
"schema.sql"
|
||||||
]
|
]
|
||||||
format = "grouped"
|
|
||||||
|
|
||||||
[tool.isort]
|
[tool.isort]
|
||||||
profile = "black"
|
profile = "black"
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
flask==2.3.3
|
-r requirements.txt
|
||||||
redis==5.0.0
|
|
||||||
ruff==0.0.291
|
ruff==0.0.291
|
||||||
|
|
Reference in a new issue