# Path: henet/util.py
# def file_age(path):
#     return os.stat(path).st_mtime
# 
# Path: henet/events.py
# def emit(event, **data):
#     for subscriber in _SUBSCRIBERS[event]:
#         subscriber(event, **data)
#     for subscriber in _SUBSCRIBERS[ALL_EVENTS]:
#         subscriber(event, **data)
# 
# EVENT_DELETED_CONTENT = 1
# 
# EVENT_CREATED_CONTENT = 2


import os
import datetime
from mimetypes import guess_type
from bottle import route, request, app, post, redirect, get, static_file
from bottle_utils.csrf import csrf_token, csrf_protect
from PIL import Image
from henet.util import file_age
from henet.events import emit, EVENT_DELETED_CONTENT, EVENT_CREATED_CONTENT
        image = Image.open(image_file)
        image.thumbnail(size)
        image.save(thumbnail_file, 'JPEG')


    mimetype = guess_type(thumbnail_file)[0]
    return static_file(thumbname, root=thumbnails_dir,
                       mimetype=mimetype)


@post("/delete/media/<filename:path>", no_i18n=True)
@csrf_protect
def del_media(filename):
    filename = filename.decode('utf8')

    # removing file
    media_dir = app._config['henet']['media_dir']
    path = os.path.join(media_dir, filename)
    if os.path.exists(path):
        os.remove(path)
        emit(EVENT_DELETED_CONTENT, filename=path)

    # removing any thumbnail
    thumbnails_dir = app._config['henet']['thumbnails_dir']
    for thumbname in os.listdir(thumbnails_dir):
        if not thumbname.endswith('-' + filename):
            continue
        thumbnail_file = os.path.join(thumbnails_dir, thumbname)
        if os.path.exists(thumbnail_file):
            os.remove(thumbnail_file)

# Path: henet/util.py
# def file_age(path):
#     return os.stat(path).st_mtime
# 
# Path: henet/events.py
# def emit(event, **data):
#     for subscriber in _SUBSCRIBERS[event]:
#         subscriber(event, **data)
#     for subscriber in _SUBSCRIBERS[ALL_EVENTS]:
#         subscriber(event, **data)
# 
# EVENT_DELETED_CONTENT = 1
# 
# EVENT_CREATED_CONTENT = 2


# Path: henet/views/media.py
import os
import datetime
from mimetypes import guess_type
from bottle import route, request, app, post, redirect, get, static_file
from bottle_utils.csrf import csrf_token, csrf_protect
from PIL import Image
from henet.util import file_age
from henet.events import emit, EVENT_DELETED_CONTENT, EVENT_CREATED_CONTENT

        image = Image.open(image_file)
        image.thumbnail(size)
        image.save(thumbnail_file, 'JPEG')


    mimetype = guess_type(thumbnail_file)[0]
    return static_file(thumbname, root=thumbnails_dir,
                       mimetype=mimetype)


@post("/delete/media/<filename:path>", no_i18n=True)
@csrf_protect
def del_media(filename):
    filename = filename.decode('utf8')

    # removing file
    media_dir = app._config['henet']['media_dir']
    path = os.path.join(media_dir, filename)
    if os.path.exists(path):
        os.remove(path)
        emit(EVENT_DELETED_CONTENT, filename=path)

    # removing any thumbnail
    thumbnails_dir = app._config['henet']['thumbnails_dir']
    for thumbname in os.listdir(thumbnails_dir):
        if not thumbname.endswith('-' + filename):
            continue
        thumbnail_file = os.path.join(thumbnails_dir, thumbname)
        if os.path.exists(thumbnail_file):
            os.remove(thumbnail_file)

            emit(EVENT_DELETED_CONTENT, filename=thumbname)