From e6e622df94fa27ff36ca9bb2e8afac8bd75c7ead Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 27 Mar 2020 01:07:40 +0100 Subject: [PATCH] Fix content detection with libmagic when dealing with stdin... prefers using a dedicated file --- archive.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/archive.py b/archive.py index ebba1fa..139a94d 100644 --- a/archive.py +++ b/archive.py @@ -58,16 +58,17 @@ def hash_archive(cnt, dest=None, imposed_hash=None): def _guess_mime(data): + fd, path = tempfile.mkstemp() + with os.fdopen(fd, 'wb') as fp: + fp.write(data.encode() if isinstance(data, str) else data) + with subprocess.Popen(["file", "--brief", "--mime-type", - "-"], env={"LANG": 'C'}, stdin=subprocess.PIPE, stdout=subprocess.PIPE) as p: - try: - p.stdin.write(data.encode() if isinstance(data, str) else data) - p.stdin.close() - except BrokenPipeError: - pass + path], env={"LANG": 'C'}, stdout=subprocess.PIPE) as p: p.wait() + os.unlink(path) + if p.returncode == 0: return p.stdout.read().decode('utf-8', 'replace').strip()