/
githubmirror
/
gogs
Обзор
Документация
Войти
/
githubmirror
/
gogs
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
latest-commit-build
internal/tool/file.go
53 строки
1 KB
ᴊᴏᴇ ᴄʜᴇɴ
chore: remove all MIT license file headers (#8083)
09 янв 2026, 03:32
Не верифицирован
09 янв 2026, 03:32
59e9fa1
Код
Авторство
О чём код?
package tool import ( "fmt" "math" "net/http" "strings" ) // IsTextFile returns true if file content format is plain text or empty. func IsTextFile(data []byte) bool { if len(data) == 0 { return true } return strings.Contains(http.DetectContentType(data), "text/") } func IsImageFile(data []byte) bool { return strings.Contains(http.DetectContentType(data), "image/") } func IsPDFFile(data []byte) bool { return strings.Contains(http.DetectContentType(data), "application/pdf") } func IsVideoFile(data []byte) bool { return strings.Contains(http.DetectContentType(data), "video/") } func logn(n, b float64) float64 { return math.Log(n) / math.Log(b) } func humanateBytes(s uint64, base float64, sizes []string) string { if s < 10 { return fmt.Sprintf("%d B", s) } e := math.Floor(logn(float64(s), base)) suffix := sizes[int(e)] val := float64(s) / math.Pow(base, math.Floor(e)) f := "%.0f" if val < 10 { f = "%.1f" } return fmt.Sprintf(f+" %s", val, suffix) } // FileSize calculates the file size and generate user-friendly string. func FileSize(s int64) string { sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} return humanateBytes(uint64(s), 1024, sizes) }