Skip to content

Commit

Permalink
Merge pull request #1133 from bcgov/dev-AA-testrook-FOIMOD-3439
Browse files Browse the repository at this point in the history
#FOIMOD-3439 zipping service update for removing sensive data
  • Loading branch information
abin-aot authored Sep 5, 2024
2 parents d8bf712 + 9c9ea4f commit 4b1bf7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Binary file modified computingservices/ZippingServices/requirements.txt
Binary file not shown.
29 changes: 27 additions & 2 deletions computingservices/ZippingServices/services/zipperservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .notificationservice import notificationservice
import json
import traceback

import PyPDF2

def processmessage(message):
try:
Expand Down Expand Up @@ -110,8 +110,16 @@ def __zipfilesandupload(_message, s3credentials):
for fileobj in _jsonfiles:
filename = fileobj["filename"]
print("\nfilename:",filename)

_docbytes = __getdocumentbytearray(fileobj, s3credentials)
_formattedbytes = None
if(filename == "{0}.pdf".format(_message.requestnumber)):
try:
_formattedbytes = __removesensitivecontent(_docbytes)
except Exception:
print(traceback.format_exc())
zip.writestr(
filename, __getdocumentbytearray(fileobj, s3credentials)
filename, _docbytes if _formattedbytes is None else _formattedbytes
)

tp.seek(0)
Expand Down Expand Up @@ -143,3 +151,20 @@ def __getzipfilepath(foldername, filename):
if foldername is not None
else filename + ".zip"
)


def __removesensitivecontent(documentbytes):
# clear metadata
reader2 = PyPDF2.PdfReader(BytesIO(documentbytes))
# Check if metadata exists.
if reader2.metadata is not None:
# Create a new PDF file without metadata.
writer = PyPDF2.PdfWriter()
# Copy pages from the original PDF to the new PDF.
for page_num in range(len(reader2.pages)):
page = reader2.pages[page_num]
writer.add_page(page)
#writer.remove_links() # to remove comments.
buffer = BytesIO()
writer.write(buffer)
return buffer.getvalue()

0 comments on commit 4b1bf7e

Please sign in to comment.