diff --git a/README.md b/README.md index ac63cb4..b889b1b 100644 --- a/README.md +++ b/README.md @@ -50,5 +50,5 @@ Verify Tweet is released under GNU Affero General Public License v3.0. ## Future features -- [ ] Support for Image links +- [x] Support for Image links - [ ] Support for Tweets with replies diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index 274ba2d..0000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,62 +0,0 @@ --i https://pypi.org/simple -alabaster==0.7.12 -atomicwrites==1.3.0 -attrs==19.1.0 -autopep8==1.4.4 -awscli==1.16.196 -babel==2.7.0 -bandit==1.6.2 -bleach==3.1.1 -botocore==1.12.186 -certifi==2019.6.16 -chardet==3.0.4 -colorama==0.3.9 -coverage==4.5.3 -docutils==0.14 -gitdb2==2.0.5 -gitpython==2.1.11 -hypothesis==4.27.0 -idna==2.8 -imagesize==1.1.0 -importlib-metadata==0.18 -jinja2==2.10.1 -jmespath==0.9.4 -markupsafe==1.1.1 -more-itertools==7.1.0 -packaging==19.0 -pbr==5.4.0 -pkginfo==1.5.0.1 -pluggy==0.12.0 -py==1.8.0 -pyasn1==0.4.5 -pycodestyle==2.5.0 -pygments==2.4.2 -pyparsing==2.4.0 -pytest-cov==2.7.1 -pytest==5.0.1 -python-dateutil==2.8.0 -pytz==2019.1 -pyyaml==5.1 ; python_version != '2.6' -readme-renderer==24.0 -requests-toolbelt==0.9.1 -requests==2.22.0 -rsa==3.4.2 -s3transfer==0.2.1 -six==1.12.0 -smmap2==2.0.5 -snowballstemmer==1.9.0 -sphinx==2.1.2 -sphinxcontrib-applehelp==1.0.1 -sphinxcontrib-devhelp==1.0.1 -sphinxcontrib-htmlhelp==1.0.2 -sphinxcontrib-jsmath==1.0.1 -sphinxcontrib-qthelp==1.0.2 -sphinxcontrib-serializinghtml==1.1.3 -stevedore==1.30.1 -tqdm==4.32.2 -twine==1.13.0 -urllib3==1.25.3 -wcwidth==0.1.7 -webencodings==0.5.1 -yapf==0.27.0 -zipp==0.5.2 diff --git a/verifytweet/app.py b/verifytweet/app.py index 94b67ca..14750e7 100644 --- a/verifytweet/app.py +++ b/verifytweet/app.py @@ -73,14 +73,20 @@ def verify_tweet(): """ logger.info('Received data for processing...') - data_type = request.form['type'] - request_image = request.files['data'] - if not data_type or not request_image: - return "Missing form fields", 400 try: - file_path = image_uploader.save_to_disk(request_image) - rest_controller = controller.NonAPIApproach() - result, controller_status = rest_controller.exec(file_path) + data_type = request.form['type'] + if not data_type: + return "Missing type", 400 + if data_type == 'image': + request_image = request.files['data'] + if not request_image: + return "Missing form fields", 400 + file_path = image_uploader.save_to_disk(request_image) + if data_type == 'link': + request_link = request.form['url'] + file_path = image_uploader.save_from_url(request_link) + rest_controller = controller.NonAPIApproach() + result, controller_status = rest_controller.exec(file_path) except Exception as e: logger.exception(e) return jsonify({ diff --git a/verifytweet/config/settings.py b/verifytweet/config/settings.py index 3b6552c..1614df5 100644 --- a/verifytweet/config/settings.py +++ b/verifytweet/config/settings.py @@ -30,6 +30,9 @@ def no_of_workers(): twice the cpu count. """ + if os.getenv('NUM_WORKERS') and os.getenv('NUM_WORKERS').isdigit(): + return int(os.getenv('NUM_WORKERS')) + return multiprocessing.cpu_count() * 2 + 1 @@ -39,7 +42,7 @@ class Config(object): IMAGEMAGICK_PATH = os.getenv('IMAGEMAGICK_PATH') if os.getenv( 'IMAGEMAGICK_PATH') else "convert" - FILE_DIRECTORY = tempfile.mkdtemp() + FILE_DIRECTORY = os.getenv('FILE_DIRECTORY') if os.getenv('FILE_DIRECTORY') else tempfile.mkdtemp() TWEET_MAX_STORE = 150 RUN_METHOD = "cli" LOG_LEVEL = logging.DEBUG if os.getenv('VERIFYTWEET_DEBUG') else logging.INFO diff --git a/verifytweet/util/uploader.py b/verifytweet/util/uploader.py index aaca7ff..c7898da 100644 --- a/verifytweet/util/uploader.py +++ b/verifytweet/util/uploader.py @@ -18,6 +18,9 @@ import os import uuid +import shutil + +import requests from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage @@ -46,6 +49,26 @@ def save_to_disk(file_obj): return saved_file_path return None +def save_from_url(image_url: str): + """Saves image given via url to disk + """ + if not isinstance(image_url, str): + raise TypeError('image_url must be a string') + if not image_url: + raise ValueError('image_url has to be a valid string') + r = requests.get(image_url, stream=True, allow_redirects=True) + if r.status_code != 200: + raise FileNotFoundError() + filename = image_url.split("/")[-1] + if filename and allowed_file(filename): + saved_file_path = os.path.join(app_config.FILE_DIRECTORY, filename) + logger.info('Saving file to path: ' + saved_file_path) + r.raw.decode_content = True + with open(saved_file_path, 'wb') as f: + shutil.copyfileobj(r.raw, f) + logger.info('Image successfully downloaded') + return saved_file_path + def allowed_file(filename): """Checks if uploaded file has valid extenstion """