示例#1
0
def avatar_file_path(instance=None, filename=None, size=None, ext=None):
    tmppath = [settings.AVATAR_STORAGE_DIR]
    if settings.AVATAR_HASH_USERDIRNAMES:
        tmp = hashlib.md5(get_username(instance.user)).hexdigest()
        tmppath.extend([tmp[0], tmp[1], get_username(instance.user)])
    else:
        tmppath.append(get_username(instance.user))
    if not filename:
        # Filename already stored in database
        filename = instance.avatar.name
        if ext and settings.AVATAR_HASH_FILENAMES:
            # An extension was provided, probably because the thumbnail
            # is in a different format than the file. Use it. Because it's
            # only enabled if AVATAR_HASH_FILENAMES is true, we can trust
            # it won't conflict with another filename
            (root, oldext) = os.path.splitext(filename)
            filename = root + "." + ext
    else:
        # File doesn't exist yet
        if settings.AVATAR_HASH_FILENAMES:
            (root, ext) = os.path.splitext(filename)
            filename = hashlib.md5(force_bytes(filename)).hexdigest()
            filename = filename + ext
    if size:
        tmppath.extend(['resized', str(size)])
    tmppath.append(os.path.basename(filename))
    return os.path.join(*tmppath)
示例#2
0
def avatar_file_path(instance=None, filename=None, size=None, ext=None):
    tmppath = [settings.AVATAR_STORAGE_DIR]
    if settings.AVATAR_HASH_USERDIRNAMES:
        tmp = hashlib.md5(get_username(instance.user)).hexdigest()
        tmppath.extend([tmp[0], tmp[1], get_username(instance.user)])
    else:
        tmppath.append(get_username(instance.user))
    if not filename:
        # Filename already stored in database
        filename = instance.avatar.name
        if ext and settings.AVATAR_HASH_FILENAMES:
            # An extension was provided, probably because the thumbnail
            # is in a different format than the file. Use it. Because it's
            # only enabled if AVATAR_HASH_FILENAMES is true, we can trust
            # it won't conflict with another filename
            (root, oldext) = os.path.splitext(filename)
            filename = root + "." + ext
    else:
        # File doesn't exist yet
        if settings.AVATAR_HASH_FILENAMES:
            (root, ext) = os.path.splitext(filename)
            filename = hashlib.md5(force_bytes(filename)).hexdigest()
            filename = filename + ext
    if size:
        tmppath.extend(['resized', str(size)])
    tmppath.append(os.path.basename(filename))
    return os.path.join(*tmppath)
示例#3
0
def music_store_path(instance,filename):
    print 'getting path'
    tmppath = MEDIA_ROOT+'/music/'+ get_username(instance.content.user)
    #avoid conflicting file names
    new_name = hashlib.md5(filename).hexdigest()+".mp3"
    save_to = tmppath+"/"+new_name
    print save_to
    return save_to
示例#4
0
def get_cache_key(user_or_username, size, prefix):
    """
    Returns a cache key consisten of a username and image size.
    """
    if isinstance(user_or_username, get_user_model()):
        user_or_username = get_username(user_or_username)
    key = six.u('%s_%s_%s') % (prefix, user_or_username, size)
    return six.u('%s_%s') % (slugify(key)[:100],
                             hashlib.md5(force_bytes(key)).hexdigest())
示例#5
0
def logo_file_path(instance=None, filename=None, size=None, ext=None):
    tmppath = [settings.LOGO_STORAGE_DIR]

    tmp = hashlib.md5(get_username(instance.user)).hexdigest()
    tmppath.extend([tmp[0], tmp[1], get_username(instance.user)])

    if not filename:
        # Filename already stored in database
        filename = instance.logo.name
        if ext:
            (root, oldext) = os.path.splitext(filename)
            filename = root + "." + ext
    else:
        (root, ext) = os.path.splitext(filename)
        filename = hashlib.md5(force_bytes(filename)).hexdigest()
        filename = filename + ext

    tmppath.append(os.path.basename(filename))
    return os.path.join(*tmppath)
示例#6
0
def avatar_file_path(instance=None, filename=None, size=None, ext=None, new=False):
    tmppath = [settings.AVATAR_STORAGE_DIR]

    if settings.AVATAR_HASH_USERDIRNAMES:
        tmp = hashlib.md5(get_username(instance.user)).hexdigest()
        tmppath.extend([tmp[0], tmp[1], get_username(instance.user)])
    else:
        tmppath.append(get_username(instance.user))
    if not filename:
        # Filename already stored in database
        filename = instance.avatar.name
        if ext and settings.AVATAR_HASH_FILENAMES:
            # An extension was provided, probably because the thumbnail
            # is in a different format than the file. Use it. Because it's
            # only enabled if AVATAR_HASH_FILENAMES is true, we can trust
            # it won't conflict with another filename
            (root, oldext) = os.path.splitext(filename)
            filename = root + "." + ext
    else:
        # File doesn't exist yet
        if settings.AVATAR_HASH_FILENAMES:
            (root, oldext) = os.path.splitext(filename)
            filename = hashlib.md5(force_bytes(filename)).hexdigest()
            filename = filename + oldext
    if size:
        tmppath.extend(["resized", str(size)])
    tmppath.append(os.path.basename(filename))
    filename = os.path.join("sites", connection.schema_name, *tmppath)

    # ext overrides current extension
    (root, oldext) = os.path.splitext(filename)
    if ext and ext != oldext:
        filename = root + "." + ext
        if new:
            # file does not yet exist, avoid filename collision
            if instance is not None:
                filename = instance.avatar.storage.get_available_name(filename)
            else:
                pass  # Not sure how to avoid collisions without storage

    return filename