Files
tyapi-server/internal/shared/pdf/resources_path.go

107 lines
3.2 KiB
Go
Raw Normal View History

2025-12-04 12:56:39 +08:00
package pdf
import (
"os"
"path/filepath"
2025-12-04 13:20:03 +08:00
"go.uber.org/zap"
2025-12-04 12:56:39 +08:00
)
2025-12-04 13:20:03 +08:00
var globalLogger *zap.Logger
// SetGlobalLogger 设置全局logger用于资源路径查找
func SetGlobalLogger(logger *zap.Logger) {
globalLogger = logger
}
2025-12-04 12:56:39 +08:00
// GetResourcesPDFDir 获取resources/pdf目录路径
2025-12-04 13:09:59 +08:00
// resources目录和可执行文件同级例如
// /app/tyapi-server (可执行文件)
// /app/resources/pdf (资源文件)
2025-12-04 12:56:39 +08:00
func GetResourcesPDFDir() string {
2025-12-04 13:09:59 +08:00
// 从可执行文件所在目录查找resources和可执行文件同级
2025-12-04 12:56:39 +08:00
if execPath, err := os.Executable(); err == nil {
execDir := filepath.Dir(execPath)
// 处理符号链接
if realPath, err := filepath.EvalSymlinks(execPath); err == nil {
execDir = filepath.Dir(realPath)
}
2025-12-04 13:09:59 +08:00
// resources目录和可执行文件同级
2025-12-04 12:56:39 +08:00
resourcesPath := filepath.Join(execDir, "resources", "pdf")
2025-12-04 13:20:03 +08:00
absPath, err := filepath.Abs(resourcesPath)
if err == nil {
if globalLogger != nil {
globalLogger.Debug("尝试从可执行文件目录查找资源",
zap.String("exec_path", execPath),
zap.String("exec_dir", execDir),
zap.String("resources_path", absPath),
)
}
2025-12-04 13:09:59 +08:00
if _, err := os.Stat(absPath); err == nil {
2025-12-04 13:20:03 +08:00
if globalLogger != nil {
globalLogger.Info("找到resources/pdf目录", zap.String("path", absPath))
}
2025-12-04 13:09:59 +08:00
return absPath
2025-12-04 13:20:03 +08:00
} else if globalLogger != nil {
globalLogger.Debug("资源目录不存在", zap.String("path", absPath), zap.Error(err))
2025-12-04 13:09:59 +08:00
}
2025-12-04 12:56:39 +08:00
}
}
2025-12-04 13:09:59 +08:00
// 后备方案:从工作目录查找(开发环境)
if workDir, err := os.Getwd(); err == nil {
resourcesPath := filepath.Join(workDir, "resources", "pdf")
2025-12-04 13:20:03 +08:00
absPath, err := filepath.Abs(resourcesPath)
if err == nil {
if globalLogger != nil {
globalLogger.Debug("尝试从工作目录查找资源",
zap.String("work_dir", workDir),
zap.String("resources_path", absPath),
)
}
2025-12-04 13:09:59 +08:00
if _, err := os.Stat(absPath); err == nil {
2025-12-04 13:20:03 +08:00
if globalLogger != nil {
globalLogger.Info("找到resources/pdf目录", zap.String("path", absPath))
}
2025-12-04 13:09:59 +08:00
return absPath
}
}
2025-12-04 13:20:03 +08:00
2025-12-04 13:09:59 +08:00
// 开发环境可能在工作目录的子目录
resourcesPath = filepath.Join(workDir, "tyapi-server-gin", "resources", "pdf")
2025-12-04 13:20:03 +08:00
absPath, err = filepath.Abs(resourcesPath)
if err == nil {
if globalLogger != nil {
globalLogger.Debug("尝试从工作目录子目录查找资源",
zap.String("resources_path", absPath),
)
}
2025-12-04 13:09:59 +08:00
if _, err := os.Stat(absPath); err == nil {
2025-12-04 13:20:03 +08:00
if globalLogger != nil {
globalLogger.Info("找到resources/pdf目录", zap.String("path", absPath))
}
2025-12-04 13:09:59 +08:00
return absPath
}
}
}
2025-12-04 13:20:03 +08:00
// 最后的后备:返回绝对路径(即使目录不存在)
2025-12-04 13:09:59 +08:00
if workDir, err := os.Getwd(); err == nil {
resourcesPath := filepath.Join(workDir, "resources", "pdf")
if absPath, err := filepath.Abs(resourcesPath); err == nil {
2025-12-04 13:20:03 +08:00
if globalLogger != nil {
globalLogger.Warn("未找到resources/pdf目录返回后备路径", zap.String("path", absPath))
}
2025-12-04 13:09:59 +08:00
return absPath
}
}
2025-12-04 13:20:03 +08:00
// 最后的最后:返回相对路径(不推荐)
if globalLogger != nil {
globalLogger.Error("无法确定resources/pdf目录路径")
}
2025-12-04 12:56:39 +08:00
return filepath.Join("resources", "pdf")
}