fix pdf path

This commit is contained in:
2025-12-04 13:09:59 +08:00
parent f12c3fb8ad
commit 619deeb456

View File

@@ -6,26 +6,11 @@ import (
)
// GetResourcesPDFDir 获取resources/pdf目录路径
// 支持开发和生产环境
// - 开发环境:从工作目录或可执行文件目录查找
// - 生产环境从可执行文件目录查找Docker容器中工作目录为/app
// resources目录和可执行文件同级例如
// /app/tyapi-server (可执行文件)
// /app/resources/pdf (资源文件)
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
// 从可执行文件所在目录查找resources和可执行文件同级
if execPath, err := os.Executable(); err == nil {
execDir := filepath.Dir(execPath)
// 处理符号链接
@@ -33,21 +18,40 @@ func GetResourcesPDFDir() string {
execDir = filepath.Dir(realPath)
}
// 检查可执行文件同目录下的resources/pdf
// resources目录和可执行文件同级
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
if absPath, err := filepath.Abs(resourcesPath); err == nil {
if _, err := os.Stat(absPath); err == nil {
return absPath
}
}
}
// 优先级3: 返回相对路径作为后备(相对于当前工作目录
// 后备方案:从工作目录查找(开发环境
if workDir, err := os.Getwd(); err == nil {
resourcesPath := filepath.Join(workDir, "resources", "pdf")
if absPath, err := filepath.Abs(resourcesPath); err == nil {
if _, err := os.Stat(absPath); err == nil {
return absPath
}
}
// 开发环境可能在工作目录的子目录
resourcesPath = filepath.Join(workDir, "tyapi-server-gin", "resources", "pdf")
if absPath, err := filepath.Abs(resourcesPath); err == nil {
if _, err := os.Stat(absPath); err == nil {
return absPath
}
}
}
// 最后的后备:返回相对路径
if workDir, err := os.Getwd(); err == nil {
resourcesPath := filepath.Join(workDir, "resources", "pdf")
if absPath, err := filepath.Abs(resourcesPath); err == nil {
return absPath
}
}
return filepath.Join("resources", "pdf")
}