File size: 2,354 Bytes
338036b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .BaseController import BaseController
from fastapi import  UploadFile
from models import ResponseSignal
from .ProjectController import ProjectController
import re
import os
class DataController(BaseController):
    def __init__(self):
        super().__init__()
        self.size_scale = 1048576 # convert MB to Bytes

    def validate_uploaded_file(self, file: UploadFile):

        if file.content_type not in self.app_settings.FILE_ALLOWED_TYPES:
            return False, ResponseSignal.FILE_TYPE_NOT_SUPPORTED
        
        if file.size > self.app_settings.FILE_MAX_SIZE * self.size_scale:
            return False, ResponseSignal.FILE_SIZE_EXCEEDED
        
        return True ,ResponseSignal.FILE_UPLOADED_SUCCESS
    
    def generate_unique_filepath(self, orig_file_name: str, project_id: str):
        # Generate a random string to guarantee the filename is unique
        random_key = self.generate_random_string()

        # Get the directory path where this specific project's files will be stored
        project_path = ProjectController().get_project_path(project_id=project_id)

        # Clean the original filename (remove spaces, special characters, etc.)
        cleaned_file_name = self.get_clean_file_name(
            orig_file_name=orig_file_name
        )

        # Join the project directory path with the generated unique filename
        # Final filename format: <randomString>_<cleanedFileName>
        new_file_path = os.path.join(
            project_path,
            random_key + "_" + cleaned_file_name
        )

        while os.path.exists(new_file_path):
                    random_key = self.generate_random_string()
                    new_file_path = os.path.join(
                        project_path,
                        random_key + "_" + cleaned_file_name
                    )

        return new_file_path, random_key + "_" + cleaned_file_name


    def get_clean_file_name(self, orig_file_name: str):
        # remove any special characters, except underscore and .
        cleaned_file_name = re.sub(r'[^\w.]', '', orig_file_name.strip())

        # replace spaces with underscore
        cleaned_file_name = cleaned_file_name.replace(" ", "_")

        # return the cleaned version of the filename
        return cleaned_file_name