This commit is contained in:
Lynn Ochs 2020-04-02 19:28:10 +02:00
parent 64400a1ade
commit 349ee8a646

8
mailcap-server.py Normal file → Executable file
View File

@ -50,7 +50,7 @@ class Server(object):
os.makedirs(dir)
self._dir = dir
else:
self._dir = tempfile.TemporaryDirectory()
self._dir = tempfile.TemporaryDirectory(prefix="mailcapd")
def close(self):
self._socket.close()
@ -100,12 +100,15 @@ class Server(object):
full_name = os.path.join(self._dir.name, fn)
append = 0
# While the filename already exists, check if we have a hash match
print("Trying path %s" % (full_name,))
while os.path.exists(full_name):
hash = hashlib.sha256()
hash.update(open(full_name, "rb").read())
print("File exists, hash is %s" % (" ".join(["%02x" % (x,) for x in hash.digest()])))
# Found a matching file. Do not receive the file.
if data == hash.digest():
print("Hash matches received hash. Closing connection.")
conn.send(b'\x00')
conn.close()
return full_name, mime
@ -116,7 +119,8 @@ class Server(object):
if len(name_parts) > 1:
full_name = os.path.join(self._dir.name, "%s-%d.%s" % ('.'.join(name_parts[:-1]), append, name_parts[-1]))
else:
full_name = os.path.join(self._dir.name, "%s-%d" % (fn, append, name_parts[-1]))
full_name = os.path.join(self._dir.name, "%s-%d" % (fn, append))
print("Accepted filename %s" % (full_name,))
conn.send(b'\x01')
# Receive the file length