# 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
def get_file(filename):
    media_dir = app._config['henet']['media_dir']
    fullpath = os.path.join(media_dir, filename)
    mimetype = guess_type(fullpath)[0]

    return static_file(filename, root=media_dir,
                       mimetype=mimetype)


@get("/thumbnail/<size>/<filename>", no_i18n=True)
def get_media_thumbnail(size, filename):
    filename = filename.decode('utf8')

    ext = os.path.splitext(filename)[-1].lower()

    if ext == '.pdf':
        redirect('/resources/images/pdf-200x200.png')
        return

    if ext not in  ('.jpg', '.png', '.jpeg', '.bmp'):
        redirect('/resources/images/blank.png')
        return

    media_dir = app._config['henet']['media_dir']
    thumbnails_dir = app._config['henet']['thumbnails_dir']

    thumbname = size + '-' + filename
    thumbnail_file = os.path.join(thumbnails_dir, thumbname)

    if not os.path.exists(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

def get_file(filename):
    media_dir = app._config['henet']['media_dir']
    fullpath = os.path.join(media_dir, filename)
    mimetype = guess_type(fullpath)[0]

    return static_file(filename, root=media_dir,
                       mimetype=mimetype)


@get("/thumbnail/<size>/<filename>", no_i18n=True)
def get_media_thumbnail(size, filename):
    filename = filename.decode('utf8')

    ext = os.path.splitext(filename)[-1].lower()

    if ext == '.pdf':
        redirect('/resources/images/pdf-200x200.png')
        return

    if ext not in  ('.jpg', '.png', '.jpeg', '.bmp'):
        redirect('/resources/images/blank.png')
        return

    media_dir = app._config['henet']['media_dir']
    thumbnails_dir = app._config['henet']['thumbnails_dir']

    thumbname = size + '-' + filename
    thumbnail_file = os.path.join(thumbnails_dir, thumbname)

    if not os.path.exists(thumbnail_file):

        image_file = os.path.join(media_dir, filename)