mirror of
https://github.com/RhenCloud/Cloud-Index.git
synced 2025-12-06 15:17:40 +08:00
feat: 实现延迟初始化存储实例,优化存储获取逻辑
This commit is contained in:
@@ -9,8 +9,16 @@ from storages.factory import StorageFactory
|
|||||||
|
|
||||||
main_route = Blueprint("main", __name__)
|
main_route = Blueprint("main", __name__)
|
||||||
|
|
||||||
# 初始化存储(使用工厂模式)
|
# 延迟初始化的存储实例
|
||||||
storage = StorageFactory.get_storage()
|
_storage = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_storage():
|
||||||
|
"""获取存储实例(延迟初始化)"""
|
||||||
|
global _storage
|
||||||
|
if _storage is None:
|
||||||
|
_storage = StorageFactory.get_storage()
|
||||||
|
return _storage
|
||||||
|
|
||||||
|
|
||||||
def get_file_url(key: str) -> str:
|
def get_file_url(key: str) -> str:
|
||||||
@@ -20,6 +28,7 @@ def get_file_url(key: str) -> str:
|
|||||||
|
|
||||||
def build_file_entry(obj: Dict[str, Any], prefix: str) -> Dict[str, Any] | None:
|
def build_file_entry(obj: Dict[str, Any], prefix: str) -> Dict[str, Any] | None:
|
||||||
"""根据对象信息构建文件条目。"""
|
"""根据对象信息构建文件条目。"""
|
||||||
|
storage = get_storage()
|
||||||
key = obj.get("Key", "")
|
key = obj.get("Key", "")
|
||||||
if not key:
|
if not key:
|
||||||
return None
|
return None
|
||||||
@@ -98,6 +107,7 @@ def index():
|
|||||||
返回文件和目录列表的 HTML 页面。
|
返回文件和目录列表的 HTML 页面。
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
prefix = request.args.get("prefix", "") or ""
|
prefix = request.args.get("prefix", "") or ""
|
||||||
|
|
||||||
response = storage.list_objects(prefix)
|
response = storage.list_objects(prefix)
|
||||||
@@ -119,6 +129,7 @@ def index():
|
|||||||
def browse(prefix_path):
|
def browse(prefix_path):
|
||||||
"""目录路由。将 URL /a/b 映射为 prefix 'a/b/' 并重用 index 的逻辑。"""
|
"""目录路由。将 URL /a/b 映射为 prefix 'a/b/' 并重用 index 的逻辑。"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
prefix = prefix_path or ""
|
prefix = prefix_path or ""
|
||||||
if prefix and not prefix.endswith("/"):
|
if prefix and not prefix.endswith("/"):
|
||||||
prefix = prefix + "/"
|
prefix = prefix + "/"
|
||||||
@@ -142,6 +153,7 @@ def browse(prefix_path):
|
|||||||
def serve_file(file_path):
|
def serve_file(file_path):
|
||||||
"""重定向到原始存储 URL,节省服务器资源"""
|
"""重定向到原始存储 URL,节省服务器资源"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
# 验证文件存在
|
# 验证文件存在
|
||||||
try:
|
try:
|
||||||
storage.get_object_info(file_path)
|
storage.get_object_info(file_path)
|
||||||
@@ -169,6 +181,7 @@ def serve_file(file_path):
|
|||||||
def download_file(file_path):
|
def download_file(file_path):
|
||||||
"""下载文件,支持所有存储类型"""
|
"""下载文件,支持所有存储类型"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
# 验证文件存在
|
# 验证文件存在
|
||||||
try:
|
try:
|
||||||
storage.get_object_info(file_path)
|
storage.get_object_info(file_path)
|
||||||
@@ -201,6 +214,7 @@ def download_file(file_path):
|
|||||||
@main_route.route("/thumb/<path:file_path>")
|
@main_route.route("/thumb/<path:file_path>")
|
||||||
def thumb(file_path):
|
def thumb(file_path):
|
||||||
"""返回图片的缩略图,使用 Vercel Cache Headers 避免重复从 R2 拉取"""
|
"""返回图片的缩略图,使用 Vercel Cache Headers 避免重复从 R2 拉取"""
|
||||||
|
storage = get_storage()
|
||||||
# 设置更长的缓存控制头以支持浏览器本地缓存
|
# 设置更长的缓存控制头以支持浏览器本地缓存
|
||||||
cache_headers = {
|
cache_headers = {
|
||||||
"Cache-Control": f"public, max-age={Config.THUMB_TTL_SECONDS}",
|
"Cache-Control": f"public, max-age={Config.THUMB_TTL_SECONDS}",
|
||||||
@@ -240,6 +254,7 @@ def thumb(file_path):
|
|||||||
def upload():
|
def upload():
|
||||||
"""上传文件到存储"""
|
"""上传文件到存储"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
# 检查是否有文件
|
# 检查是否有文件
|
||||||
if "file" not in request.files:
|
if "file" not in request.files:
|
||||||
return jsonify({"success": False, "error": "No file provided"}), 400
|
return jsonify({"success": False, "error": "No file provided"}), 400
|
||||||
@@ -286,6 +301,7 @@ def upload():
|
|||||||
def delete(file_path):
|
def delete(file_path):
|
||||||
"""删除存储中的文件"""
|
"""删除存储中的文件"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
# 删除文件
|
# 删除文件
|
||||||
success = storage.delete_file(file_path)
|
success = storage.delete_file(file_path)
|
||||||
|
|
||||||
@@ -302,6 +318,7 @@ def delete(file_path):
|
|||||||
def rename(old_key):
|
def rename(old_key):
|
||||||
"""重命名存储中的文件"""
|
"""重命名存储中的文件"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
new_name = data.get("newName")
|
new_name = data.get("newName")
|
||||||
|
|
||||||
@@ -337,6 +354,7 @@ def rename(old_key):
|
|||||||
def delete_folder_route(prefix):
|
def delete_folder_route(prefix):
|
||||||
"""删除存储中的文件夹"""
|
"""删除存储中的文件夹"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
if not prefix.endswith("/"):
|
if not prefix.endswith("/"):
|
||||||
prefix += "/"
|
prefix += "/"
|
||||||
success = storage.delete_folder(prefix)
|
success = storage.delete_folder(prefix)
|
||||||
@@ -352,6 +370,7 @@ def delete_folder_route(prefix):
|
|||||||
def rename_folder_route(old_prefix):
|
def rename_folder_route(old_prefix):
|
||||||
"""重命名存储中的文件夹"""
|
"""重命名存储中的文件夹"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
new_name = data.get("newName")
|
new_name = data.get("newName")
|
||||||
|
|
||||||
@@ -388,6 +407,7 @@ def rename_folder_route(old_prefix):
|
|||||||
def copy_item():
|
def copy_item():
|
||||||
"""复制文件或文件夹"""
|
"""复制文件或文件夹"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
source = data.get("source")
|
source = data.get("source")
|
||||||
destination = data.get("destination")
|
destination = data.get("destination")
|
||||||
@@ -420,6 +440,7 @@ def copy_item():
|
|||||||
def move_item():
|
def move_item():
|
||||||
"""移动文件或文件夹"""
|
"""移动文件或文件夹"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
source = data.get("source")
|
source = data.get("source")
|
||||||
destination = data.get("destination")
|
destination = data.get("destination")
|
||||||
@@ -452,6 +473,7 @@ def move_item():
|
|||||||
def create_folder_route():
|
def create_folder_route():
|
||||||
"""创建文件夹"""
|
"""创建文件夹"""
|
||||||
try:
|
try:
|
||||||
|
storage = get_storage()
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
path = data.get("path")
|
path = data.get("path")
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "Cloud-Index"
|
name = "Cloud-Index"
|
||||||
version = "0.10.0"
|
version = "0.10.1"
|
||||||
description = "A cloud storage index system based on a number of cloud storage platforms"
|
description = "A cloud storage index system based on a number of cloud storage platforms"
|
||||||
authors = [{ name = "RhenCloud", email = "i@rhen.cloud" }]
|
authors = [{ name = "RhenCloud", email = "i@rhen.cloud" }]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|||||||
Reference in New Issue
Block a user