# -*- coding: utf-8 -*-

import os
import mimetypes
import arrow
from hurry.filesize import size


additional_file_types = {".md": "text/markdown"}


def datetimeformat(date_str):
    dt = arrow.get(date_str)
    return dt.humanize()


def file_type(key):
    file_info = os.path.splitext(key)
    file_extension = file_info[1]
    try:
        return mimetypes.types_map[file_extension]
    except KeyError:
        filetype = "Unknown"
        if file_info[0].startswith(".") and file_extension == "":
            filetype = "text"

        if file_extension in additional_file_types.keys():
            filetype = additional_file_types[file_extension]

        return filetype


def file_name(key):
    return os.path.basename(key)


def folder_name(key):
    return os.path.basename(os.path.normpath(key))


def file_size(key):
    return size(key)
