def extract(
file: FileStorage,
ignore_filter: IgnoreFilterManager = None,
handle_ignore: IgnoreHandling = IgnoreHandling.keep
) -> t.Optional[ExtractFileTree]:
"""Extracts all files in archive with random name to uploads folder.
:param werkzeug.datastructures.FileStorage file: The file to extract.
:param ignore_filter: What files should be ignored in the given archive.
This can only be None when ``handle_ignore`` is
``IgnoreHandling.keep``.
:param handle_ignore: How should ignored file be handled.
:returns: A file tree as generated by
:py:func:`rename_directory_structure`.
"""
if handle_ignore == IgnoreHandling.keep and ignore_filter is None:
ignore_filter = IgnoreFilterManager([])
elif ignore_filter is None: # pragma: no cover
raise ValueError
tmpdir = extract_to_temp(
file,
ignore_filter,
handle_ignore,
)
rootdir = tmpdir.rstrip(os.sep)
start = rootdir.rfind(os.sep) + 1
try:
res = rename_directory_structure(tmpdir)[tmpdir[start:]]
filename: str = file.filename.split('.')[0]
if not res:
return None
elif len(res) > 1:
return {filename: res if isinstance(res, list) else [res]}
elif not isinstance(res[0], t.MutableMapping):
return {filename: res}
else:
return res[0]
finally:
shutil.rmtree(tmpdir)
评论列表
文章目录