pgcontents/query.py
def save_file(db, user_id, path, content, encrypt_func, max_size_bytes):
    """
    Save a file.

    TODO: Update-then-insert is probably cheaper than insert-then-update.
    """
    content = preprocess_incoming_content(
        content,
        encrypt_func,
        max_size_bytes,
    )
    directory, name = split_api_filepath(path)
    with db.begin_nested() as savepoint:
        try:
            res = db.execute(
                files.insert().values(
                    name=name,
                    user_id=user_id,
                    parent_name=directory,
                    content=content,
                )
            )
        except IntegrityError as error:
            # The file already exists, so overwrite its content with the newer
            # version.
            if is_unique_violation(error):
                savepoint.rollback()
                res = db.execute(
                    files.update().where(
                        _file_where(user_id, path),
                    ).values(
                        content=content,
                        created_at=func.now(),
                    )
                )
            else:
                # Unknown error.  Reraise
                raise

    return res
def save_file(db, user_id, path, content, encrypt_func, max_size_bytes):
    content = preprocess_incoming_content(
        content,
        encrypt_func,
        max_size_bytes,
    )
    directory, name = split_api_filepath(path)
    with db.begin_nested() as savepoint:
        try:
            res = db.execute(
                files.insert().values(
                    name=name,
                    user_id=user_id,
                    parent_name=directory,
                    content=content,
                )
            )
        except IntegrityError as error:
            # The file already exists, so overwrite its content with the newer
            # version.
            if is_unique_violation(error):
                savepoint.rollback()
                res = db.execute(
                    files.update().where(
                        _file_where(user_id, path),
                    ).values(
                        content=content,
                        created_at=func.now(),
                    )
                )
            else:
                # Unknown error.  Reraise
                raise

    return res
def save_file ( db , user_id , path , content , encrypt_func , max_size_bytes ) : content = preprocess_incoming_content ( content , encrypt_func , max_size_bytes , ) directory , name = split_api_filepath ( path ) with db . begin_nested ( ) as savepoint : try : res = db . execute ( files . insert ( ) . values ( name = name , user_id = user_id , parent_name = directory , content = content , ) ) except IntegrityError as error : # The file already exists, so overwrite its content with the newer # version. if is_unique_violation ( error ) : savepoint . rollback ( ) res = db . execute ( files . update ( ) . where ( _file_where ( user_id , path ) , ) . values ( content = content , created_at = func . now ( ) , ) ) else : # Unknown error.  Reraise raise return res
Save a file.

    TODO: Update-then-insert is probably cheaper than insert-then-update.
Save a file .