liuhaijun e94826ce29 add server
Change-Id: I0760f17f6a01c0121b59fcbfafc666032dbc30af
2024-09-19 09:44:15 +00:00

207 lines
4.4 KiB
Go
Raw Permalink 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 utils
import (
"database/sql/driver"
"fmt"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/utils/file_utils"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/pkg/log"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/pkg/utils"
"github.com/google/uuid"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
type FormatDate struct {
time.Time
}
const (
timeFormat = "2006-01-02 15:04:05"
)
func (t FormatDate) MarshalJSON() ([]byte, error) {
if &t == nil || t.IsZero() {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", t.Format(timeFormat))), nil
}
func (t FormatDate) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
func (t *FormatDate) Scan(v interface{}) error {
if value, ok := v.(time.Time); ok {
*t = FormatDate{value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func (t *FormatDate) String() string {
if t == nil || t.IsZero() {
return ""
}
return fmt.Sprintf("%s", t.Time.Format(timeFormat))
}
func (t *FormatDate) UnmarshalJSON(data []byte) error {
str := string(data)
if str == "null" {
return nil
}
t1, err := time.ParseInLocation(timeFormat, strings.Trim(str, "\""), time.Local)
*t = FormatDate{t1}
return err
}
func StringToBool(s string) bool {
switch s {
case "0", "false":
return false
case "1", "true":
return true
default:
return false
}
}
func BoolToString(b bool) string {
if b {
return "true"
}
return "false"
}
// GetRunPath 获取执行目录作为默认目录
func GetRunPath() string {
currentPath, err := os.Getwd()
if err != nil {
return ""
}
return currentPath
}
// GetCurrentAbPathByExecutable 获取当前执行文件绝对路径
func GetCurrentAbPathByExecutable() (string, error) {
exePath, err := os.Executable()
if err != nil {
return "", err
}
res, _ := filepath.EvalSymlinks(exePath)
return filepath.Dir(res), nil
}
// GetDataPath 获取当前执行文件路径,如果是临时目录则获取运行命令的工作目录
func GetDataPath() (dir string, err error) {
path, err := utils.GetCurrentPath()
if err != nil {
path = utils.GetRunPath()
}
dataPath := filepath.Join(path, "data")
isDir, err := file_utils.IsDirectory(dataPath)
if err != nil || isDir == false {
err = os.MkdirAll(dataPath, 0755)
if err != nil {
return "", err
}
}
return dataPath, nil
}
func GetUserTempPath(userId string) (string, error) {
path, err := GetFixedTempPathName()
if err != nil {
return "", err
}
joinPath := filepath.Join(path, userId)
return joinPath, nil
}
// 获取临时文件存储路径
func GetFixedTempPathName() (string, error) {
tempPath, err := getTempPath()
if err != nil {
return "", err
}
return filepath.Abs(tempPath)
}
// 获取临时文件存储路径
func GetTempPathName() (string, error) {
tempPath, err := getTempPath()
if err != nil {
return "", err
}
// 生成随机的一个文件夹、避免同一个节点分发同一个文件mv 失败
tempPath = filepath.Join(tempPath, uuid.New().String())
return filepath.Abs(tempPath)
}
// 获取临时文件存储路径
func getTempPath() (string, error) {
basePath, err := utils.GetDefaultPath()
if err != nil {
return "", err
}
timestamp := time.Now().Format("2006-01-02")
path := filepath.Join(basePath, timestamp)
if err := os.MkdirAll(path, 0755); err != nil {
return "", err
}
return path, nil
}
// 清理临时文件存储目录:
// 将basePath目录下所有格式为2006-01-02格式文件夹并删除超过 30 天的所有文件夹
func CleanTempPath() {
basePath, err := utils.GetDefaultPath()
if err != nil {
return
}
// 获取basePath目录下所有格式为2006-01-02格式文件夹
// 匹配 YYYY-MM-DD 格式的文件夹
datePattern := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
now := time.Now()
filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 只处理目录
if !info.IsDir() {
return nil
}
// 检查文件夹名是否匹配日期格式
if datePattern.MatchString(info.Name()) {
// 尝试解析文件夹名以确认它是一个有效的日期
if past, err := time.Parse("2006-01-02", info.Name()); err == nil {
diff := now.Sub(past)
diffDays := diff.Hours() / 24
if diffDays >= 30 {
log.Info("删除临时文件夹: %s", path)
os.RemoveAll(path)
}
}
}
return nil
})
}