Skip to content

Commit

Permalink
Evgeniy d/fix pdf checker (#32)
Browse files Browse the repository at this point in the history
* Revert "fix (#31)"

This reverts commit cbc63da.

* add decrypted pdf to check_pdf_ok
  • Loading branch information
mrerro authored Feb 26, 2023
1 parent cbc63da commit fb9e2ad
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
8 changes: 3 additions & 5 deletions print_service/routes/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,9 @@ async def upload_file(
if len(memory_file) > settings.MAX_SIZE:
raise HTTPException(413, f'Content too large, {settings.MAX_SIZE} bytes allowed')
await saved_file.write(memory_file)

# if not check_pdf_ok(memory_file):
# await aiofiles.os.remove(path)
# raise HTTPException(415, 'File corrupted')

if not check_pdf_ok(memory_file):
await aiofiles.os.remove(path)
raise HTTPException(415, 'File corrupted')
await file.close()

return {
Expand Down
10 changes: 4 additions & 6 deletions print_service/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from print_service.models import File
from print_service.models import File as FileModel
from print_service.settings import Settings, get_settings
from PyPDF3 import PdfFileReader
from PyPDF3.utils import PyPdfError
from PyPDF4 import PdfFileReader
import aiofiles
import io

Expand Down Expand Up @@ -75,8 +74,7 @@ def get_file(dbsession, pin: str or list[str]):

def check_pdf_ok(f: bytes):
try:
pdf = PdfFileReader(io.BytesIO(f))
info = pdf.getDocumentInfo()
return bool(info)
except PyPdfError:
PdfFileReader(io.BytesIO(f))
return True
except Exception as e:
return False
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ gunicorn
websockets
auth_lib_profcomff[fastapi]
redis
PyPDF3
PyPDF4
22 changes: 20 additions & 2 deletions tests/test_routes/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def test_file_check():
assert check_pdf_ok(open("tests/test_routes/test_files/correct.pdf", "rb").read()) is True


@pytest.mark.skip
def test_upload_and_print_correct_pdf(pin_pdf, client):
pin = pin_pdf
fileName = 'tests/test_routes/test_files/correct.pdf'
Expand All @@ -107,7 +106,6 @@ def test_upload_and_print_correct_pdf(pin_pdf, client):
assert res2.status_code == status.HTTP_200_OK


@pytest.mark.skip
def test_upload_and_print_broken_file(pin_pdf, client):
pin = pin_pdf
fileName = 'tests/test_routes/test_files/broken.pdf'
Expand All @@ -116,3 +114,23 @@ def test_upload_and_print_broken_file(pin_pdf, client):
assert res.status_code == status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
res2 = client.get(f"{url}/{pin}")
assert res2.status_code == status.HTTP_415_UNSUPPORTED_MEDIA_TYPE


def test_upload_and_print_not_pdf_file(pin_pdf, client):
pin = pin_pdf
fileName = 'tests/test_routes/test_files/not_pdf.pdf'
files = {'file': (f"{fileName}", open(f"{fileName}", 'rb'), "application/pdf")}
res = client.post(f"{url}/{pin}", files=files)
assert res.status_code == status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
res2 = client.get(f"{url}/{pin}")
assert res2.status_code == status.HTTP_415_UNSUPPORTED_MEDIA_TYPE


def test_upload_and_print_encrypted_file(pin_pdf, client):
pin = pin_pdf
fileName = 'tests/test_routes/test_files/encrypted.pdf'
files = {'file': (f"{fileName}", open(f"{fileName}", 'rb'), "application/pdf")}
res = client.post(f"{url}/{pin}", files=files)
assert res.status_code == status.HTTP_200_OK
res2 = client.get(f"{url}/{pin}")
assert res2.status_code == status.HTTP_200_OK
Binary file modified tests/test_routes/test_files/broken.pdf
Binary file not shown.
Binary file added tests/test_routes/test_files/encrypted.pdf
Binary file not shown.
Binary file added tests/test_routes/test_files/not_pdf.pdf
Binary file not shown.

0 comments on commit fb9e2ad

Please sign in to comment.