Example #1
0
def create_file():
    if 'file' not in request.files or request.files['file'].filename == '':
        raise NotFound
    id = mongo.save_file(request.files['file'].filename, request.files['file'])
    _, ext = path.splitext(request.files['file'].filename)
    executor.submit(lambda: make_thumbnails(id))
    return json_response(id='{}{}'.format(id, ext))
Example #2
0
def create_file():
    if 'file' not in request.files or request.files['file'].filename == '':
        raise NotFound()

    id = mongo.save_file(request.files['file'].filename, request.files["file"])

    _, ext = path.splitext(request.files['file'].filename)

    return json_response(id='{}{}'.format(id, ext))
Example #3
0
def make_thumbnails(id):
    try:
        file = GridFS(mongo.db).get(id)
    except NoFile:
        raise NotFound()
    img = Image.open(file)
    thumbnails = {}
    for size in (1024, 512, 200):
        t = img.copy()
        t.thumbnail((size, size))
        filename = '{}_{}'.format(id, size)
        filepath = '/tmp/{}'.format(filename)
        t.sava(filepath, 'JPEG')
        with open(filepath, 'rb') as f:
            thumbnails[str(size)] = mongo.save_file(filename, f)
        unlink(filepath)
    mongo.db.fs.files.update({'_id': id}, {'$set': {'thumbnails': thumbnails}})