58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package payment
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
const legacyCertPrefix = "certs/payment/"
|
|
|
|
func CertBaseDir() string {
|
|
dir, _ := beego.AppConfig.String("payment_cert_dir")
|
|
dir = strings.TrimSpace(dir)
|
|
if dir == "" {
|
|
dir = filepath.Join("certs", "payment")
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func CertDir(channel string) string {
|
|
return filepath.Join(CertBaseDir(), filepath.Base(channel))
|
|
}
|
|
|
|
func ResolveCertPath(p string) string {
|
|
p = strings.TrimSpace(p)
|
|
if p == "" {
|
|
return ""
|
|
}
|
|
if filepath.IsAbs(p) {
|
|
return p
|
|
}
|
|
if certPathExists(p) {
|
|
return p
|
|
}
|
|
if cand := filepath.Join(CertBaseDir(), certRelative(p)); certPathExists(cand) {
|
|
return cand
|
|
}
|
|
if cand := filepath.Join(CertBaseDir(), filepath.Base(p)); certPathExists(cand) {
|
|
return cand
|
|
}
|
|
return p
|
|
}
|
|
|
|
func certRelative(p string) string {
|
|
norm := filepath.ToSlash(filepath.Clean(p))
|
|
if i := strings.Index(norm, legacyCertPrefix); i >= 0 {
|
|
norm = norm[i+len(legacyCertPrefix):]
|
|
}
|
|
return filepath.FromSlash(norm)
|
|
}
|
|
|
|
func certPathExists(path string) bool {
|
|
info, err := os.Stat(path)
|
|
return err == nil && !info.IsDir()
|
|
}
|