def get_file_contents(code: models.File) -> bytes:
"""Get the contents of the given :class:`.models.File`.
:param code: The file object to read.
:returns: The contents of the file with newlines.
"""
if code.is_directory:
raise APIException(
'Cannot display this file as it is a directory.',
f'The selected file with id {code.id} is a directory.',
APICodes.OBJECT_WRONG_TYPE, 400
)
filename = code.get_diskname()
if os.path.islink(filename):
raise APIException(
f'This file is a symlink to `{os.readlink(filename)}`.',
'The file {} is a symlink'.format(code.id), APICodes.INVALID_STATE,
410
)
with open(filename, 'rb') as codefile:
return codefile.read()
评论列表
文章目录