Files
tyapi-server/internal/shared/pdf/resources_path.go
2025-12-04 13:09:59 +08:00

58 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package pdf
import (
"os"
"path/filepath"
)
// GetResourcesPDFDir 获取resources/pdf目录路径
// resources目录和可执行文件同级例如
// /app/tyapi-server (可执行文件)
// /app/resources/pdf (资源文件)
func GetResourcesPDFDir() string {
// 从可执行文件所在目录查找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目录和可执行文件同级
resourcesPath := filepath.Join(execDir, "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 {
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")
}