File size: 1,062 Bytes
6e94f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import re
import json
import logging
from fastapi import FastAPI, HTTPException, Depends, Security
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

security = HTTPBearer()

def load_config():
    """
    Load configuration from config.json.
    """
    with open('./app/config/config.json', 'r') as file:
        config_data = json.load(file)
    print(config_data)
    logging.info("Configuration loaded successfully.")
    return config_data


def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security)):
    """
    Verify the API key from Bearer token
    """
    config = load_config()
    if credentials.credentials != config.get("VALID_API_KEY"):
        raise HTTPException(
            status_code=401,
            detail="Invalid API Key"
        )
    
    logging.info("API Key verified successfully.")
    return credentials.credentials


def sanitize_filename(file_link: str) -> str:
    """
    Sanitize file link to create a safe filename.
    """
    return re.sub(r'[^a-zA-Z0-9_-]', '_', file_link)