From ca6326c937fe1d50e53a357e8fba499f453e7f59 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 31 Mar 2017 17:20:05 -0400 Subject: [PATCH] modules/base: move file-related functions to file.go --- modules/base/file.go | 77 ++++++++++++++++++++++++++++++++++++++++++++ modules/base/tool.go | 67 -------------------------------------- 2 files changed, 77 insertions(+), 67 deletions(-) create mode 100644 modules/base/file.go diff --git a/modules/base/file.go b/modules/base/file.go new file mode 100644 index 000000000..1c39e7265 --- /dev/null +++ b/modules/base/file.go @@ -0,0 +1,77 @@ +// Copyright 2017 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package base + +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/") +} + +const ( + Byte = 1 + KByte = Byte * 1024 + MByte = KByte * 1024 + GByte = MByte * 1024 + TByte = GByte * 1024 + PByte = TByte * 1024 + EByte = PByte * 1024 +) + +var bytesSizeTable = map[string]uint64{ + "b": Byte, + "kb": KByte, + "mb": MByte, + "gb": GByte, + "tb": TByte, + "pb": PByte, + "eb": EByte, +} + +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) +} diff --git a/modules/base/tool.go b/modules/base/tool.go index af1313b2e..7186e59d2 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -12,9 +12,7 @@ import ( "encoding/hex" "fmt" "html/template" - "math" "math/big" - "net/http" "strings" "time" "unicode" @@ -346,51 +344,6 @@ func TimeSince(t time.Time, lang string) template.HTML { return template.HTML(fmt.Sprintf(`%s`, t.Format(setting.TimeFormat), timeSince(t, lang))) } -const ( - Byte = 1 - KByte = Byte * 1024 - MByte = KByte * 1024 - GByte = MByte * 1024 - TByte = GByte * 1024 - PByte = TByte * 1024 - EByte = PByte * 1024 -) - -var bytesSizeTable = map[string]uint64{ - "b": Byte, - "kb": KByte, - "mb": MByte, - "gb": GByte, - "tb": TByte, - "pb": PByte, - "eb": EByte, -} - -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) -} - // Subtract deals with subtraction of all types of number. func Subtract(left interface{}, right interface{}) interface{} { var rleft, rright int64 @@ -491,23 +444,3 @@ func Int64sToMap(ints []int64) map[int64]bool { func IsLetter(ch rune) bool { return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch) } - -// 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.Index(http.DetectContentType(data), "text/") != -1 -} - -func IsImageFile(data []byte) bool { - return strings.Index(http.DetectContentType(data), "image/") != -1 -} - -func IsPDFFile(data []byte) bool { - return strings.Index(http.DetectContentType(data), "application/pdf") != -1 -} - -func IsVideoFile(data []byte) bool { - return strings.Index(http.DetectContentType(data), "video/") != -1 -}