Ejemplo n.º 1
0
 def test_image_resize(self):
     os.environ["IMAGE_RESIZE_PIXELS"] = str(150)
     test_jpg = requests.get("https://i.imgur.com/qRWH5.jpg")
     image = resize_image(test_jpg.content)
     file_photodata = BytesIO(image)
     working_image = Image.open(file_photodata)
     edited_width = working_image.size[0]
     print(edited_width)
     assert edited_width == 300
Ejemplo n.º 2
0
Archivo: s3.py Proyecto: jmduke/corji
def put(emoji, corgis, override_existing_file=False):
    """Places all corgis in a bucket for the given emoji.

    Returns True if at least one put is successful; returns False otherwise.
    """

    success = False

    for i, corgi in enumerate(corgis):
        s3_key = get_file_name_from_emoji(i, emoji)
        # see if this corgi already exists in s3 bucket
        if 'Contents' in all_objects:
            possible_s3_entry = next(
                (item for item in all_objects['Contents'] if item['Key'] == s3_key), None)
        else:
            possible_s3_entry = None
        try:
            if not possible_s3_entry or override_existing_file:
                logger.debug("Adding %s to remote cache", s3_key)
                logger.debug(
                    "Downloading corgi %s in prep for remote cache", corgi)
                picture_request = requests.get(corgi)
                picture_body = picture_request.content
                content_type = get_content_type_header(picture_request)

                # If the image is greater than the max size, resize it no matter what.
                original_filesize = int(picture_request.headers['content-length'])
                if Config.IMAGE_RESIZE or original_filesize > Config.MAXIMUM_S3_FILESIZE:
                    file_photodata = BytesIO(picture_request.content)
                    working_image = Image.open(file_photodata)
                    original_width = working_image.size[0]

                    # Don't resize gifs.
                    if content_type != "image/gif" and original_width > Config.IMAGE_RESIZE_PIXELS:
                        picture_body = resize_image(picture_request.content)
                        content_type = "image/jpeg"

                logger.debug("Adding %s to remote cache", s3_key)

                aws_s3_client.put_object(Body=picture_body,
                                         ContentType=content_type,
                                         Key=s3_key,
                                         Bucket=Config.AWS_S3_CACHE_BUCKET_NAME)
                success = True

            else:
                logger.debug("%s found in remote cache. Skipping", s3_key)
        except OSError as e:
            logger.error("Error occurred resizing %s.", s3_key, str(e))
        except Exception as e:
            logger.error("Error occurred adding %s to S3.", s3_key, str(e))

        return success