summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorsyn <isaqtm@gmail.com>2020-03-22 18:25:34 +0300
committersyn <isaqtm@gmail.com>2020-03-22 18:25:34 +0300
commitb7dcd0f4457833a5f1aa56d87e604edcc2a5a2e7 (patch)
treea705107675127518602dcb9a381e364b2002dd96 /app
parent45cd960311141768cc1c26685b62e70812a9431d (diff)
downloadblure-b7dcd0f4457833a5f1aa56d87e604edcc2a5a2e7.tar.gz
[MIGRATE] Save image based on content type
Diffstat (limited to 'app')
-rw-r--r--app/imutil.py22
-rw-r--r--app/schema.py2
-rw-r--r--app/static/js/eternalload.js1
-rw-r--r--app/views.py13
4 files changed, 31 insertions, 7 deletions
diff --git a/app/imutil.py b/app/imutil.py
index ec16dd1..015a2c1 100644
--- a/app/imutil.py
+++ b/app/imutil.py
@@ -10,6 +10,10 @@ _NOT_FOUND_IMAGE = blure.config.NOT_FOUND_IMAGE
_NOT_FOUND_IMAGE_CONTENT_TYPE = blure.config.NOT_FOUND_IMAGE_CONTENT_TYPE
+class InvalidImageFormat(ValueError):
+ pass
+
+
class NGXImage:
def __init__(self, id: int):
self.filename = blure.url.to_url(id)
@@ -37,7 +41,21 @@ class NGXImage:
content_type=_NOT_FOUND_IMAGE_CONTENT_TYPE,
status=404)
- def save(self, body: BytesIO):
+ @staticmethod
+ def pillow_format(content_type: str):
+ mapping = {
+ '': 'PNG', # try png if content type is not available
+ 'image/bmp': 'BMP',
+ 'image/gif': 'GIF',
+ 'image/jpeg': 'JPEG',
+ 'image/png': 'PNG'
+ }
+ if content_type in mapping.keys():
+ return mapping[content_type]
+ else:
+ raise InvalidImageFormat(f'{content_type} is not supported')
+
+ def save(self, body: BytesIO, content_type: str):
image_path = Path(_IMAGE_PATH.format(self.filename))
thumb_path = Path(_IMAGE_PATH.format(self.filename + '_thumb'))
@@ -48,7 +66,7 @@ class NGXImage:
im = Image.open(body)
thumb_stream = BytesIO()
im.thumbnail(blure.config.CUT_SIZES[2])
- im.save(thumb_stream, format='JPEG')
+ im.save(thumb_stream, format=self.pillow_format(content_type))
f.write(thumb_stream.getvalue())
def delete_from_disk(self):
diff --git a/app/schema.py b/app/schema.py
index 247638f..38b934f 100644
--- a/app/schema.py
+++ b/app/schema.py
@@ -3,7 +3,7 @@ schema_up = [
CREATE TABLE IF NOT EXISTS pics (
id SERIAL PRIMARY KEY UNIQUE,
src_url VARCHAR(1024),
- ext VARCHAR(10),
+ content_type VARCHAR(256),
deleted BOOLEAN
);
''',
diff --git a/app/static/js/eternalload.js b/app/static/js/eternalload.js
index fe23c98..ee4d570 100644
--- a/app/static/js/eternalload.js
+++ b/app/static/js/eternalload.js
@@ -74,6 +74,7 @@ class EternalLoad {
Array.prototype.forEach.call(input_event.target.files, file => {
let data = new FormData();
data.append('im', file);
+ data.append('content-type', file.type)
let fileno = this.local.total;
this.local.total += 1;
diff --git a/app/views.py b/app/views.py
index 8cb0755..f81cfd4 100644
--- a/app/views.py
+++ b/app/views.py
@@ -6,7 +6,7 @@ from app import log, blure
from requests import get as fetch_url
from .imutil import NGXImage
from .util import URLDecodeError
-from sanic.exceptions import NotFound
+from sanic.exceptions import NotFound, InvalidUsage as BadRequest
async def is_image_exists(pg, id: int):
@@ -60,7 +60,6 @@ async def pic_profile(ctx, url):
log.warn(url)
try:
id = ctx.app.url.to_id(url)
- log.warn(id)
if await is_image_exists(ctx.pg, id):
return render_template('profile.html.j2',
ctx.r,
@@ -77,9 +76,15 @@ async def pic_push(ctx):
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, ext)
+ INSERT INTO pics(src_url, content_type)
VALUES ($1, $2)
RETURNING id
''',
@@ -88,7 +93,7 @@ async def pic_push(ctx):
)
im = NGXImage(id)
- im.save(image_stream)
+ im.save(image_stream, content_type)
return text('new url is ' + ctx.app.url.to_url(id))
except KeyError: