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
Showing
6 changed files
with
96 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,45 @@ | ||
from fastapi import APIRouter, File | ||
from fastapi.encoders import jsonable_encoder | ||
from fastapi.responses import JSONResponse | ||
from pydantic import ValidationError | ||
|
||
from app.factories.response import ResponseFactory | ||
from app.schemas.payload import Payload | ||
from app.schemas.payload import FilePayload, Payload | ||
from app.schemas.response import Response | ||
|
||
router = APIRouter() | ||
|
||
|
||
async def _analyze(file: bytes) -> Response: | ||
try: | ||
payload = FilePayload(file=file) | ||
except ValidationError as exc: | ||
return JSONResponse( | ||
status_code=422, content=jsonable_encoder({"detail": exc.errors()}) | ||
) | ||
|
||
return await ResponseFactory.from_bytes(payload.file) | ||
|
||
|
||
@router.post( | ||
"/", | ||
response_model=Response, | ||
response_description="Return a parsed result", | ||
summary="Parse an eml", | ||
description="Parse an eml and return a parsed result", | ||
response_description="Return an analysis result", | ||
summary="Analyze an eml", | ||
description="Analyze an eml and return an analysis result", | ||
status_code=200, | ||
) | ||
async def analyze(payload: Payload) -> Response: | ||
eml_file = payload.eml_file.encode() | ||
return await ResponseFactory.from_bytes(eml_file) | ||
return await _analyze(payload.file.encode()) | ||
|
||
|
||
@router.post( | ||
"/file", | ||
response_model=Response, | ||
response_description="Return a parsed result", | ||
summary="Parse an eml", | ||
description="Parse an eml and return a parsed result", | ||
response_description="Return an analysis result", | ||
summary="Analyze an eml", | ||
description="Analyze an eml and return an analysis result", | ||
status_code=200, | ||
) | ||
async def analyze_file(file: bytes = File(...)) -> Response: | ||
return await ResponseFactory.from_bytes(file) | ||
return await _analyze(file) |
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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
from fastapi_utils.api_model import APIModel | ||
from pydantic import validator | ||
|
||
from app.services.validator import is_eml_file | ||
|
||
|
||
class Payload(APIModel): | ||
eml_file: str | ||
file: str | ||
|
||
|
||
class FilePayload(APIModel): | ||
file: bytes | ||
|
||
@validator("file") | ||
def eml_file_must_be_eml(cls, v: bytes): | ||
if is_eml_file(v) is False: | ||
raise ValueError("Invalid EML file.") | ||
return v |
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,15 @@ | ||
from typing import cast | ||
|
||
import magic | ||
|
||
VALID_MIME_TYPES = ["message/rfc822", "text/html", "text/plain"] | ||
|
||
|
||
def is_eml_file(data: bytes) -> bool: | ||
detected = magic.detect_from_content(data) | ||
mime_type = cast(str, detected.mime_type) | ||
|
||
if mime_type in VALID_MIME_TYPES: | ||
return True | ||
|
||
return False |
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
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,24 @@ | ||
import pytest | ||
|
||
from app.schemas.payload import FilePayload | ||
|
||
|
||
def test_sample_eml(sample_eml: bytes): | ||
FilePayload(file=sample_eml) | ||
|
||
|
||
def test_multipart_eml(multipart_eml: bytes): | ||
FilePayload(file=multipart_eml) | ||
|
||
|
||
def test_encrypted_docx_eml(encrypted_docx_eml: bytes): | ||
FilePayload(file=encrypted_docx_eml) | ||
|
||
|
||
def test_cc_eml(cc_eml: bytes): | ||
FilePayload(file=cc_eml) | ||
|
||
|
||
def test_invalid_eml_file(): | ||
with pytest.raises(ValueError): | ||
FilePayload(file=b"") |