-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
104 lines (80 loc) · 3.02 KB
/
app.py
File metadata and controls
104 lines (80 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
from pathlib import Path
from hashlib import md5
from functools import partial
import peewee
from sanic_session import InMemorySessionInterface
from sanic_jinja2 import SanicJinja2
from sanic import Sanic
from sanic.response import json, html, redirect
from jinja2 import Environment, FileSystemLoader, select_autoescape
from memegram.db.models import Image
UPLOAD_DIRECTORY = "./static/pictures"
VIEWS_PATH = Path(__file__).parent.joinpath("views")
app = Sanic('image-gallery')
jinja = SanicJinja2(app, loader=FileSystemLoader(str(VIEWS_PATH)))
session = InMemorySessionInterface(cookie_name=app.name, prefix=app.name)
jinja.add_env(
"image_static_url", partial(app.url_for, "static", name="images")
)
# Serve CSS files
app.static("/css", "./static/css", name="css")
# Serve JS files
app.static("/js", "./static/js", name="js")
# Serve Image files
app.static("/images", "./static/pictures", name="images")
def save_image_details(image_file, image_filepath):
image_digest = md5(image_file.body).hexdigest()
try:
Image.create(
filename=image_file.name,
filepath=image_filepath,
digest=image_digest,
)
return True
except peewee.IntegrityError as e:
print("Duplicate file detected")
return False
@app.middleware("request")
async def add_session_to_request(request):
# before each request initialize a session
# using the client's request
await session.open(request)
@app.middleware("response")
async def save_session(request, response):
# after each request save the session,
# pass the response to set client cookies
await session.save(request, response)
def render_template(filename, request, **template_variables):
return jinja.render(
filename,
errors=request.get("flash", {}).get("errors", []),
info=request.get("flash", {}).get("info", []),
**template_variables,
)
@app.route("/")
async def test(request):
return json({"hello": "world"})
@app.route("/upload", methods=["POST"])
async def upload(request):
picture_file = request.files["picture"][0]
if len(picture_file.name) > 0:
filepath = os.path.join(UPLOAD_DIRECTORY, picture_file.name)
if save_image_details(picture_file, filepath):
with open(filepath, "wb") as f:
f.write(picture_file.body)
# request["flash"]("successfully saved image", "success")
jinja.flash(request, "successfully saved image", "success")
else:
# request["flash"]("Image file already exists", "danger")
jinja.flash(request, "Image file already exists", "danger")
else:
# request["flash"]("Please upload a file", "danger")
jinja.flash(request, "Please upload a file", "danger")
return redirect(app.url_for("index"))
@app.route("/index")
async def index(request):
image_files = Image.select()
return jinja.render("layout.html.jinja2", request, image_files=image_files)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)