tool.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "crypto/md5"
  7. "crypto/rand"
  8. "crypto/sha1"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "fmt"
  12. "html/template"
  13. "math"
  14. "math/big"
  15. "net/http"
  16. "strings"
  17. "time"
  18. "unicode"
  19. "unicode/utf8"
  20. "github.com/Unknwon/com"
  21. "github.com/Unknwon/i18n"
  22. log "gopkg.in/clog.v1"
  23. "github.com/gogits/chardet"
  24. "github.com/gogits/gogs/modules/setting"
  25. )
  26. // EncodeMD5 encodes string to md5 hex value.
  27. func EncodeMD5(str string) string {
  28. m := md5.New()
  29. m.Write([]byte(str))
  30. return hex.EncodeToString(m.Sum(nil))
  31. }
  32. // Encode string to sha1 hex value.
  33. func EncodeSha1(str string) string {
  34. h := sha1.New()
  35. h.Write([]byte(str))
  36. return hex.EncodeToString(h.Sum(nil))
  37. }
  38. func ShortSha(sha1 string) string {
  39. if len(sha1) > 10 {
  40. return sha1[:10]
  41. }
  42. return sha1
  43. }
  44. func DetectEncoding(content []byte) (string, error) {
  45. if utf8.Valid(content) {
  46. log.Trace("Detected encoding: utf-8 (fast)")
  47. return "UTF-8", nil
  48. }
  49. result, err := chardet.NewTextDetector().DetectBest(content)
  50. if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 {
  51. log.Trace("Using default AnsiCharset: %s", setting.Repository.AnsiCharset)
  52. return setting.Repository.AnsiCharset, err
  53. }
  54. log.Trace("Detected encoding: %s", result.Charset)
  55. return result.Charset, err
  56. }
  57. func BasicAuthDecode(encoded string) (string, string, error) {
  58. s, err := base64.StdEncoding.DecodeString(encoded)
  59. if err != nil {
  60. return "", "", err
  61. }
  62. auth := strings.SplitN(string(s), ":", 2)
  63. return auth[0], auth[1], nil
  64. }
  65. func BasicAuthEncode(username, password string) string {
  66. return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  67. }
  68. // GetRandomString generate random string by specify chars.
  69. func GetRandomString(n int) (string, error) {
  70. const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  71. buffer := make([]byte, n)
  72. max := big.NewInt(int64(len(alphanum)))
  73. for i := 0; i < n; i++ {
  74. index, err := randomInt(max)
  75. if err != nil {
  76. return "", err
  77. }
  78. buffer[i] = alphanum[index]
  79. }
  80. return string(buffer), nil
  81. }
  82. func randomInt(max *big.Int) (int, error) {
  83. rand, err := rand.Int(rand.Reader, max)
  84. if err != nil {
  85. return 0, err
  86. }
  87. return int(rand.Int64()), nil
  88. }
  89. // verify time limit code
  90. func VerifyTimeLimitCode(data string, minutes int, code string) bool {
  91. if len(code) <= 18 {
  92. return false
  93. }
  94. // split code
  95. start := code[:12]
  96. lives := code[12:18]
  97. if d, err := com.StrTo(lives).Int(); err == nil {
  98. minutes = d
  99. }
  100. // right active code
  101. retCode := CreateTimeLimitCode(data, minutes, start)
  102. if retCode == code && minutes > 0 {
  103. // check time is expired or not
  104. before, _ := time.ParseInLocation("200601021504", start, time.Local)
  105. now := time.Now()
  106. if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
  107. return true
  108. }
  109. }
  110. return false
  111. }
  112. const TimeLimitCodeLength = 12 + 6 + 40
  113. // create a time limit code
  114. // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
  115. func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
  116. format := "200601021504"
  117. var start, end time.Time
  118. var startStr, endStr string
  119. if startInf == nil {
  120. // Use now time create code
  121. start = time.Now()
  122. startStr = start.Format(format)
  123. } else {
  124. // use start string create code
  125. startStr = startInf.(string)
  126. start, _ = time.ParseInLocation(format, startStr, time.Local)
  127. startStr = start.Format(format)
  128. }
  129. end = start.Add(time.Minute * time.Duration(minutes))
  130. endStr = end.Format(format)
  131. // create sha1 encode string
  132. sh := sha1.New()
  133. sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
  134. encoded := hex.EncodeToString(sh.Sum(nil))
  135. code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
  136. return code
  137. }
  138. // HashEmail hashes email address to MD5 string.
  139. // https://en.gravatar.com/site/implement/hash/
  140. func HashEmail(email string) string {
  141. email = strings.ToLower(strings.TrimSpace(email))
  142. h := md5.New()
  143. h.Write([]byte(email))
  144. return hex.EncodeToString(h.Sum(nil))
  145. }
  146. // AvatarLink returns relative avatar link to the site domain by given email,
  147. // which includes app sub-url as prefix. However, it is possible
  148. // to return full URL if user enables Gravatar-like service.
  149. func AvatarLink(email string) (url string) {
  150. if setting.EnableFederatedAvatar && setting.LibravatarService != nil &&
  151. strings.Contains(email, "@") {
  152. var err error
  153. url, err = setting.LibravatarService.FromEmail(email)
  154. if err != nil {
  155. log.Warn("AvatarLink.LibravatarService.FromEmail [%s]: %v", email, err)
  156. }
  157. }
  158. if len(url) == 0 && !setting.DisableGravatar {
  159. url = setting.GravatarSource + HashEmail(email)
  160. }
  161. if len(url) == 0 {
  162. url = setting.AppSubUrl + "/img/avatar_default.png"
  163. }
  164. return url
  165. }
  166. // Seconds-based time units
  167. const (
  168. Minute = 60
  169. Hour = 60 * Minute
  170. Day = 24 * Hour
  171. Week = 7 * Day
  172. Month = 30 * Day
  173. Year = 12 * Month
  174. )
  175. func computeTimeDiff(diff int64) (int64, string) {
  176. diffStr := ""
  177. switch {
  178. case diff <= 0:
  179. diff = 0
  180. diffStr = "now"
  181. case diff < 2:
  182. diff = 0
  183. diffStr = "1 second"
  184. case diff < 1*Minute:
  185. diffStr = fmt.Sprintf("%d seconds", diff)
  186. diff = 0
  187. case diff < 2*Minute:
  188. diff -= 1 * Minute
  189. diffStr = "1 minute"
  190. case diff < 1*Hour:
  191. diffStr = fmt.Sprintf("%d minutes", diff/Minute)
  192. diff -= diff / Minute * Minute
  193. case diff < 2*Hour:
  194. diff -= 1 * Hour
  195. diffStr = "1 hour"
  196. case diff < 1*Day:
  197. diffStr = fmt.Sprintf("%d hours", diff/Hour)
  198. diff -= diff / Hour * Hour
  199. case diff < 2*Day:
  200. diff -= 1 * Day
  201. diffStr = "1 day"
  202. case diff < 1*Week:
  203. diffStr = fmt.Sprintf("%d days", diff/Day)
  204. diff -= diff / Day * Day
  205. case diff < 2*Week:
  206. diff -= 1 * Week
  207. diffStr = "1 week"
  208. case diff < 1*Month:
  209. diffStr = fmt.Sprintf("%d weeks", diff/Week)
  210. diff -= diff / Week * Week
  211. case diff < 2*Month:
  212. diff -= 1 * Month
  213. diffStr = "1 month"
  214. case diff < 1*Year:
  215. diffStr = fmt.Sprintf("%d months", diff/Month)
  216. diff -= diff / Month * Month
  217. case diff < 2*Year:
  218. diff -= 1 * Year
  219. diffStr = "1 year"
  220. default:
  221. diffStr = fmt.Sprintf("%d years", diff/Year)
  222. diff = 0
  223. }
  224. return diff, diffStr
  225. }
  226. // TimeSincePro calculates the time interval and generate full user-friendly string.
  227. func TimeSincePro(then time.Time) string {
  228. now := time.Now()
  229. diff := now.Unix() - then.Unix()
  230. if then.After(now) {
  231. return "future"
  232. }
  233. var timeStr, diffStr string
  234. for {
  235. if diff == 0 {
  236. break
  237. }
  238. diff, diffStr = computeTimeDiff(diff)
  239. timeStr += ", " + diffStr
  240. }
  241. return strings.TrimPrefix(timeStr, ", ")
  242. }
  243. func timeSince(then time.Time, lang string) string {
  244. now := time.Now()
  245. lbl := i18n.Tr(lang, "tool.ago")
  246. diff := now.Unix() - then.Unix()
  247. if then.After(now) {
  248. lbl = i18n.Tr(lang, "tool.from_now")
  249. diff = then.Unix() - now.Unix()
  250. }
  251. switch {
  252. case diff <= 0:
  253. return i18n.Tr(lang, "tool.now")
  254. case diff <= 2:
  255. return i18n.Tr(lang, "tool.1s", lbl)
  256. case diff < 1*Minute:
  257. return i18n.Tr(lang, "tool.seconds", diff, lbl)
  258. case diff < 2*Minute:
  259. return i18n.Tr(lang, "tool.1m", lbl)
  260. case diff < 1*Hour:
  261. return i18n.Tr(lang, "tool.minutes", diff/Minute, lbl)
  262. case diff < 2*Hour:
  263. return i18n.Tr(lang, "tool.1h", lbl)
  264. case diff < 1*Day:
  265. return i18n.Tr(lang, "tool.hours", diff/Hour, lbl)
  266. case diff < 2*Day:
  267. return i18n.Tr(lang, "tool.1d", lbl)
  268. case diff < 1*Week:
  269. return i18n.Tr(lang, "tool.days", diff/Day, lbl)
  270. case diff < 2*Week:
  271. return i18n.Tr(lang, "tool.1w", lbl)
  272. case diff < 1*Month:
  273. return i18n.Tr(lang, "tool.weeks", diff/Week, lbl)
  274. case diff < 2*Month:
  275. return i18n.Tr(lang, "tool.1mon", lbl)
  276. case diff < 1*Year:
  277. return i18n.Tr(lang, "tool.months", diff/Month, lbl)
  278. case diff < 2*Year:
  279. return i18n.Tr(lang, "tool.1y", lbl)
  280. default:
  281. return i18n.Tr(lang, "tool.years", diff/Year, lbl)
  282. }
  283. }
  284. func RawTimeSince(t time.Time, lang string) string {
  285. return timeSince(t, lang)
  286. }
  287. // TimeSince calculates the time interval and generate user-friendly string.
  288. func TimeSince(t time.Time, lang string) template.HTML {
  289. return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
  290. }
  291. const (
  292. Byte = 1
  293. KByte = Byte * 1024
  294. MByte = KByte * 1024
  295. GByte = MByte * 1024
  296. TByte = GByte * 1024
  297. PByte = TByte * 1024
  298. EByte = PByte * 1024
  299. )
  300. var bytesSizeTable = map[string]uint64{
  301. "b": Byte,
  302. "kb": KByte,
  303. "mb": MByte,
  304. "gb": GByte,
  305. "tb": TByte,
  306. "pb": PByte,
  307. "eb": EByte,
  308. }
  309. func logn(n, b float64) float64 {
  310. return math.Log(n) / math.Log(b)
  311. }
  312. func humanateBytes(s uint64, base float64, sizes []string) string {
  313. if s < 10 {
  314. return fmt.Sprintf("%d B", s)
  315. }
  316. e := math.Floor(logn(float64(s), base))
  317. suffix := sizes[int(e)]
  318. val := float64(s) / math.Pow(base, math.Floor(e))
  319. f := "%.0f"
  320. if val < 10 {
  321. f = "%.1f"
  322. }
  323. return fmt.Sprintf(f+" %s", val, suffix)
  324. }
  325. // FileSize calculates the file size and generate user-friendly string.
  326. func FileSize(s int64) string {
  327. sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
  328. return humanateBytes(uint64(s), 1024, sizes)
  329. }
  330. // Subtract deals with subtraction of all types of number.
  331. func Subtract(left interface{}, right interface{}) interface{} {
  332. var rleft, rright int64
  333. var fleft, fright float64
  334. var isInt bool = true
  335. switch left.(type) {
  336. case int:
  337. rleft = int64(left.(int))
  338. case int8:
  339. rleft = int64(left.(int8))
  340. case int16:
  341. rleft = int64(left.(int16))
  342. case int32:
  343. rleft = int64(left.(int32))
  344. case int64:
  345. rleft = left.(int64)
  346. case float32:
  347. fleft = float64(left.(float32))
  348. isInt = false
  349. case float64:
  350. fleft = left.(float64)
  351. isInt = false
  352. }
  353. switch right.(type) {
  354. case int:
  355. rright = int64(right.(int))
  356. case int8:
  357. rright = int64(right.(int8))
  358. case int16:
  359. rright = int64(right.(int16))
  360. case int32:
  361. rright = int64(right.(int32))
  362. case int64:
  363. rright = right.(int64)
  364. case float32:
  365. fright = float64(left.(float32))
  366. isInt = false
  367. case float64:
  368. fleft = left.(float64)
  369. isInt = false
  370. }
  371. if isInt {
  372. return rleft - rright
  373. } else {
  374. return fleft + float64(rleft) - (fright + float64(rright))
  375. }
  376. }
  377. // EllipsisString returns a truncated short string,
  378. // it appends '...' in the end of the length of string is too large.
  379. func EllipsisString(str string, length int) string {
  380. if len(str) < length {
  381. return str
  382. }
  383. return str[:length-3] + "..."
  384. }
  385. // TruncateString returns a truncated string with given limit,
  386. // it returns input string if length is not reached limit.
  387. func TruncateString(str string, limit int) string {
  388. if len(str) < limit {
  389. return str
  390. }
  391. return str[:limit]
  392. }
  393. // StringsToInt64s converts a slice of string to a slice of int64.
  394. func StringsToInt64s(strs []string) []int64 {
  395. ints := make([]int64, len(strs))
  396. for i := range strs {
  397. ints[i] = com.StrTo(strs[i]).MustInt64()
  398. }
  399. return ints
  400. }
  401. // Int64sToStrings converts a slice of int64 to a slice of string.
  402. func Int64sToStrings(ints []int64) []string {
  403. strs := make([]string, len(ints))
  404. for i := range ints {
  405. strs[i] = com.ToStr(ints[i])
  406. }
  407. return strs
  408. }
  409. // Int64sToMap converts a slice of int64 to a int64 map.
  410. func Int64sToMap(ints []int64) map[int64]bool {
  411. m := make(map[int64]bool)
  412. for _, i := range ints {
  413. m[i] = true
  414. }
  415. return m
  416. }
  417. // IsLetter reports whether the rune is a letter (category L).
  418. // https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257
  419. func IsLetter(ch rune) bool {
  420. return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
  421. }
  422. // IsTextFile returns true if file content format is plain text or empty.
  423. func IsTextFile(data []byte) bool {
  424. if len(data) == 0 {
  425. return true
  426. }
  427. return strings.Index(http.DetectContentType(data), "text/") != -1
  428. }
  429. func IsImageFile(data []byte) bool {
  430. return strings.Index(http.DetectContentType(data), "image/") != -1
  431. }
  432. func IsPDFFile(data []byte) bool {
  433. return strings.Index(http.DetectContentType(data), "application/pdf") != -1
  434. }
  435. func IsVideoFile(data []byte) bool {
  436. return strings.Index(http.DetectContentType(data), "video/") != -1
  437. }