def add_file(parent_dir, filename, content):
    # First check if parent dir exist
    parent_dir = sanitize_dir_path(parent_dir)
    try:
        dir = get_dir(parent_dir)
    except Entry.DoesNotExist:
        raise NoSuchEntry(parent_dir)

    # save file in Stroage
    filepath = os.path.join(parent_dir, filename)
    try:
        url = save_file_obj(parent_dir, filename, content)
    except:
        tb = traceback.format_exc()
        logging.exception('Failed to save file %s', filepath)
        raise StorageError('Failed to save %s: %s' % (filepath, tb))

    newfile = Entry(dir_name=parent_dir,
                    base_name=filename,
                    size=len(content),
                    is_dir=False,
                    url=url)
    try:
        newfile.save()
    except IntegrityError:
        raise AlreadyExistError(filepath)

    dir.ctime = datetime.datetime.now()
    dir.save()

    return transform_entry(parent_dir, newfile)
def get_root_dir():
    dir_name = ''
    base_name = '/'
    try:
        return Entry.objects.get(dir_name_hash=hash_dir_name(dir_name),
                                 base_name=base_name,
                                 is_dir=True)
    except Entry.DoesNotExist:
        d = Entry(dir_name='', base_name='/', is_dir=True)
        d.save()
        return d
def add_dir(parent_dir, dname):
    # Ensure the parent dir exist
    parent_dir = sanitize_dir_path(parent_dir)
    try:
        get_dir(parent_dir)
    except Entry.DoesNotExist:
        raise NoSuchEntry(parent_dir)

    newdir = Entry(dir_name=parent_dir, base_name=dname, is_dir=True)

    try:
        newdir.save()
    except IntegrityError:
        raise AlreadyExistError(os.path.join(parent_dir, dname))

    return transform_entry(parent_dir, newdir)