Fix content detection with libmagic when dealing with stdin... prefers using a dedicated file

This commit is contained in:
nemunaire 2020-03-27 01:07:40 +01:00
parent f2cac09f6f
commit e6e622df94

View File

@ -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()