Skip to content

Commit

Permalink
feat: upload image to imgur to reduce slug size
Browse files Browse the repository at this point in the history
heroku limit: the maximum slug size is 500 MB
  • Loading branch information
Flora2020 committed Oct 17, 2021
1 parent 37f9bb2 commit d7ef91f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ MerchantID=
HashKey=
HashIV=
PORT=
IMGUR_ID=
Empty file added helpers/__init__.py
Empty file.
1 change: 1 addition & 0 deletions helpers/products_view_helper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from helpers.products_view_helper.upload_to_imgur import upload_to_imgur
13 changes: 13 additions & 0 deletions helpers/products_view_helper/upload_to_imgur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from typing import Union
from flask_imgur.flask_imgur import Imgur

imgur_handler = Imgur(client_id=os.environ.get('IMGUR_ID'))


def upload_to_imgur(image: bytes) -> Union[str, None]:
if not image:
return None

image_data = imgur_handler.send_image(image)
return image_data['data']['link'] if image_data['success'] else None
17 changes: 4 additions & 13 deletions views/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from common.flash_message import product_not_found, seller_products_not_found, product_update_success
from common.generate_many_flash_message import generate_many_flash_message
from common.forms import NewProduct
from helpers.products_view_helper import upload_to_imgur

product_blueprint = Blueprint('products', __name__)
products_per_page = 20
Expand Down Expand Up @@ -171,16 +172,11 @@ def new_product():
form.category.choices = [(category.id, category.name) for category in categories]
if form.validate_on_submit():
seller_id = session['user']['id']
image = form.image.data
if image:
image_name = f'{uuid4().hex}.{secure_filename(image.filename).rsplit(".", 1)[1]}'
image.save(f'{upload_folder}{image_name}')
else:
image_name = no_image_filename
image_url = upload_to_imgur(form.image.data) or f'/{upload_folder}{no_image_filename}'

product = Product(name=form.name.data,
price=form.price.data,
image_url=f'/{upload_folder}{image_name}',
image_url=image_url,
inventory=form.inventory.data,
description=form.description.data,
seller_id=seller_id,
Expand Down Expand Up @@ -227,14 +223,9 @@ def edit_product(product_id):

if request.method == 'POST':
if form.validate_on_submit():
image = form.image.data
if image:
image_name = f'{uuid4().hex}.{secure_filename(image.filename).rsplit(".", 1)[1]}'
image.save(f'{upload_folder}{image_name}')
product.image_url = f'/{upload_folder}{image_name}'

product.name = form.name.data
product.price = form.price.data
product.image_url = upload_to_imgur(form.image.data) or product.image_url
product.inventory = form.inventory.data
product.description = form.description.data
product.category = Category.find_by_id(form.category.data)
Expand Down

0 comments on commit d7ef91f

Please sign in to comment.