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
1 changed files with 7 additions and 6 deletions

View File

@ -58,16 +58,17 @@ def hash_archive(cnt, dest=None, imposed_hash=None):
def _guess_mime(data): 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", with subprocess.Popen(["file",
"--brief", "--brief",
"--mime-type", "--mime-type",
"-"], env={"LANG": 'C'}, stdin=subprocess.PIPE, stdout=subprocess.PIPE) as p: path], env={"LANG": 'C'}, stdout=subprocess.PIPE) as p:
try:
p.stdin.write(data.encode() if isinstance(data, str) else data)
p.stdin.close()
except BrokenPipeError:
pass
p.wait() p.wait()
os.unlink(path)
if p.returncode == 0: if p.returncode == 0:
return p.stdout.read().decode('utf-8', 'replace').strip() return p.stdout.read().decode('utf-8', 'replace').strip()