Use file(1) to determine the mime type

This commit is contained in:
Lynn Ochs 2019-05-10 23:30:57 +02:00
parent c2d48d48b1
commit bf691d4b0c
Signed by: apo
GPG Key ID: 588AD47B1D06BE99

View File

@ -1,18 +1,19 @@
#!python3 #!/usr/bin/python3
import struct import struct
import socket import socket
import os import os
import sys import sys
import mimetypes import mimetypes
import subprocess
s = socket.socket(socket.AF_UNIX) s = socket.socket(socket.AF_UNIX)
s.connect(os.path.join(os.environ["HOME"], ".mailcap.sock")) s.connect(os.path.join(os.environ["HOME"], ".mailcap.sock"))
file = sys.argv[1] filename = sys.argv[1]
mime = mimetypes.guess_type(file)[0] mime = subprocess.run(["file", "-i", "-b", filename], check=True, stdout=subprocess.PIPE).stdout.decode('utf-8')
data = open(file, "rb").read() data = open(filename, "rb").read()
def strtr(x, c="B"): def strtr(x, c="B"):
return struct.pack(c, len(x)) + x 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"))