fix(files): fall back to global file service when tenant storage config is unavailable
This commit is contained in:
@@ -35,6 +35,7 @@ type RouterParams struct {
|
||||
dig.In
|
||||
|
||||
Config *config.Config
|
||||
FileService interfaces.FileService
|
||||
UserService interfaces.UserService
|
||||
KBService interfaces.KnowledgeBaseService
|
||||
KnowledgeService interfaces.KnowledgeService
|
||||
@@ -120,7 +121,7 @@ func NewRouter(params RouterParams) *gin.Engine {
|
||||
r.Use(middleware.Auth(params.TenantService, params.UserService, params.Config))
|
||||
|
||||
// 文件服务:统一代理本地/MinIO/COS/TOS存储后端(需要认证)
|
||||
serveFiles(r)
|
||||
serveFiles(r, params.FileService)
|
||||
|
||||
// Presigned file access: no auth required, signature-verified.
|
||||
servePresignedFiles(r, params.TenantService)
|
||||
@@ -741,7 +742,11 @@ func serveFrontendStatic(r *gin.Engine) {
|
||||
//
|
||||
// Route:
|
||||
// - /files?file_path=<provider://...>
|
||||
func serveFiles(r *gin.Engine) {
|
||||
type getRouteRegistrar interface {
|
||||
GET(string, ...gin.HandlerFunc) gin.IRoutes
|
||||
}
|
||||
|
||||
func serveFiles(r getRouteRegistrar, globalFileService interfaces.FileService) {
|
||||
baseDir := os.Getenv("LOCAL_STORAGE_BASE_DIR")
|
||||
if baseDir == "" {
|
||||
baseDir = "/data/files"
|
||||
@@ -774,11 +779,31 @@ func serveFiles(r *gin.Engine) {
|
||||
return
|
||||
}
|
||||
|
||||
fileSvc, resolvedProvider, err := filesvc.NewFileServiceFromStorageConfig(provider, tenant.StorageEngineConfig, absDir)
|
||||
var (
|
||||
fileSvc interfaces.FileService
|
||||
resolvedProvider string
|
||||
err error
|
||||
)
|
||||
|
||||
if tenant.StorageEngineConfig != nil {
|
||||
fileSvc, resolvedProvider, err = filesvc.NewFileServiceFromStorageConfig(provider, tenant.StorageEngineConfig, absDir)
|
||||
} else {
|
||||
err = http.ErrMissingFile
|
||||
}
|
||||
if err != nil {
|
||||
logger.Warnf(context.Background(), "[Router] /files resolve file service failed: tenant_id=%d provider=%s err=%v", tenant.ID, provider, err)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
globalStorageType := strings.ToLower(strings.TrimSpace(os.Getenv("STORAGE_TYPE")))
|
||||
if globalStorageType == "" {
|
||||
globalStorageType = "local"
|
||||
}
|
||||
if provider == globalStorageType && globalFileService != nil {
|
||||
logger.Warnf(context.Background(), "[Router] /files tenant storage config missing or invalid, fallback to global file service: tenant_id=%d provider=%s err=%v", tenant.ID, provider, err)
|
||||
fileSvc = globalFileService
|
||||
resolvedProvider = globalStorageType
|
||||
} else {
|
||||
logger.Warnf(context.Background(), "[Router] /files resolve file service failed without fallback: tenant_id=%d provider=%s global_storage_type=%s err=%v", tenant.ID, provider, globalStorageType, err)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
reader, err := fileSvc.GetFile(c.Request.Context(), filePath)
|
||||
|
||||
Reference in New Issue
Block a user