Skip to content

Commit

Permalink
Merge 914ac84 into 0c763a4
Browse files Browse the repository at this point in the history
  • Loading branch information
korrosivesec committed Jan 19, 2018
2 parents 0c763a4 + 914ac84 commit 1a64ffe
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pymisp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,32 +557,37 @@ def add_filename(self, event, filename, category='Artifacts dropped', to_ids=Fal
"""Add filename(s)"""
return self.add_named_attribute(event, 'filename', filename, category, to_ids, comment, distribution, proposal, **kwargs)

def add_attachment(self, event, attachment, category='Artifacts dropped', to_ids=False, comment=None, distribution=None, proposal=False, **kwargs):
def add_attachment(self, event, attachment, category='Artifacts dropped', to_ids=False, comment=None, distribution=None, proposal=False, filename=None, **kwargs):
"""Add an attachment to the MISP event
:param event: The event to add an attachment to
:param attachment: Either a file handle or a path to a file - will be uploaded
:param filename: Explicitly defined attachment filename
"""
if isinstance(attachment, basestring) and os.path.isfile(attachment):
# We have a file to open
filename = os.path.basename(attachment)
if filename is None:
filename = os.path.basename(attachment)
with open(attachment, "rb") as f:
fileData = f.read()
elif hasattr(attachment, "read"):
# It's a file handle - we can read it but it has no filename
fileData = attachment.read()
filename = 'attachment'
if filename is None:
filename = 'attachment'
elif isinstance(attachment, (tuple, list)):
# tuple/list (filename, pseudofile)
filename = attachment[0]
if filename is None:
filename = attachment[0]
if hasattr(attachment[1], "read"):
# Pseudo file
fileData = attachment[1].read()
else:
fileData = attachment[1]
else:
# Plain file content, no filename
filename = 'attachment'
if filename is None:
filename = 'attachment'
fileData = attachment

if not isinstance(fileData, bytes):
Expand Down

0 comments on commit 1a64ffe

Please sign in to comment.