second commit
This commit is contained in:
parent
6df398966e
commit
4e52414845
58
saddle.py
58
saddle.py
|
@ -2,6 +2,10 @@
|
|||
"""
|
||||
A file hosting service similar to Pomf and Uguu but without the public nature.
|
||||
"""
|
||||
import os
|
||||
import string
|
||||
import random
|
||||
|
||||
from aiohttp import web
|
||||
import jinja2
|
||||
import aiohttp_jinja2
|
||||
|
@ -17,9 +21,61 @@ routes = web.RouteTableDef()
|
|||
|
||||
|
||||
@routes.get('/', name='index')
|
||||
@routes.post('/', name='index')
|
||||
async def index(request):
|
||||
"""The index page."""
|
||||
return render_template("index.html", request, locals())
|
||||
if request.method == 'GET':
|
||||
return render_template("index.html", request, locals())
|
||||
data = await request.post()
|
||||
files = []
|
||||
for filefield in data.getall('files'):
|
||||
files.append(handle_filefield(filefield))
|
||||
|
||||
files_insert = []
|
||||
for file in files:
|
||||
t = (request.cookies.get('userid'), file[0], file[1], 'DEFAUlT')
|
||||
async with request.app['pool'] as conn:
|
||||
await conn.executemany(
|
||||
"INSERT INTO upload (user_id, id, filename, expiration_date) "
|
||||
"VALUES ($1, $2, $3, $4)",
|
||||
files_insert)
|
||||
urls = [config.upload_url + f[1] for f in files]
|
||||
return render_template("result.html", request, locals())
|
||||
|
||||
|
||||
def handle_filefield(filefield, rand_name=True):
|
||||
"""Handles a posted file."""
|
||||
filename = safe_filename(filefield.filename)
|
||||
if not filename:
|
||||
rand_name = True
|
||||
prefix = get_rand_chars()
|
||||
if rand_name:
|
||||
filename = prefix + os.path.splitext(filename)[1]
|
||||
else:
|
||||
filename = prefix + '_' + filename
|
||||
|
||||
with open(os.path.join(config.upload_dir, filename), 'wb') as file:
|
||||
file.write(filefield.file.read())
|
||||
|
||||
t = (prefix, filename)
|
||||
return t
|
||||
|
||||
|
||||
def safe_filename(filename=''):
|
||||
"""Sanitizes the given filename."""
|
||||
safe_char = string.ascii_letters + string.digits + '._ '
|
||||
filename = ''.join([c for c in filename if c in safe_char])
|
||||
filename = filename.strip('._ ')
|
||||
return filename
|
||||
|
||||
|
||||
def get_rand_chars(n=8):
|
||||
"""Returns `n` number of random ASCII characters."""
|
||||
chars = []
|
||||
for _ in range(n):
|
||||
char = random.choice(string.ascii_letters + string.digits)
|
||||
chars.append(char)
|
||||
return "".join(chars)
|
||||
|
||||
|
||||
async def init_app():
|
||||
|
|
|
@ -11,6 +11,12 @@
|
|||
<h1>Saddle</h1>
|
||||
</header>
|
||||
<main>
|
||||
<section>
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="files" multiple>
|
||||
<input type="submit">
|
||||
</form>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
|
23
templates/result.html
Normal file
23
templates/result.html
Normal file
|
@ -0,0 +1,23 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Saddle - Results</title>
|
||||
<link rel="stylesheet" type="text/css" href="/static/saddle.css">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=0.8">
|
||||
<meta name="description" content="A file upload service.">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Saddle</h1>
|
||||
</header>
|
||||
<main>
|
||||
<section>
|
||||
<ul>
|
||||
{% for url in urls %}
|
||||
<li><a href="{{ url }}">{{ url }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user