Skip to content

Commit 44f2d6f

Browse files
authored
Move 'get_project_base_directory' to common directory (#10940)
### What problem does this PR solve? As title ### Type of change - [x] Refactoring --------- Signed-off-by: Jin Hai <[email protected]>
1 parent 57a83ec commit 44f2d6f

24 files changed

+186
-51
lines changed

api/apps/document_app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
server_error_response,
4343
validate_request,
4444
)
45-
from api.utils.file_utils import filename_type, get_project_base_directory, thumbnail
45+
from api.utils.file_utils import filename_type, thumbnail
46+
from common.file_utils import get_project_base_directory
4647
from api.utils.web_utils import CONTENT_TYPE_MAP, html2pdf, is_valid_url
4748
from deepdoc.parser.html_parser import RAGFlowHtmlParser
4849
from rag.nlp import search, rag_tokenizer

api/db/init_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from api.db.services.llm_service import LLMService, LLMBundle, get_init_tenant_llm
3131
from api.db.services.user_service import TenantService, UserTenantService
3232
from api import settings
33-
from api.utils.file_utils import get_project_base_directory
33+
from common.file_utils import get_project_base_directory
3434
from api.common.base64 import encode_to_base64
3535

3636

api/ragflow_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from api.apps import app, smtp_mail_server
3737
from api.db.runtime_config import RuntimeConfig
3838
from api.db.services.document_service import DocumentService
39-
from api import utils
39+
from common.file_utils import get_project_base_directory
4040

4141
from api.db.db_models import init_database_tables as init_web_db
4242
from api.db.init_data import init_web_data
@@ -88,7 +88,7 @@ def signal_handler(sig, frame):
8888
f'RAGFlow version: {get_ragflow_version()}'
8989
)
9090
logging.info(
91-
f'project base: {utils.file_utils.get_project_base_directory()}'
91+
f'project base: {get_project_base_directory()}'
9292
)
9393
show_configs()
9494
settings.init_settings()

api/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import rag.utils.opensearch_conn
2626
from api.constants import RAG_FLOW_SERVICE_NAME
2727
from api.utils.configs import decrypt_database_config, get_base_config
28-
from api.utils.file_utils import get_project_base_directory
28+
from common.file_utils import get_project_base_directory
2929
from rag.nlp import search
3030

3131
LLM = None

api/utils/configs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
import importlib
2424

2525
from api.utils import file_utils
26+
from common.file_utils import get_project_base_directory
2627
from filelock import FileLock
2728
from api.utils.common import bytes_to_string, string_to_bytes
2829
from api.constants import SERVICE_CONF
2930

3031

3132
def conf_realpath(conf_name):
3233
conf_path = f"conf/{conf_name}"
33-
return os.path.join(file_utils.get_project_base_directory(), conf_path)
34+
return os.path.join(get_project_base_directory(), conf_path)
3435

3536

3637
def read_config(conf_name=SERVICE_CONF):
@@ -129,8 +130,7 @@ def decrypt_database_config(
129130
def update_config(key, value, conf_name=SERVICE_CONF):
130131
conf_path = conf_realpath(conf_name=conf_name)
131132
if not os.path.isabs(conf_path):
132-
conf_path = os.path.join(
133-
file_utils.get_project_base_directory(), conf_path)
133+
conf_path = os.path.join(get_project_base_directory(), conf_path)
134134

135135
with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
136136
config = file_utils.load_yaml_conf(conf_path=conf_path) or {}

api/utils/crypt.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import sys
2020
from Cryptodome.PublicKey import RSA
2121
from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
22-
from api.utils import file_utils
22+
from common.file_utils import get_project_base_directory
2323

2424

2525
def crypt(line):
2626
"""
2727
decrypt(crypt(input_string)) == base64(input_string), which frontend and admin_client use.
2828
"""
29-
file_path = os.path.join(file_utils.get_project_base_directory(), "conf", "public.pem")
29+
file_path = os.path.join(get_project_base_directory(), "conf", "public.pem")
3030
rsa_key = RSA.importKey(open(file_path).read(), "Welcome")
3131
cipher = Cipher_pkcs1_v1_5.new(rsa_key)
3232
password_base64 = base64.b64encode(line.encode('utf-8')).decode("utf-8")
@@ -35,7 +35,7 @@ def crypt(line):
3535

3636

3737
def decrypt(line):
38-
file_path = os.path.join(file_utils.get_project_base_directory(), "conf", "private.pem")
38+
file_path = os.path.join(get_project_base_directory(), "conf", "private.pem")
3939
rsa_key = RSA.importKey(open(file_path).read(), "Welcome")
4040
cipher = Cipher_pkcs1_v1_5.new(rsa_key)
4141
return cipher.decrypt(base64.b64decode(line), "Fail to decrypt password!").decode('utf-8')
@@ -50,7 +50,7 @@ def decrypt2(crypt_text):
5050
hex_fixed = '00' + decode_data.hex()
5151
decode_data = b16decode(hex_fixed.upper())
5252

53-
file_path = os.path.join(file_utils.get_project_base_directory(), "conf", "private.pem")
53+
file_path = os.path.join(get_project_base_directory(), "conf", "private.pem")
5454
pem = open(file_path).read()
5555
rsa_key = RSA.importKey(pem, "Welcome")
5656
cipher = Cipher_PKCS1_v1_5.new(rsa_key)

api/utils/file_utils.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
# Local imports
4444
from api.constants import IMG_BASE64_PREFIX
4545
from api.db import FileType
46+
from common.file_utils import get_project_base_directory
4647

4748
PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
4849

@@ -51,21 +52,6 @@
5152
sys.modules[LOCK_KEY_pdfplumber] = threading.Lock()
5253

5354

54-
def get_project_base_directory(*args):
55-
global PROJECT_BASE
56-
if PROJECT_BASE is None:
57-
PROJECT_BASE = os.path.abspath(
58-
os.path.join(
59-
os.path.dirname(os.path.realpath(__file__)),
60-
os.pardir,
61-
os.pardir,
62-
)
63-
)
64-
65-
if args:
66-
return os.path.join(PROJECT_BASE, *args)
67-
return PROJECT_BASE
68-
6955
@cached(cache=LRUCache(maxsize=10))
7056
def load_json_conf(conf_path):
7157
if os.path.isabs(conf_path):

api/utils/log_utils.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,10 @@
1717
import os.path
1818
import logging
1919
from logging.handlers import RotatingFileHandler
20+
from common.file_utils import get_project_base_directory
2021

2122
initialized_root_logger = False
2223

23-
def get_project_base_directory():
24-
PROJECT_BASE = os.path.abspath(
25-
os.path.join(
26-
os.path.dirname(os.path.realpath(__file__)),
27-
os.pardir,
28-
os.pardir,
29-
)
30-
)
31-
return PROJECT_BASE
32-
3324
def init_root_logger(logfile_basename: str, log_format: str = "%(asctime)-15s %(levelname)-8s %(process)d %(message)s"):
3425
global initialized_root_logger
3526
if initialized_root_logger:

common/file_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import os
18+
19+
PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
20+
21+
def get_project_base_directory(*args):
22+
global PROJECT_BASE
23+
if PROJECT_BASE is None:
24+
PROJECT_BASE = os.path.abspath(
25+
os.path.join(
26+
os.path.dirname(os.path.realpath(__file__)),
27+
os.pardir,
28+
)
29+
)
30+
31+
if args:
32+
return os.path.join(PROJECT_BASE, *args)
33+
return PROJECT_BASE

deepdoc/parser/pdf_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from PIL import Image
3535
from pypdf import PdfReader as pdf2_read
3636

37-
from api.utils.file_utils import get_project_base_directory
37+
from common.file_utils import get_project_base_directory
3838
from api.utils.common import pip_install_torch
3939
from deepdoc.vision import OCR, AscendLayoutRecognizer, LayoutRecognizer, Recognizer, TableStructureRecognizer
4040
from rag.app.picture import vision_llm_chunk as picture_vision_llm_chunk

0 commit comments

Comments
 (0)