73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"math/rand"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// FormatFileSize
|
|
/*
|
|
@Description: 转换文件单位
|
|
@param fileSize 文件大小
|
|
@return size 字符串
|
|
*/
|
|
func FormatFileSize(fileSize float64) (size string) {
|
|
units := []string{"B", "KB", "MB", "GB", "TB"}
|
|
var unitIndex int
|
|
for fileSize >= 1024 && unitIndex < len(units)-1 {
|
|
fileSize /= 1024
|
|
unitIndex++
|
|
}
|
|
return fmt.Sprintf("%.2f %s", fileSize, units[unitIndex])
|
|
}
|
|
|
|
func FillData(num int) []string {
|
|
return make([]string, num)
|
|
}
|
|
|
|
func MakeDir(filePath string) {
|
|
_, err := os.Stat(filePath)
|
|
if os.IsNotExist(err) {
|
|
err = os.MkdirAll(filePath, os.ModePerm)
|
|
if err != nil {
|
|
log.Println("创建文件夹失败:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// DecodeToUnicode unicode中文解码
|
|
func DecodeToUnicode(name string) (string, error) {
|
|
// 使用 strconv.Unquote 解码
|
|
decodedStr, err := strconv.Unquote(`"` + name + `"`)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return decodedStr, nil
|
|
}
|
|
|
|
func MakeSavePath(s string) string {
|
|
//wd, _ := os.Getwd()wd,
|
|
folderPath := filepath.Join("files", time.Now().Format("2006-01"), s)
|
|
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
|
|
_ = os.MkdirAll(folderPath, os.ModePerm)
|
|
}
|
|
return folderPath
|
|
}
|
|
|
|
func GenerateCaptcha(num int) (randStr string) {
|
|
//chars := "ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz0123456789"
|
|
chars := "0123456789"
|
|
charsLen := len(chars)
|
|
for i := 0; i < num; i++ {
|
|
randIndex := rand.Intn(charsLen)
|
|
randStr += chars[randIndex : randIndex+1]
|
|
}
|
|
return randStr
|
|
}
|