From 62b7c655c511ce458e0ecde650cc69a949b00d54 Mon Sep 17 00:00:00 2001 From: Jin Hai Date: Thu, 25 Sep 2025 23:37:50 +0800 Subject: [PATCH] Refactor: migrate the function to specific file (#10201) ### What problem does this PR solve? Move base64 related function to api/common/base64.py ### Type of change - [x] Refactoring --------- Signed-off-by: jinhai Signed-off-by: Jin Hai --- admin/admin_client.py | 13 ++++++------- admin/config.py | 2 +- api/common/README.md | 2 ++ api/common/base64.py | 21 +++++++++++++++++++++ api/db/init_data.py | 7 +------ 5 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 api/common/README.md create mode 100644 api/common/base64.py diff --git a/admin/admin_client.py b/admin/admin_client.py index 9c63c07ca..e0c29fb7f 100644 --- a/admin/admin_client.py +++ b/admin/admin_client.py @@ -1,11 +1,13 @@ import argparse import base64 + from Cryptodome.PublicKey import RSA from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5 from typing import Dict, List, Any from lark import Lark, Transformer, Tree import requests from requests.auth import HTTPBasicAuth +from api.common.base64 import encode_to_base64 GRAMMAR = r""" start: command @@ -166,11 +168,6 @@ class AdminTransformer(Transformer): return items -def encode_to_base64(input_string): - base64_encoded = base64.b64encode(input_string.encode('utf-8')) - return base64_encoded.decode('utf-8') - - def encrypt(input_string): pub = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB\n-----END PUBLIC KEY-----' pub_key = RSA.importKey(pub) @@ -440,7 +437,8 @@ class AdminCLI: password: str = password_tree.children[0].strip("'\"") print(f"Alter user: {username}, password: {password}") url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}/password' - response = requests.put(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password), json={'new_password': encrypt(password)}) + response = requests.put(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password), + json={'new_password': encrypt(password)}) res_json = response.json() if response.status_code == 200: print(res_json["message"]) @@ -474,7 +472,8 @@ class AdminCLI: if activate_status.lower() in ['on', 'off']: print(f"Alter user {username} activate status, turn {activate_status.lower()}.") url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}/activate' - response = requests.put(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password), json={'activate_status': activate_status}) + response = requests.put(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password), + json={'activate_status': activate_status}) res_json = response.json() if response.status_code == 200: print(res_json["message"]) diff --git a/admin/config.py b/admin/config.py index c09ef6392..570807737 100644 --- a/admin/config.py +++ b/admin/config.py @@ -4,7 +4,7 @@ from enum import Enum from pydantic import BaseModel from typing import Any -from api.utils import read_config +from api.utils.configs import read_config from urllib.parse import urlparse diff --git a/api/common/README.md b/api/common/README.md new file mode 100644 index 000000000..02f630216 --- /dev/null +++ b/api/common/README.md @@ -0,0 +1,2 @@ +The python files in this directory are shared between service. They contain common utilities, models, and functions that can be used across various +services to ensure consistency and reduce code duplication. \ No newline at end of file diff --git a/api/common/base64.py b/api/common/base64.py new file mode 100644 index 000000000..2b37dd281 --- /dev/null +++ b/api/common/base64.py @@ -0,0 +1,21 @@ +# +# Copyright 2025 The InfiniFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import base64 + +def encode_to_base64(input_string): + base64_encoded = base64.b64encode(input_string.encode('utf-8')) + return base64_encoded.decode('utf-8') \ No newline at end of file diff --git a/api/db/init_data.py b/api/db/init_data.py index 166cf3bac..39b87d06f 100644 --- a/api/db/init_data.py +++ b/api/db/init_data.py @@ -14,7 +14,6 @@ # limitations under the License. # import logging -import base64 import json import os import time @@ -32,11 +31,7 @@ from api.db.services.llm_service import LLMService, LLMBundle, get_init_tenant_l from api.db.services.user_service import TenantService, UserTenantService from api import settings from api.utils.file_utils import get_project_base_directory - - -def encode_to_base64(input_string): - base64_encoded = base64.b64encode(input_string.encode('utf-8')) - return base64_encoded.decode('utf-8') +from api.common.base64 import encode_to_base64 def init_superuser():