-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (51 loc) · 2.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from fastapi import FastAPI, File, UploadFile
import uvicorn
import requests
import logging
VERISYS_API_URL = "https://eu1.api.av.ionxsolutions.com/v1/malware/scan/file"
API_KEY = "your_api_key"
logger = logging.getLogger('uvicorn.error')
logger.setLevel(logging.DEBUG)
app = FastAPI()
class ScanResult:
def __init__(self, filename="", status="", content_type="", signals=None, metadata=None):
self.filename = filename
self.status = status
self.content_type = content_type
self.signals = signals if signals is not None else []
self.metadata = metadata if metadata is not None else []
def __repr__(self):
return f"ScanResult(filename={self.filename},status={self.status}, content_type={self.content_type}, signals={self.signals}, metadata={self.metadata})"
def scan_file(file_content, filename):
files = {'file': (filename, file_content)}
headers = {'X-API-Key': API_KEY, 'Accept': '*/*'}
response = requests.post(VERISYS_API_URL, headers=headers, files=files)
# Was the scan successful?
if response.status_code == 201:
result = response.json()
# Print the full scan result to the console
logger.debug(f"Scan result: {result}")
scan_result = ScanResult(
filename=filename,
status=result["status"],
content_type=result["content_type"],
signals=result["signals"],
metadata=result["metadata"]
)
return scan_result
else:
return ScanResult(status="error", metadata=[{"message": "Failed to scan file"}])
@app.post("/upload/")
async def upload_file(file: UploadFile = File(...)):
content = await file.read()
# Print information about the file to the console
logger.debug(f"File Name: {file.filename}")
logger.debug(f"File Size: {len(content)}")
logger.debug(f"File MIME Type: {file.content_type}")
# Scan file with Verisys API
scan_result = scan_file(content, file.filename)
# In real-life, you'd now use the scan result to determine what to do
# next - but here, we'll just return the scan results to the caller
return scan_result
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)