Compare commits

...

2 Commits

Author SHA1 Message Date
ad306ad0e9
Improve mime type handling 2019-05-10 23:31:38 +02:00
bf691d4b0c
Use file(1) to determine the mime type 2019-05-10 23:30:57 +02:00
2 changed files with 8 additions and 7 deletions

View File

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

View File

@ -112,8 +112,8 @@ if __name__ == "__main__":
if rv:
f, mime = rv
print(f, mime)
match = mailcap.findmatch(caps, mime, filename=f)
if match:
match = mailcap.findmatch(caps, mime.split(';')[0], filename=f)
if match and match[0]:
os.system(match[0])
if 'nodelete' not in match[1]:
os.remove(f)