summaryrefslogtreecommitdiffstats
path: root/app/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/views.py')
-rw-r--r--app/views.py78
1 files changed, 22 insertions, 56 deletions
diff --git a/app/views.py b/app/views.py
index f81cfd4..fcd3818 100644
--- a/app/views.py
+++ b/app/views.py
@@ -2,16 +2,11 @@ from io import BytesIO
from .request_routine import db_route
from sanic.response import text, redirect, json
from jinja2_sanic import render_template
-from app import log, blure
+from app import blure
from requests import get as fetch_url
from .imutil import NGXImage
from .util import URLDecodeError
-from sanic.exceptions import NotFound, InvalidUsage as BadRequest
-
-
-async def is_image_exists(pg, id: int):
- rec = await pg.fetchval('SELECT 1 FROM pics WHERE id=$1', id)
- return rec is not None
+from sanic.exceptions import NotFound, InvalidUsage as BadRequest, ServerError
@blure.exception(NotFound)
@@ -19,6 +14,11 @@ async def not_found(req, exc):
return render_template('404.html.j2', req, dict())
+@blure.exception(URLDecodeError)
+async def handle_urldecode(req, exc):
+ raise ServerError(str(exc), status_code=400)
+
+
@db_route('/')
async def index(ctx):
records = await ctx.pg.fetch('SELECT id FROM pics')
@@ -28,46 +28,24 @@ async def index(ctx):
@db_route('/i/<url>')
async def raw_image(ctx, url):
- try:
- id = ctx.app.url.to_id(url)
- except URLDecodeError as err:
- log.warn(f'URL not decoded: {err}')
- return NGXImage.not_found()
-
- if not await is_image_exists(ctx.pg, id):
- log.error('Image not in db')
- return NGXImage.not_found()
-
- return NGXImage(id).orig()
+ async with NGXImage(blure.url.to_id(url)) as image:
+ return image.orig()
@db_route('/t/<url>')
async def thumb_image(ctx, url):
- try:
- id = ctx.app.url.to_id(url)
- except URLDecodeError:
- return NGXImage.not_found()
-
- if not await is_image_exists(ctx.pg, id):
- log.error('Image not in db')
- return NGXImage.not_found()
-
- return NGXImage(id).thumb()
+ async with NGXImage(blure.url.to_id(url)) as image:
+ return image.orig()
@db_route('/p/<url>')
async def pic_profile(ctx, url):
- log.warn(url)
- try:
- id = ctx.app.url.to_id(url)
- if await is_image_exists(ctx.pg, id):
- return render_template('profile.html.j2',
- ctx.r,
- dict(url=url, tags=[]))
- else:
- raise NotFound('Url not found')
- except URLDecodeError:
- raise NotFound('Invalid url')
+ # Check image exists
+ # This will be used to get meta
+ async with NGXImage(blure.url.to_id(url)):
+ return render_template('profile.html.j2',
+ ctx.r,
+ dict(url=url, tags=[]))
@db_route('/c/push', methods=['POST'])
@@ -75,27 +53,15 @@ async def pic_push(ctx):
try:
file = ctx.r.files['im'][0]
image_stream = BytesIO(file.body)
- ext = file.name.split('.')[-1]
if len(ctx.r.form['content-type']) != 1:
raise BadRequest('Need only one content-type')
content_type = ctx.r.form['content-type'][0]
- id = await ctx.pg.fetchval(
- '''
- INSERT INTO pics(src_url, content_type)
- VALUES ($1, $2)
- RETURNING id
- ''',
- '',
- ext
- )
-
- im = NGXImage(id)
- im.save(image_stream, content_type)
+ image = await NGXImage.create_from_bytes(image_stream, content_type)
- return text('new url is ' + ctx.app.url.to_url(id))
+ return text(ctx.app.url.to_url(image.id))
except KeyError:
return text('you did not post anything')
@@ -112,7 +78,7 @@ async def push_url(ctx):
@db_route('/c/delete/<url>', methods=['POST'])
async def delete_pic(ctx, url):
id = ctx.app.url.to_id(url)
- await ctx.pg.execute('DELETE FROM pics WHERE id=$1', id)
- im = NGXImage(id)
- im.delete_from_disk()
+ async with NGXImage(id) as image:
+ await image.delete_from_db()
+ image.delete_from_disk()
return redirect(ctx.app.url_for('index'))