Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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