diff options
author | syn <isaqtm@gmail.com> | 2020-03-23 10:42:48 +0300 |
---|---|---|
committer | syn <isaqtm@gmail.com> | 2020-03-23 10:42:48 +0300 |
commit | 27bccdea5d5e376fdecb27b03f692408a2cc1f99 (patch) | |
tree | 99f927303fa7bb1928c0e5c5ae7b400c74790148 | |
parent | 3ff1340efb5956c9b417c6e03c6bab5684dda384 (diff) | |
download | blure-27bccdea5d5e376fdecb27b03f692408a2cc1f99.tar.gz |
* Fix hardcoded url
* Improve resizing
-rw-r--r-- | app/imutil.py | 39 | ||||
-rw-r--r-- | app/views.py | 4 | ||||
-rw-r--r-- | config.py | 19 |
3 files changed, 28 insertions, 34 deletions
diff --git a/app/imutil.py b/app/imutil.py index 05d5436..1b8c5ea 100644 --- a/app/imutil.py +++ b/app/imutil.py @@ -55,13 +55,13 @@ class NGXImage: ngx_image = NGXImage(from_id=new_id) - with ngx_image.orig_path.open('wb') as f: + with ngx_image.make_path('o').open('wb') as f: f.write(bytes_io.getvalue()) image = Image.open(bytes_io) - image.thumbnail(blure.config.CUT_SIZES[2]) + image.thumbnail(blure.config.CUT_SIZES['m']) image.save( - ngx_image.thumb_path, + ngx_image.make_path('m'), format=cls.pillow_format(content_type) ) @@ -78,30 +78,21 @@ class NGXImage: async def __aexit__(self, *exc): pass - @property - def orig_path(self): - return Path(_IMAGE_PATH.format(blure.url.to_url(self.id))) + def make_path(self, size: str='o') -> Path: + return _IMAGE_PATH / Path(blure.url.to_url(self.id) + size) - @property - def thumb_path(self): - return Path(_IMAGE_PATH.format(blure.url.to_url(self.id)) + '_thumb') + def make_url(self, size: str='o') -> Path: + return _IMAGE_URL / Path(blure.url.to_url(self.id) + size) - @classmethod - def _send_image(cls, filepath: Path) -> BaseHTTPResponse: - if not filepath.is_file(): - return cls.not_found() + def send_image(self, size: str='o') -> BaseHTTPResponse: + if not self.make_path(size).is_file(): + return self.not_found() return raw(b'', content_type='image', - headers={'X-Accel-Redirect': str(filepath)[4:]}, # FIXME: this should NOT be '[4:]' # noqa + headers={'X-Accel-Redirect': self.make_url(size)}, status=200) - def orig(self): - return self._send_image(self.orig_path) - - def thumb(self): - return self._send_image(self.thumb_path) - @staticmethod def not_found(): return raw(_NOT_FOUND_IMAGE, @@ -113,7 +104,7 @@ class NGXImage: await conn.execute('DELETE FROM pics WHERE id=$1', self.id) def delete_from_disk(self): - if self.orig_path.exists(): - self.orig_path.unlink() - if self.thumb_path.exists(): - self.thumb_path.unlink() + for cut_name in blure.config.CUT_SIZES.keys(): + path = self.make_path(cut_name) + if path.exists(): + path.unlink() diff --git a/app/views.py b/app/views.py index fcd3818..ef3bbd0 100644 --- a/app/views.py +++ b/app/views.py @@ -29,13 +29,13 @@ async def index(ctx): @db_route('/i/<url>') async def raw_image(ctx, url): async with NGXImage(blure.url.to_id(url)) as image: - return image.orig() + return image.send_image('o') @db_route('/t/<url>') async def thumb_image(ctx, url): async with NGXImage(blure.url.to_id(url)) as image: - return image.orig() + return image.send_image('m') @db_route('/p/<url>') @@ -1,5 +1,6 @@ from uuid import UUID from os import environ +from pathlib import Path host = '0.0.0.0' port = 80 @@ -12,14 +13,16 @@ PG_URI = f'postgres://{pg_user}:{pg_pass}@pg' APP_SECRET = UUID('8036587d-11ea-4c59-af9b-9da52eded1bc').bytes -NGX_IMAGE_PATH = '/var/ngx_img/{}' -NGX_IMAGE_URL = '/ngx_img/{}' -CUT_SIZES = [ - (256, 256), - (512, 512), - (1024, 1024), - (2048, 2048), -] +NGX_IMAGE_PATH = Path('/var/ngx_img') +NGX_IMAGE_URL = Path('/ngx_img') + +CUT_SIZES = { + 's': (256, 256), + 'm': (512, 512), + 'l': (1024, 1024), + 'xl': (2048, 2048) + # 'o': <original> +} DELETE_FILE_ON_DELETE = False NOT_FOUND_IMAGE_CONTENT_TYPE = 'image/jpeg' |