55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func GetOutputPath(inputPath, suffix string) string {
|
|
ext := filepath.Ext(inputPath)
|
|
name := strings.TrimSuffix(inputPath, ext)
|
|
return name + suffix + ext
|
|
}
|
|
|
|
func ChangeExtension(filePath, newExt string) string {
|
|
ext := filepath.Ext(filePath)
|
|
name := strings.TrimSuffix(filePath, ext)
|
|
return name + newExt
|
|
}
|
|
|
|
func GetBaseName(filePath string) string {
|
|
name := filepath.Base(filePath)
|
|
ext := filepath.Ext(name)
|
|
return strings.TrimSuffix(name, ext)
|
|
}
|
|
|
|
func ToJSON(v interface{}) ([]byte, error) {
|
|
return json.MarshalIndent(v, "", " ")
|
|
}
|
|
|
|
func ParseJSON(data []byte, v interface{}) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
// formatSize 格式化文件大小为可读字符串
|
|
func formatSize(bytes int64) string {
|
|
const (
|
|
KB = 1024
|
|
MB = KB * 1024
|
|
GB = MB * 1024
|
|
)
|
|
|
|
switch {
|
|
case bytes >= GB:
|
|
return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB))
|
|
case bytes >= MB:
|
|
return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB))
|
|
case bytes >= KB:
|
|
return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB))
|
|
default:
|
|
return fmt.Sprintf("%d B", bytes)
|
|
}
|
|
}
|