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

54 lines
1.8 KiB
Go
Raw Normal View History

2025-12-04 12:56:39 +08:00
package pdf
import (
"os"
"path/filepath"
)
// GetResourcesPDFDir 获取resources/pdf目录路径
// 支持开发和生产环境:
// - 开发环境:从工作目录或可执行文件目录查找
// - 生产环境从可执行文件目录查找Docker容器中工作目录为/app
func GetResourcesPDFDir() string {
// 优先级1: 从工作目录查找(适用于开发环境和生产环境)
if workDir, err := os.Getwd(); err == nil {
// 检查当前工作目录下的resources/pdf
resourcesPath := filepath.Join(workDir, "resources", "pdf")
if _, err := os.Stat(resourcesPath); err == nil {
return resourcesPath
}
// 检查tyapi-server-gin子目录开发环境
resourcesPath = filepath.Join(workDir, "tyapi-server-gin", "resources", "pdf")
if _, err := os.Stat(resourcesPath); err == nil {
return resourcesPath
}
}
// 优先级2: 从可执行文件所在目录查找适用于生产环境Docker容器
// 在生产环境中,可执行文件在/app/tyapi-server资源在/app/resources
if execPath, err := os.Executable(); err == nil {
execDir := filepath.Dir(execPath)
// 处理符号链接
if realPath, err := filepath.EvalSymlinks(execPath); err == nil {
execDir = filepath.Dir(realPath)
}
// 检查可执行文件同目录下的resources/pdf
resourcesPath := filepath.Join(execDir, "resources", "pdf")
if _, err := os.Stat(resourcesPath); err == nil {
return resourcesPath
}
// 检查可执行文件父目录下的resources/pdf
// 适用于生产环境:/app/tyapi-server -> /app/resources
resourcesPath = filepath.Join(filepath.Dir(execDir), "resources", "pdf")
if _, err := os.Stat(resourcesPath); err == nil {
return resourcesPath
}
}
// 优先级3: 返回相对路径作为后备(相对于当前工作目录)
return filepath.Join("resources", "pdf")
}