# coding: utf-8

# imports
import os, unicodedata, re
from time import gmtime, strftime, localtime, mktime, time
from tempfile import NamedTemporaryFile

# django imports
from django.utils.translation import ugettext as _
from django.core.files import File
from django.utils.encoding import smart_unicode

# filebrowser imports
from filebrowser.settings import *

# PIL import
if STRICT_PIL:
    from PIL import Image
else:
    try:
        from PIL import Image
    except ImportError:
        import Image

def validate_path(path):
    '''Method to protect against file path manipulation.'''
    
    (head, tail) = os.path.split(path)
    if '.' in head:
        raise Exception("File Browser was about to open an invalid file path: %s" % head)
    
    return path

def path_strip(path, root):
    if not path or not root:
        return path
    path = os.path.normcase(path)
    root = os.path.normcase(root)
    if path.startswith(root):
        return path[len(root):]
    return path

def url_strip(url, root):
    if not url or not root:
        return url
    if url.startswith(root):
        return url[len(root):]
    return url

def get_version_filename(filename, version_prefix):
    filename, ext = os.path.splitext(filename)
    version_filename = filename + "_" + version_prefix + ext
    return version_filename