tool.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. "encoding/hex"
  8. "encoding/json"
  9. "fmt"
  10. "math"
  11. "strings"
  12. "time"
  13. )
  14. // Encode string to md5 hex value
  15. func EncodeMd5(str string) string {
  16. m := md5.New()
  17. m.Write([]byte(str))
  18. return hex.EncodeToString(m.Sum(nil))
  19. }
  20. // Seconds-based time units
  21. const (
  22. Minute = 60
  23. Hour = 60 * Minute
  24. Day = 24 * Hour
  25. Week = 7 * Day
  26. Month = 30 * Day
  27. Year = 12 * Month
  28. )
  29. // TimeSince calculates the time interval and generate user-friendly string.
  30. func TimeSince(then time.Time) string {
  31. now := time.Now()
  32. lbl := "ago"
  33. diff := now.Unix() - then.Unix()
  34. if then.After(now) {
  35. lbl = "from now"
  36. diff = then.Unix() - now.Unix()
  37. }
  38. switch {
  39. case diff <= 0:
  40. return "now"
  41. case diff <= 2:
  42. return fmt.Sprintf("1 second %s", lbl)
  43. case diff < 1*Minute:
  44. return fmt.Sprintf("%d seconds %s", diff, lbl)
  45. case diff < 2*Minute:
  46. return fmt.Sprintf("1 minute %s", lbl)
  47. case diff < 1*Hour:
  48. return fmt.Sprintf("%d minutes %s", diff/Minute, lbl)
  49. case diff < 2*Hour:
  50. return fmt.Sprintf("1 hour %s", lbl)
  51. case diff < 1*Day:
  52. return fmt.Sprintf("%d hours %s", diff/Hour, lbl)
  53. case diff < 2*Day:
  54. return fmt.Sprintf("1 day %s", lbl)
  55. case diff < 1*Week:
  56. return fmt.Sprintf("%d days %s", diff/Day, lbl)
  57. case diff < 2*Week:
  58. return fmt.Sprintf("1 week %s", lbl)
  59. case diff < 1*Month:
  60. return fmt.Sprintf("%d weeks %s", diff/Week, lbl)
  61. case diff < 2*Month:
  62. return fmt.Sprintf("1 month %s", lbl)
  63. case diff < 1*Year:
  64. return fmt.Sprintf("%d months %s", diff/Month, lbl)
  65. case diff < 18*Month:
  66. return fmt.Sprintf("1 year %s", lbl)
  67. }
  68. return then.String()
  69. }
  70. const (
  71. Byte = 1
  72. KByte = Byte * 1024
  73. MByte = KByte * 1024
  74. GByte = MByte * 1024
  75. TByte = GByte * 1024
  76. PByte = TByte * 1024
  77. EByte = PByte * 1024
  78. )
  79. var bytesSizeTable = map[string]uint64{
  80. "b": Byte,
  81. "kb": KByte,
  82. "mb": MByte,
  83. "gb": GByte,
  84. "tb": TByte,
  85. "pb": PByte,
  86. "eb": EByte,
  87. }
  88. func logn(n, b float64) float64 {
  89. return math.Log(n) / math.Log(b)
  90. }
  91. func humanateBytes(s uint64, base float64, sizes []string) string {
  92. if s < 10 {
  93. return fmt.Sprintf("%dB", s)
  94. }
  95. e := math.Floor(logn(float64(s), base))
  96. suffix := sizes[int(e)]
  97. val := float64(s) / math.Pow(base, math.Floor(e))
  98. f := "%.0f"
  99. if val < 10 {
  100. f = "%.1f"
  101. }
  102. return fmt.Sprintf(f+"%s", val, suffix)
  103. }
  104. // FileSize calculates the file size and generate user-friendly string.
  105. func FileSize(s int64) string {
  106. sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
  107. return humanateBytes(uint64(s), 1024, sizes)
  108. }
  109. // Subtract deals with subtraction of all types of number.
  110. func Subtract(left interface{}, right interface{}) interface{} {
  111. var rleft, rright int64
  112. var fleft, fright float64
  113. var isInt bool = true
  114. switch left.(type) {
  115. case int:
  116. rleft = int64(left.(int))
  117. case int8:
  118. rleft = int64(left.(int8))
  119. case int16:
  120. rleft = int64(left.(int16))
  121. case int32:
  122. rleft = int64(left.(int32))
  123. case int64:
  124. rleft = left.(int64)
  125. case float32:
  126. fleft = float64(left.(float32))
  127. isInt = false
  128. case float64:
  129. fleft = left.(float64)
  130. isInt = false
  131. }
  132. switch right.(type) {
  133. case int:
  134. rright = int64(right.(int))
  135. case int8:
  136. rright = int64(right.(int8))
  137. case int16:
  138. rright = int64(right.(int16))
  139. case int32:
  140. rright = int64(right.(int32))
  141. case int64:
  142. rright = right.(int64)
  143. case float32:
  144. fright = float64(left.(float32))
  145. isInt = false
  146. case float64:
  147. fleft = left.(float64)
  148. isInt = false
  149. }
  150. if isInt {
  151. return rleft - rright
  152. } else {
  153. return fleft + float64(rleft) - (fright + float64(rright))
  154. }
  155. }
  156. // DateFormat pattern rules.
  157. var datePatterns = []string{
  158. // year
  159. "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
  160. "y", "06", //A two digit representation of a year Examples: 99 or 03
  161. // month
  162. "m", "01", // Numeric representation of a month, with leading zeros 01 through 12
  163. "n", "1", // Numeric representation of a month, without leading zeros 1 through 12
  164. "M", "Jan", // A short textual representation of a month, three letters Jan through Dec
  165. "F", "January", // A full textual representation of a month, such as January or March January through December
  166. // day
  167. "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
  168. "j", "2", // Day of the month without leading zeros 1 to 31
  169. // week
  170. "D", "Mon", // A textual representation of a day, three letters Mon through Sun
  171. "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
  172. // time
  173. "g", "3", // 12-hour format of an hour without leading zeros 1 through 12
  174. "G", "15", // 24-hour format of an hour without leading zeros 0 through 23
  175. "h", "03", // 12-hour format of an hour with leading zeros 01 through 12
  176. "H", "15", // 24-hour format of an hour with leading zeros 00 through 23
  177. "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
  178. "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
  179. "i", "04", // Minutes with leading zeros 00 to 59
  180. "s", "05", // Seconds, with leading zeros 00 through 59
  181. // time zone
  182. "T", "MST",
  183. "P", "-07:00",
  184. "O", "-0700",
  185. // RFC 2822
  186. "r", time.RFC1123Z,
  187. }
  188. // Parse Date use PHP time format.
  189. func DateParse(dateString, format string) (time.Time, error) {
  190. replacer := strings.NewReplacer(datePatterns...)
  191. format = replacer.Replace(format)
  192. return time.ParseInLocation(format, dateString, time.Local)
  193. }
  194. // Date takes a PHP like date func to Go's time format.
  195. func DateFormat(t time.Time, format string) string {
  196. replacer := strings.NewReplacer(datePatterns...)
  197. format = replacer.Replace(format)
  198. return t.Format(format)
  199. }
  200. type Actioner interface {
  201. GetOpType() int
  202. GetActUserName() string
  203. GetRepoName() string
  204. GetContent() string
  205. }
  206. // ActionIcon accepts a int that represents action operation type
  207. // and returns a icon class name.
  208. func ActionIcon(opType int) string {
  209. switch opType {
  210. case 1: // Create repository.
  211. return "plus-circle"
  212. case 5: // Commit repository.
  213. return "arrow-circle-o-right"
  214. default:
  215. return "invalid type"
  216. }
  217. }
  218. const (
  219. TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s/%s">%s</a>`
  220. TPL_COMMIT_REPO = `<a href="/user/%s">%s</a> pushed to <a href="/%s/%s/tree/%s">%s</a> at <a href="/%s/%s">%s/%s</a>
  221. <ul>
  222. <li><a href="/%s/%s/commit/%s">%s</a> %s</li>
  223. </ul>`
  224. )
  225. // ActionDesc accepts int that represents action operation type
  226. // and returns the description.
  227. func ActionDesc(act Actioner) string {
  228. actUserName := act.GetActUserName()
  229. repoName := act.GetRepoName()
  230. content := act.GetContent()
  231. switch act.GetOpType() {
  232. case 1: // Create repository.
  233. return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, actUserName, repoName, repoName)
  234. case 5: // Commit repository.
  235. var commits [][]string
  236. if err := json.Unmarshal([]byte(content), &commits); err != nil {
  237. return err.Error()
  238. }
  239. return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, actUserName, repoName, "master", "master", actUserName, repoName, actUserName, repoName,
  240. actUserName, repoName, commits[0][0], commits[0][0][:7], commits[0][1])
  241. default:
  242. return "invalid type"
  243. }
  244. }