<commit_before>from StringIO import StringIO
from flask import Flask, send_file
from flask import request
from PIL import Image
import requests
import sys

app = Flask(__name__)


@app.route("/")
def get_image():
    url = request.args.get('url', None)
    if not url:
        return "Url must not be None!"
    # TODO test if request is an image

    width = int(request.args.get('width', sys.maxint))
    height = int(request.args.get('height', sys.maxint))
    quality = int(request.args.get('quality', 100))
    response = requests.get(url)

    if height == sys.maxint and width == sys.maxint and quality == 100:
        return response.content, response.status_code, response.headers.items()
    if quality > 100:
        quality = 100
    elif quality < 0:
        # todo better errors
        return "quality must not be under 0!"

    size = (width, height)

    content_type = response.headers.get("content-type")
    response_type, file_type = content_type.split('/')
    if not response_type == "image":
        return "Not an image!"

    img = Image.open(StringIO(response.content))

    img_io = StringIO()
    img.thumbnail(size, Image.ANTIALIAS)
    img.save(img_io, format=file_type, quality=quality)

    img_io.seek(0)

    return send_file(img_io, mimetype=content_type)


if __name__ == "__main__":
    app.run(debug=True)<commit_msg>added redirection if image is smaller as an given tolerance parameter<commit_after>from StringIO import StringIO
from flask import Flask, send_file, request, redirect

from PIL import Image
import requests
import sys

app = Flask(__name__)


def resize_image(file_type, width, height, quality, image):
    size = (width, height)
    img = Image.open(StringIO(image))
    img_io = StringIO()
    img.thumbnail(size, Image.ANTIALIAS)
    img.save(img_io, format=file_type, quality=quality)
    img_io.seek(0)
    return img_io


@app.route("/")
def get_image():
    url = request.args.get('url', None)
    if not url:
        return "URL must not be None!"
    # TODO test if request is an image

    width = int(request.args.get('width', sys.maxint))
    height = int(request.args.get('height', sys.maxint))
    quality = int(request.args.get('quality', 100))
    max_size = int(request.args.get('max_size', 0))

    if max_size > 0:
        max_size *= 1024

    header = requests.head(url).headers
    response_size = header.get('Content-Length')
    in_size = int(response_size) <= max_size

    if (height == sys.maxint and width == sys.maxint and quality == 100) or in_size:
        return redirect(url)

    if quality > 100:
        quality = 100
    elif quality < 0:
        # todo better errors
        return "quality must not be under 0!"

    response = requests.get(url)
    content_type = response.headers.get("content-type")
    response_type, file_type = content_type.split('/')
    if not response_type == "image":
        return "Not an image!"

    img_io = resize_image(file_type, width, height, quality, response.content)

    return send_file(img_io, mimetype=content_type)


if __name__ == "__main__":
    app.run(debug=True)
{'fraction_non_alphanumeric': 0.11565721649484537, 'fraction_numerical': 0.011597938144329897, 'mean_word_length': 3.3184979137691237, 'pattern_counts': {'":': 4, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '267448', 'n_tokens_mistral': 1022, 'n_tokens_neox': 958, 'n_words': 276}