示例#1
0
文件: image.py 项目: jmduke/corji
 def test_image_identification(self):
     test_png = requests.get("http://i.imgur.com/OacBzQQ.png")
     test_jpg = requests.get("https://i.imgur.com/T0vc7TU.jpg")
     test_nonsense = requests.get("http://pugandchalice.com/")
     png_header = get_content_type_header(test_png)
     jpg_header = get_content_type_header(test_jpg)
     nonsense_header = get_content_type_header(test_nonsense)
     assert jpg_header == "image/jpeg"
     assert png_header == "image/png"
     assert nonsense_header == "image/jpeg"
示例#2
0
文件: s3.py 项目: 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