From bf691d4b0c8a3b0dbbb94db4df2f75c09d42cf95 Mon Sep 17 00:00:00 2001 From: Valentin Ochs Date: Fri, 10 May 2019 23:30:57 +0200 Subject: [PATCH] Use file(1) to determine the mime type --- mailcap-client.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mailcap-client.py b/mailcap-client.py index e4d6fbd..3d6af95 100644 --- a/mailcap-client.py +++ b/mailcap-client.py @@ -1,18 +1,19 @@ -#!python3 +#!/usr/bin/python3 import struct import socket import os import sys import mimetypes +import subprocess s = socket.socket(socket.AF_UNIX) s.connect(os.path.join(os.environ["HOME"], ".mailcap.sock")) -file = sys.argv[1] -mime = mimetypes.guess_type(file)[0] -data = open(file, "rb").read() +filename = sys.argv[1] +mime = subprocess.run(["file", "-i", "-b", filename], check=True, stdout=subprocess.PIPE).stdout.decode('utf-8') +data = open(filename, "rb").read() def strtr(x, c="B"): return struct.pack(c, len(x)) + x -s.sendall(strtr(file.encode('utf-8')) + strtr(mime.encode('utf-8')) + strtr(data, "!I")) +s.sendall(strtr(filename.encode('utf-8')) + strtr(mime.encode('utf-8')) + strtr(data, "!I"))