You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool.go 15KB

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