47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
# standard imports
|
|
import os
|
|
import hashlib
|
|
import logging
|
|
|
|
# local imports
|
|
from .base import *
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
class Attachment(Data):
|
|
|
|
def __init__(self, path='.'):
|
|
super(Attachment, self).__init__()
|
|
self.contents = []
|
|
self.digests = []
|
|
self.path = path
|
|
self.attachment_path = os.path.join(self.path, 'attachments')
|
|
self.__hasher = self.__basehasher
|
|
|
|
|
|
def __basehasher(self, v):
|
|
h = hashlib.sha256()
|
|
h.update(v)
|
|
return h.digest()
|
|
|
|
|
|
def load(self):
|
|
for s in os.listdir(self.attachment_path):
|
|
fp = os.path.realpath(os.path.join(self.attachment_path, s))
|
|
f = open(fp, 'rb')
|
|
r = f.read()
|
|
f.close()
|
|
self.contents.append(fp)
|
|
|
|
z = self.__hasher(r)
|
|
self.digests.append(z)
|
|
|
|
logg.debug('loaded attachment file {} digest {}'.format(fp, z.hex()))
|
|
|
|
|
|
def start(self):
|
|
super(Attachment, self).start()
|
|
|
|
os.makedirs(self.attachment_path)
|