Repository: fossasia/open-event-server

Issue:
Use type "event-image" "speaker-image" for creating image urls of event /user/speaker
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->

Use type "event-image" for creating image urls of event 

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->

**Stacktrace**
<!-- If applicable, add stacktrace to help explain your problem. -->

**Additional details (please complete the following information):**
 - OS: [e.g. MacOS, Ubuntu, CentOS]
 - Python Version [e.g. `3.5`, `3.6`]
 - `HEAD` Commit hash [e.g. `4629c62`]

**Additional context**
<!-- Add any other context about the problem here. -->

https://github.com/fossasia/open-event-server/blob/development/app/api/events.py#LC152

https://github.com/fossasia/open-event-server/blob/development/app/api/helpers/files.py#LC130

https://github.com/fossasia/open-event-server/blob/development/populate_db.py#LC108


Discussion:

Resolving patch:
diff --git a/app/api/events.py b/app/api/events.py
--- a/app/api/events.py
+++ b/app/api/events.py
@@ -149,7 +149,7 @@ def after_create_object(self, event, data, view_kwargs):
 
         if data.get('original_image_url'):
             try:
-                uploaded_images = create_save_image_sizes(data['original_image_url'], 'event', event.id)
+                uploaded_images = create_save_image_sizes(data['original_image_url'], 'event-image', event.id)
             except (urllib.error.HTTPError, urllib.error.URLError):
                 raise UnprocessableEntity(
                     {'source': 'attributes/original-image-url'}, 'Invalid Image URL'
@@ -454,7 +454,7 @@ def before_update_object(self, event, data, view_kwargs):
         """
         if data.get('original_image_url') and data['original_image_url'] != event.original_image_url:
             try:
-                uploaded_images = create_save_image_sizes(data['original_image_url'], 'event', event.id)
+                uploaded_images = create_save_image_sizes(data['original_image_url'], 'event-image', event.id)
             except (urllib.error.HTTPError, urllib.error.URLError):
                 raise UnprocessableEntity(
                     {'source': 'attributes/original-image-url'}, 'Invalid Image URL'
diff --git a/app/api/helpers/files.py b/app/api/helpers/files.py
--- a/app/api/helpers/files.py
+++ b/app/api/helpers/files.py
@@ -139,27 +139,49 @@ def create_save_image_sizes(image_file, image_sizes_type, unique_identifier=None
     large_basewidth = image_sizes.full_width if image_sizes.full_width else 1300
     large_height_size = image_sizes.full_height if image_sizes.full_width else 500
 
-    thumbnail_aspect = image_sizes.thumbnail_aspect
-    thumbnail_basewidth = image_sizes.thumbnail_width
-    thumbnail_height_size = image_sizes.thumbnail_height
-
-    icon_aspect = image_sizes.icon_aspect
-    icon_basewidth = image_sizes.icon_width
-    icon_height_size = image_sizes.icon_height
-
-    original_upload_path = UPLOAD_PATHS[image_sizes_type]['original'].format(
-        identifier=unique_identifier
-    )
-
-    large_upload_path = UPLOAD_PATHS[image_sizes_type]['large'].format(
-        identifier=unique_identifier
-    )
-    thumbnail_upload_path = UPLOAD_PATHS[image_sizes_type]['thumbnail'].format(
-        identifier=unique_identifier
-    )
-    icon_upload_path = UPLOAD_PATHS[image_sizes_type]['icon'].format(
-        identifier=unique_identifier
-    )
+    if image_sizes_type == 'speaker-image':
+        thumbnail_aspect = image_sizes.thumbnail_aspect if image_sizes.thumbnail_aspect else True
+        thumbnail_basewidth = thumbnail_height_size = image_sizes.thumbnail_size_width_height
+    else:
+        thumbnail_aspect = image_sizes.thumbnail_aspect
+        thumbnail_basewidth = image_sizes.thumbnail_width
+        thumbnail_height_size = image_sizes.thumbnail_height
+
+    if image_sizes_type == 'speaker-image':
+        icon_aspect = image_sizes.icon_aspect if image_sizes.icon_aspect else True
+        icon_basewidth = icon_height_size = image_sizes.icon_size_width_height
+    else:
+        icon_aspect = image_sizes.icon_aspect
+        icon_basewidth = image_sizes.icon_width
+        icon_height_size = image_sizes.icon_height
+
+    if image_sizes_type == 'event-image':
+        original_upload_path = UPLOAD_PATHS['event']['original'].format(
+            identifier=unique_identifier)
+        large_upload_path = UPLOAD_PATHS['event']['large'].format(
+            identifier=unique_identifier)
+        thumbnail_upload_path = UPLOAD_PATHS['event']['thumbnail'].format(
+            identifier=unique_identifier)
+        icon_upload_path = UPLOAD_PATHS['event']['icon'].format(
+            identifier=unique_identifier)
+    elif image_sizes_type == 'speaker-image':
+        original_upload_path = UPLOAD_PATHS['user']['original'].format(
+            identifier=unique_identifier)
+        large_upload_path = UPLOAD_PATHS['user']['large'].format(
+            identifier=unique_identifier)
+        thumbnail_upload_path = UPLOAD_PATHS['user']['thumbnail'].format(
+            identifier=unique_identifier)
+        icon_upload_path = UPLOAD_PATHS['user']['icon'].format(
+            identifier=unique_identifier)
+    else:
+        original_upload_path = UPLOAD_PATHS[image_sizes_type]['original'].format(
+            identifier=unique_identifier)
+        large_upload_path = UPLOAD_PATHS[image_sizes_type]['large'].format(
+            identifier=unique_identifier)
+        thumbnail_upload_path = UPLOAD_PATHS[image_sizes_type]['thumbnail'].format(
+            identifier=unique_identifier)
+        icon_upload_path = UPLOAD_PATHS[image_sizes_type]['icon'].format(
+            identifier=unique_identifier)
 
     new_images = {
         'original_image_url': create_save_resized_image(image_file, 0, 0, 0, original_upload_path, resize=False),
diff --git a/app/api/users.py b/app/api/users.py
--- a/app/api/users.py
+++ b/app/api/users.py
@@ -4,12 +4,12 @@
 from flask_jwt import current_identity as current_user
 from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
 from sqlalchemy.orm.exc import NoResultFound
+import urllib.error
 
 from app import get_settings
 from app.api.bootstrap import api
 from app.api.helpers.db import safe_query, get_count
-from app.api.helpers.exceptions import ConflictException
-from app.api.helpers.exceptions import ForbiddenException
+from app.api.helpers.exceptions import ConflictException, UnprocessableEntity, ForbiddenException
 from app.api.helpers.files import create_save_image_sizes, make_frontend_url
 from app.api.helpers.mail import send_email_confirmation, send_email_change_user_email, send_email_with_action
 from app.api.helpers.permission_manager import has_access
@@ -66,7 +66,12 @@ def after_create_object(self, user, data, view_kwargs):
         send_email_confirmation(user.email, link)
 
         if data.get('original_image_url'):
-            uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)
+            try:
+                uploaded_images = create_save_image_sizes(data['original_image_url'], 'speaker-image', user.id)
+            except (urllib.error.HTTPError, urllib.error.URLError):
+                raise UnprocessableEntity(
+                    {'source': 'attributes/original-image-url'}, 'Invalid Image URL'
+                )
             uploaded_images['small_image_url'] = uploaded_images['thumbnail_image_url']
             del uploaded_images['large_image_url']
             self.session.query(User).filter_by(id=user.id).update(uploaded_images)
@@ -175,7 +180,12 @@ def before_get_object(self, view_kwargs):
 
     def before_update_object(self, user, data, view_kwargs):
         if data.get('original_image_url') and data['original_image_url'] != user.original_image_url:
-            uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)
+            try:
+                uploaded_images = create_save_image_sizes(data['original_image_url'], 'speaker-image', user.id)
+            except (urllib.error.HTTPError, urllib.error.URLError):
+                raise UnprocessableEntity(
+                    {'source': 'attributes/original-image-url'}, 'Invalid Image URL'
+                )
             data['original_image_url'] = uploaded_images['original_image_url']
             data['small_image_url'] = uploaded_images['thumbnail_image_url']
             data['thumbnail_image_url'] = uploaded_images['thumbnail_image_url']


Test patch:
diff --git a/tests/unittests/api/helpers/test_files.py b/tests/unittests/api/helpers/test_files.py
--- a/tests/unittests/api/helpers/test_files.py
+++ b/tests/unittests/api/helpers/test_files.py
@@ -121,7 +121,7 @@ def test_create_save_resized_image(self):
     def test_create_save_image_sizes(self):
         with app.test_request_context():
             image_url_test = 'https://cdn.pixabay.com/photo/2014/09/08/17/08/hot-air-balloons-439331_960_720.jpg'
-            image_sizes_type = "event"
+            image_sizes_type = "event-image"
             width_large = 1300
             width_thumbnail = 500
             width_icon = 75