"tests/tests_master/test_models.py" did not exist on "8f4d1b494d2c50c4dc3d942d111a5340a2c6228d"
files.py 6.46 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
from fastapi import (
    Depends,
    FastAPI,
    HTTPException,
    status,
    Request,
    UploadFile,
    File,
    Form,
)


from datetime import datetime, timedelta
Michael Poluektov's avatar
Michael Poluektov committed
14
from typing import Union, Optional
Timothy J. Baek's avatar
Timothy J. Baek committed
15
from pathlib import Path
Timothy J. Baek's avatar
Timothy J. Baek committed
16
17

from fastapi import APIRouter
Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
from fastapi.responses import StreamingResponse, JSONResponse, FileResponse

Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
22
from pydantic import BaseModel
import json

Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
26
27
28
from apps.webui.models.files import (
    Files,
    FileForm,
    FileModel,
    FileModelResponse,
)
Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31
32
33
34
from utils.utils import get_verified_user, get_admin_user
from constants import ERROR_MESSAGES

from importlib import util
import os
import uuid
Timothy J. Baek's avatar
Timothy J. Baek committed
35
import os, shutil, logging, re
Timothy J. Baek's avatar
Timothy J. Baek committed
36
37


Timothy J. Baek's avatar
Timothy J. Baek committed
38
from config import SRC_LOG_LEVELS, UPLOAD_DIR
Timothy J. Baek's avatar
Timothy J. Baek committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52


log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])


router = APIRouter()

############################
# Upload File
############################


@router.post("/")
53
def upload_file(file: UploadFile = File(...), user=Depends(get_verified_user)):
Timothy J. Baek's avatar
Timothy J. Baek committed
54
55
56
57
58
59
60
    log.info(f"file.content_type: {file.content_type}")
    try:
        unsanitized_filename = file.filename
        filename = os.path.basename(unsanitized_filename)

        # replace filename with uuid
        id = str(uuid.uuid4())
61
        name = filename
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
62
        filename = f"{id}_{filename}"
Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
65
66
67
68
69
70
        file_path = f"{UPLOAD_DIR}/{filename}"

        contents = file.file.read()
        with open(file_path, "wb") as f:
            f.write(contents)
            f.close()

        file = Files.insert_new_file(
Timothy J. Baek's avatar
Timothy J. Baek committed
71
72
73
74
75
76
            user.id,
            FileForm(
                **{
                    "id": id,
                    "filename": filename,
                    "meta": {
77
                        "name": name,
Timothy J. Baek's avatar
Timothy J. Baek committed
78
79
80
81
82
83
                        "content_type": file.content_type,
                        "size": len(contents),
                        "path": file_path,
                    },
                }
            ),
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        )

        if file:
            return file
        else:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ERROR_MESSAGES.DEFAULT("Error uploading file"),
            )

    except Exception as e:
        log.exception(e)
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.DEFAULT(e),
        )


############################
# List Files
############################


Michael Poluektov's avatar
Michael Poluektov committed
107
@router.get("/", response_model=list[FileModel])
108
109
async def list_files(user=Depends(get_verified_user)):
    files = Files.get_files()
Timothy J. Baek's avatar
Timothy J. Baek committed
110
111
112
    return files


Timothy J. Baek's avatar
Timothy J. Baek committed
113
114
115
116
117
118
############################
# Delete All Files
############################


@router.delete("/all")
119
120
async def delete_all_files(user=Depends(get_admin_user)):
    result = Files.delete_all_files()
Timothy J. Baek's avatar
Timothy J. Baek committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

    if result:
        folder = f"{UPLOAD_DIR}"
        try:
            # Check if the directory exists
            if os.path.exists(folder):
                # Iterate over all the files and directories in the specified directory
                for filename in os.listdir(folder):
                    file_path = os.path.join(folder, filename)
                    try:
                        if os.path.isfile(file_path) or os.path.islink(file_path):
                            os.unlink(file_path)  # Remove the file or link
                        elif os.path.isdir(file_path):
                            shutil.rmtree(file_path)  # Remove the directory
                    except Exception as e:
                        print(f"Failed to delete {file_path}. Reason: {e}")
            else:
                print(f"The directory {folder} does not exist")
        except Exception as e:
            print(f"Failed to process the directory {folder}. Reason: {e}")

        return {"message": "All files deleted successfully"}
    else:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.DEFAULT("Error deleting files"),
        )


Timothy J. Baek's avatar
Timothy J. Baek committed
150
151
152
153
154
155
############################
# Get File By Id
############################


@router.get("/{id}", response_model=Optional[FileModel])
156
157
async def get_file_by_id(id: str, user=Depends(get_verified_user)):
    file = Files.get_file_by_id(id)
Timothy J. Baek's avatar
Timothy J. Baek committed
158
159
160
161
162

    if file:
        return file
    else:
        raise HTTPException(
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
163
            status_code=status.HTTP_404_NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
164
165
166
167
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


Timothy J. Baek's avatar
Timothy J. Baek committed
168
169
170
171
172
173
############################
# Get File Content By Id
############################


@router.get("/{id}/content", response_model=Optional[FileModel])
174
175
async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
    file = Files.get_file_by_id(id)
Timothy J. Baek's avatar
Timothy J. Baek committed
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    if file:
        file_path = Path(file.meta["path"])

        # Check if the file already exists in the cache
        if file_path.is_file():
            print(f"file_path: {file_path}")
            return FileResponse(file_path)
        else:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail=ERROR_MESSAGES.NOT_FOUND,
            )
    else:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


@router.get("/{id}/content/{file_name}", response_model=Optional[FileModel])
async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
    file = Files.get_file_by_id(id)

Timothy J. Baek's avatar
Timothy J. Baek committed
200
201
202
203
204
205
206
207
208
    if file:
        file_path = Path(file.meta["path"])

        # Check if the file already exists in the cache
        if file_path.is_file():
            print(f"file_path: {file_path}")
            return FileResponse(file_path)
        else:
            raise HTTPException(
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
209
                status_code=status.HTTP_404_NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
210
211
212
213
                detail=ERROR_MESSAGES.NOT_FOUND,
            )
    else:
        raise HTTPException(
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
214
            status_code=status.HTTP_404_NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
215
216
217
218
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


Timothy J. Baek's avatar
Timothy J. Baek committed
219
220
221
222
223
224
############################
# Delete File By Id
############################


@router.delete("/{id}")
225
226
async def delete_file_by_id(id: str, user=Depends(get_verified_user)):
    file = Files.get_file_by_id(id)
Timothy J. Baek's avatar
Timothy J. Baek committed
227
228

    if file:
229
        result = Files.delete_file_by_id(id)
Timothy J. Baek's avatar
Timothy J. Baek committed
230
231
232
233
234
235
236
237
238
        if result:
            return {"message": "File deleted successfully"}
        else:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ERROR_MESSAGES.DEFAULT("Error deleting file"),
            )
    else:
        raise HTTPException(
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
239
            status_code=status.HTTP_404_NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
240
241
            detail=ERROR_MESSAGES.NOT_FOUND,
        )