tool.go 11 KB

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