2024-09-19 17:34:12 +08:00
|
|
|
import logging
|
2024-02-09 15:21:33 +08:00
|
|
|
from collections.abc import Generator
|
|
|
|
from typing import Union
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
from flask import Flask
|
2024-04-29 18:22:03 +08:00
|
|
|
|
2024-10-09 14:15:27 +08:00
|
|
|
from configs import dify_config
|
|
|
|
from extensions.storage.base_storage import BaseStorage
|
|
|
|
from extensions.storage.storage_type import StorageType
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Storage:
|
|
|
|
def __init__(self):
|
2024-04-29 18:22:03 +08:00
|
|
|
self.storage_runner = None
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def init_app(self, app: Flask):
|
2024-10-09 14:15:27 +08:00
|
|
|
storage_factory = self.get_storage_factory(dify_config.STORAGE_TYPE)
|
|
|
|
self.storage_runner = storage_factory(app=app)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_storage_factory(storage_type: str) -> type[BaseStorage]:
|
|
|
|
match storage_type:
|
|
|
|
case StorageType.S3:
|
|
|
|
from extensions.storage.aws_s3_storage import AwsS3Storage
|
|
|
|
|
|
|
|
return AwsS3Storage
|
|
|
|
case StorageType.AZURE_BLOB:
|
|
|
|
from extensions.storage.azure_blob_storage import AzureBlobStorage
|
|
|
|
|
|
|
|
return AzureBlobStorage
|
|
|
|
case StorageType.ALIYUN_OSS:
|
|
|
|
from extensions.storage.aliyun_oss_storage import AliyunOssStorage
|
|
|
|
|
|
|
|
return AliyunOssStorage
|
|
|
|
case StorageType.GOOGLE_STORAGE:
|
|
|
|
from extensions.storage.google_cloud_storage import GoogleCloudStorage
|
|
|
|
|
|
|
|
return GoogleCloudStorage
|
|
|
|
case StorageType.TENCENT_COS:
|
|
|
|
from extensions.storage.tencent_cos_storage import TencentCosStorage
|
|
|
|
|
|
|
|
return TencentCosStorage
|
|
|
|
case StorageType.OCI_STORAGE:
|
|
|
|
from extensions.storage.oracle_oci_storage import OracleOCIStorage
|
|
|
|
|
|
|
|
return OracleOCIStorage
|
|
|
|
case StorageType.HUAWEI_OBS:
|
|
|
|
from extensions.storage.huawei_obs_storage import HuaweiObsStorage
|
|
|
|
|
|
|
|
return HuaweiObsStorage
|
|
|
|
case StorageType.BAIDU_OBS:
|
|
|
|
from extensions.storage.baidu_obs_storage import BaiduObsStorage
|
|
|
|
|
|
|
|
return BaiduObsStorage
|
|
|
|
case StorageType.VOLCENGINE_TOS:
|
|
|
|
from extensions.storage.volcengine_tos_storage import VolcengineTosStorage
|
|
|
|
|
|
|
|
return VolcengineTosStorage
|
|
|
|
case StorageType.LOCAL | _:
|
|
|
|
from extensions.storage.local_fs_storage import LocalFsStorage
|
|
|
|
|
|
|
|
return LocalFsStorage
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def save(self, filename, data):
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
self.storage_runner.save(filename, data)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to save file: %s", e)
|
|
|
|
raise e
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-11-13 22:05:46 +08:00
|
|
|
def load(self, filename: str, stream: bool = False) -> Union[bytes, Generator]:
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
if stream:
|
|
|
|
return self.load_stream(filename)
|
|
|
|
else:
|
|
|
|
return self.load_once(filename)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to load file: %s", e)
|
|
|
|
raise e
|
2023-11-13 22:05:46 +08:00
|
|
|
|
|
|
|
def load_once(self, filename: str) -> bytes:
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
return self.storage_runner.load_once(filename)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to load_once file: %s", e)
|
|
|
|
raise e
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-11-13 22:05:46 +08:00
|
|
|
def load_stream(self, filename: str) -> Generator:
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
return self.storage_runner.load_stream(filename)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to load_stream file: %s", e)
|
|
|
|
raise e
|
2023-11-13 22:05:46 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
def download(self, filename, target_filepath):
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
self.storage_runner.download(filename, target_filepath)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to download file: %s", e)
|
|
|
|
raise e
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def exists(self, filename):
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
return self.storage_runner.exists(filename)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to check file exists: %s", e)
|
|
|
|
raise e
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-04-01 20:19:30 +08:00
|
|
|
def delete(self, filename):
|
2024-09-19 17:34:12 +08:00
|
|
|
try:
|
|
|
|
return self.storage_runner.delete(filename)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Failed to delete file: %s", e)
|
|
|
|
raise e
|
2024-04-01 20:19:30 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
storage = Storage()
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app: Flask):
|
|
|
|
storage.init_app(app)
|