-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sumathi.thirumani
committed
Jun 26, 2024
1 parent
19b454a
commit c9e60a8
Showing
4 changed files
with
53 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
request-management-api/request_api/services/email/templates/embeddedimagehandler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
from bs4 import BeautifulSoup | ||
import base64 | ||
|
||
""" | ||
This class is leveraged to handle the embedded images in mail body. | ||
""" | ||
class embeddedimagehandler: | ||
|
||
""" | ||
This function can be enhanced to store the image in S3 & re-use (if required) | ||
Please note; in case of storing in S3. This function should be invoked before persisting to DB. | ||
""" | ||
def formatembeddedimage(self, content): | ||
soup = BeautifulSoup(content, "html.parser") | ||
img_nodes = soup.find_all("img") | ||
embeddedimages = [] | ||
for img in img_nodes: | ||
if 'base64' in img['src']: | ||
imgsrc = img['src'] | ||
imagename = "image"+str(len(embeddedimages)) | ||
img['src'] = "cid:"+imagename | ||
img['style'] = "width:180px;height:50px;" | ||
imgbytes = imgsrc.split("base64,")[1] | ||
embeddedimages.append({"cid": imagename, "bytes": base64.b64decode(str(imgbytes)), 'type': self.__getmimetype(imgsrc)}) | ||
return soup.prettify(), embeddedimages | ||
|
||
|
||
def __suportedmimetypes(self): | ||
return [ | ||
{ "prefix": "/9j/", "mimetype" : "image/jpg", "extension": "jpg"}, | ||
{ "prefix": "iVBORw0KGgo", "mimetype" : "image/png", "extension": "png"} | ||
] | ||
|
||
|
||
def __getmimetype(self, b64): | ||
for entry in self.__suportedmimetypes(): | ||
if entry['prefix'] in b64: | ||
return entry['extension'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters