KevinHuSh
commited on
Commit
·
3f48800
1
Parent(s):
9426215
fix bug of file management (#565)
Browse files### What problem does this PR solve?
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- api/apps/file2document_app.py +1 -1
- api/apps/file_app.py +4 -4
- api/apps/user_app.py +2 -2
- api/db/services/file_service.py +22 -4
api/apps/file2document_app.py
CHANGED
|
@@ -45,7 +45,7 @@ def convert():
|
|
| 45 |
for file_id in file_ids:
|
| 46 |
e, file = FileService.get_by_id(file_id)
|
| 47 |
file_ids_list = [file_id]
|
| 48 |
-
if file.type == FileType.FOLDER:
|
| 49 |
file_ids_list = FileService.get_all_innermost_file_ids(file_id, [])
|
| 50 |
for id in file_ids_list:
|
| 51 |
informs = File2DocumentService.get_by_file_id(id)
|
|
|
|
| 45 |
for file_id in file_ids:
|
| 46 |
e, file = FileService.get_by_id(file_id)
|
| 47 |
file_ids_list = [file_id]
|
| 48 |
+
if file.type == FileType.FOLDER.value:
|
| 49 |
file_ids_list = FileService.get_all_innermost_file_ids(file_id, [])
|
| 50 |
for id in file_ids_list:
|
| 51 |
informs = File2DocumentService.get_by_file_id(id)
|
api/apps/file_app.py
CHANGED
|
@@ -64,7 +64,7 @@ def upload():
|
|
| 64 |
return get_data_error_result(
|
| 65 |
retmsg="Can't find this folder!")
|
| 66 |
MAX_FILE_NUM_PER_USER = int(os.environ.get('MAX_FILE_NUM_PER_USER', 0))
|
| 67 |
-
if MAX_FILE_NUM_PER_USER > 0 and DocumentService.get_doc_count(
|
| 68 |
return get_data_error_result(
|
| 69 |
retmsg="Exceed the maximum file number of a free user!")
|
| 70 |
|
|
@@ -143,9 +143,9 @@ def create():
|
|
| 143 |
retmsg="Duplicated folder name in the same folder.")
|
| 144 |
|
| 145 |
if input_file_type == FileType.FOLDER.value:
|
| 146 |
-
file_type = FileType.FOLDER
|
| 147 |
else:
|
| 148 |
-
file_type = FileType.VIRTUAL
|
| 149 |
|
| 150 |
file = FileService.insert({
|
| 151 |
"id": get_uuid(),
|
|
@@ -251,7 +251,7 @@ def rm():
|
|
| 251 |
if not file.tenant_id:
|
| 252 |
return get_data_error_result(retmsg="Tenant not found!")
|
| 253 |
|
| 254 |
-
if file.type == FileType.FOLDER:
|
| 255 |
file_id_list = FileService.get_all_innermost_file_ids(file_id, [])
|
| 256 |
for inner_file_id in file_id_list:
|
| 257 |
e, file = FileService.get_by_id(inner_file_id)
|
|
|
|
| 64 |
return get_data_error_result(
|
| 65 |
retmsg="Can't find this folder!")
|
| 66 |
MAX_FILE_NUM_PER_USER = int(os.environ.get('MAX_FILE_NUM_PER_USER', 0))
|
| 67 |
+
if MAX_FILE_NUM_PER_USER > 0 and DocumentService.get_doc_count(current_user.id) >= MAX_FILE_NUM_PER_USER:
|
| 68 |
return get_data_error_result(
|
| 69 |
retmsg="Exceed the maximum file number of a free user!")
|
| 70 |
|
|
|
|
| 143 |
retmsg="Duplicated folder name in the same folder.")
|
| 144 |
|
| 145 |
if input_file_type == FileType.FOLDER.value:
|
| 146 |
+
file_type = FileType.FOLDER.value
|
| 147 |
else:
|
| 148 |
+
file_type = FileType.VIRTUAL.value
|
| 149 |
|
| 150 |
file = FileService.insert({
|
| 151 |
"id": get_uuid(),
|
|
|
|
| 251 |
if not file.tenant_id:
|
| 252 |
return get_data_error_result(retmsg="Tenant not found!")
|
| 253 |
|
| 254 |
+
if file.type == FileType.FOLDER.value:
|
| 255 |
file_id_list = FileService.get_all_innermost_file_ids(file_id, [])
|
| 256 |
for inner_file_id in file_id_list:
|
| 257 |
e, file = FileService.get_by_id(inner_file_id)
|
api/apps/user_app.py
CHANGED
|
@@ -24,7 +24,7 @@ from api.db.db_models import TenantLLM
|
|
| 24 |
from api.db.services.llm_service import TenantLLMService, LLMService
|
| 25 |
from api.utils.api_utils import server_error_response, validate_request
|
| 26 |
from api.utils import get_uuid, get_format_time, decrypt, download_img, current_timestamp, datetime_format
|
| 27 |
-
from api.db import UserTenantRole, LLMType
|
| 28 |
from api.settings import RetCode, GITHUB_OAUTH, CHAT_MDL, EMBEDDING_MDL, ASR_MDL, IMAGE2TEXT_MDL, PARSERS, API_KEY, \
|
| 29 |
LLM_FACTORY, LLM_BASE_URL
|
| 30 |
from api.db.services.user_service import UserService, TenantService, UserTenantService
|
|
@@ -229,7 +229,7 @@ def user_register(user_id, user):
|
|
| 229 |
"tenant_id": user_id,
|
| 230 |
"created_by": user_id,
|
| 231 |
"name": "/",
|
| 232 |
-
"type": FileType.FOLDER,
|
| 233 |
"size": 0,
|
| 234 |
"location": "",
|
| 235 |
}
|
|
|
|
| 24 |
from api.db.services.llm_service import TenantLLMService, LLMService
|
| 25 |
from api.utils.api_utils import server_error_response, validate_request
|
| 26 |
from api.utils import get_uuid, get_format_time, decrypt, download_img, current_timestamp, datetime_format
|
| 27 |
+
from api.db import UserTenantRole, LLMType, FileType
|
| 28 |
from api.settings import RetCode, GITHUB_OAUTH, CHAT_MDL, EMBEDDING_MDL, ASR_MDL, IMAGE2TEXT_MDL, PARSERS, API_KEY, \
|
| 29 |
LLM_FACTORY, LLM_BASE_URL
|
| 30 |
from api.db.services.user_service import UserService, TenantService, UserTenantService
|
|
|
|
| 229 |
"tenant_id": user_id,
|
| 230 |
"created_by": user_id,
|
| 231 |
"name": "/",
|
| 232 |
+
"type": FileType.FOLDER.value,
|
| 233 |
"size": 0,
|
| 234 |
"location": "",
|
| 235 |
}
|
api/db/services/file_service.py
CHANGED
|
@@ -120,7 +120,7 @@ class FileService(CommonService):
|
|
| 120 |
"name": name[count],
|
| 121 |
"location": "",
|
| 122 |
"size": 0,
|
| 123 |
-
"type": FileType.FOLDER
|
| 124 |
})
|
| 125 |
return cls.create_folder(file, file.id, name, count + 1)
|
| 126 |
|
|
@@ -138,7 +138,23 @@ class FileService(CommonService):
|
|
| 138 |
def get_root_folder(cls, tenant_id):
|
| 139 |
file = cls.model.select().where(cls.model.tenant_id == tenant_id and
|
| 140 |
cls.model.parent_id == cls.model.id)
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
if not e:
|
| 143 |
raise RuntimeError("Database error (File retrieval)!")
|
| 144 |
return file
|
|
@@ -214,12 +230,14 @@ class FileService(CommonService):
|
|
| 214 |
@DB.connection_context()
|
| 215 |
def get_folder_size(cls, folder_id):
|
| 216 |
size = 0
|
|
|
|
| 217 |
def dfs(parent_id):
|
| 218 |
nonlocal size
|
| 219 |
-
for f in cls.model.select(*[cls.model.id, cls.model.size, cls.model.type]).where(
|
|
|
|
| 220 |
size += f.size
|
| 221 |
if f.type == FileType.FOLDER.value:
|
| 222 |
dfs(f.id)
|
| 223 |
|
| 224 |
dfs(folder_id)
|
| 225 |
-
return size
|
|
|
|
| 120 |
"name": name[count],
|
| 121 |
"location": "",
|
| 122 |
"size": 0,
|
| 123 |
+
"type": FileType.FOLDER.value
|
| 124 |
})
|
| 125 |
return cls.create_folder(file, file.id, name, count + 1)
|
| 126 |
|
|
|
|
| 138 |
def get_root_folder(cls, tenant_id):
|
| 139 |
file = cls.model.select().where(cls.model.tenant_id == tenant_id and
|
| 140 |
cls.model.parent_id == cls.model.id)
|
| 141 |
+
if not file:
|
| 142 |
+
file_id = get_uuid()
|
| 143 |
+
file = {
|
| 144 |
+
"id": file_id,
|
| 145 |
+
"parent_id": file_id,
|
| 146 |
+
"tenant_id": tenant_id,
|
| 147 |
+
"created_by": tenant_id,
|
| 148 |
+
"name": "/",
|
| 149 |
+
"type": FileType.FOLDER.value,
|
| 150 |
+
"size": 0,
|
| 151 |
+
"location": "",
|
| 152 |
+
}
|
| 153 |
+
cls.save(**file)
|
| 154 |
+
else:
|
| 155 |
+
file_id = file[0].id
|
| 156 |
+
|
| 157 |
+
e, file = cls.get_by_id(file_id)
|
| 158 |
if not e:
|
| 159 |
raise RuntimeError("Database error (File retrieval)!")
|
| 160 |
return file
|
|
|
|
| 230 |
@DB.connection_context()
|
| 231 |
def get_folder_size(cls, folder_id):
|
| 232 |
size = 0
|
| 233 |
+
|
| 234 |
def dfs(parent_id):
|
| 235 |
nonlocal size
|
| 236 |
+
for f in cls.model.select(*[cls.model.id, cls.model.size, cls.model.type]).where(
|
| 237 |
+
cls.model.parent_id == parent_id, cls.model.id != parent_id):
|
| 238 |
size += f.size
|
| 239 |
if f.type == FileType.FOLDER.value:
|
| 240 |
dfs(f.id)
|
| 241 |
|
| 242 |
dfs(folder_id)
|
| 243 |
+
return size
|