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

tool.go 11 kB

10 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
Add Octicon SVG spritemap (#10107) * Add octicon SVG sprite Signed-off-by: jolheiser <john.olheiser@gmail.com> * Static prefix Signed-off-by: jolheiser <john.olheiser@gmail.com> * SVG for all repo icons Signed-off-by: jolheiser <john.olheiser@gmail.com> * make vendor Signed-off-by: jolheiser <john.olheiser@gmail.com> * Swap out octicons Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move octicons to top of less imports Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix JS Signed-off-by: jolheiser <john.olheiser@gmail.com> * Definitely not a search/replace Signed-off-by: jolheiser <john.olheiser@gmail.com> * Missed regex Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move to more generic calls and webpack Signed-off-by: jolheiser <john.olheiser@gmail.com> * make svg -> make webpack Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove svg-sprite Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Missed a test Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove svg from makefile Signed-off-by: jolheiser <john.olheiser@gmail.com> * Suggestions Signed-off-by: jolheiser <john.olheiser@gmail.com> * Attempt to fix test Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Revert timetracking test Signed-off-by: jolheiser <john.olheiser@gmail.com> * Swap .octicon for .svg in less Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add aria-hidden Signed-off-by: jolheiser <john.olheiser@gmail.com> * Replace mega-octicon Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix webpack globbing on Windows Signed-off-by: jolheiser <john.olheiser@gmail.com> * Revert Co-Authored-By: silverwind <me@silverwind.io> * Fix octions from upstream Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix Vue and missed JS function Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add JS helper and PWA Signed-off-by: jolheiser <john.olheiser@gmail.com> * Preload SVG Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: techknowlogick <matti@mdranta.net>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/sha1"
  8. "crypto/sha256"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "fmt"
  12. "net/http"
  13. "net/url"
  14. "os"
  15. "path"
  16. "path/filepath"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "unicode"
  22. "unicode/utf8"
  23. "code.gitea.io/gitea/modules/git"
  24. "code.gitea.io/gitea/modules/log"
  25. "code.gitea.io/gitea/modules/setting"
  26. "github.com/dustin/go-humanize"
  27. "github.com/unknwon/com"
  28. )
  29. // EncodeMD5 encodes string to md5 hex value.
  30. func EncodeMD5(str string) string {
  31. m := md5.New()
  32. _, _ = m.Write([]byte(str))
  33. return hex.EncodeToString(m.Sum(nil))
  34. }
  35. // EncodeSha1 string to sha1 hex value.
  36. func EncodeSha1(str string) string {
  37. h := sha1.New()
  38. _, _ = h.Write([]byte(str))
  39. return hex.EncodeToString(h.Sum(nil))
  40. }
  41. // EncodeSha256 string to sha1 hex value.
  42. func EncodeSha256(str string) string {
  43. h := sha256.New()
  44. _, _ = h.Write([]byte(str))
  45. return hex.EncodeToString(h.Sum(nil))
  46. }
  47. // ShortSha is basically just truncating.
  48. // It is DEPRECATED and will be removed in the future.
  49. func ShortSha(sha1 string) string {
  50. return TruncateString(sha1, 10)
  51. }
  52. // BasicAuthDecode decode basic auth string
  53. func BasicAuthDecode(encoded string) (string, string, error) {
  54. s, err := base64.StdEncoding.DecodeString(encoded)
  55. if err != nil {
  56. return "", "", err
  57. }
  58. auth := strings.SplitN(string(s), ":", 2)
  59. return auth[0], auth[1], nil
  60. }
  61. // BasicAuthEncode encode basic auth string
  62. func BasicAuthEncode(username, password string) string {
  63. return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  64. }
  65. // VerifyTimeLimitCode verify time limit code
  66. func VerifyTimeLimitCode(data string, minutes int, code string) bool {
  67. if len(code) <= 18 {
  68. return false
  69. }
  70. // split code
  71. start := code[:12]
  72. lives := code[12:18]
  73. if d, err := com.StrTo(lives).Int(); err == nil {
  74. minutes = d
  75. }
  76. // right active code
  77. retCode := CreateTimeLimitCode(data, minutes, start)
  78. if retCode == code && minutes > 0 {
  79. // check time is expired or not
  80. before, _ := time.ParseInLocation("200601021504", start, time.Local)
  81. now := time.Now()
  82. if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. // TimeLimitCodeLength default value for time limit code
  89. const TimeLimitCodeLength = 12 + 6 + 40
  90. // CreateTimeLimitCode create a time limit code
  91. // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
  92. func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
  93. format := "200601021504"
  94. var start, end time.Time
  95. var startStr, endStr string
  96. if startInf == nil {
  97. // Use now time create code
  98. start = time.Now()
  99. startStr = start.Format(format)
  100. } else {
  101. // use start string create code
  102. startStr = startInf.(string)
  103. start, _ = time.ParseInLocation(format, startStr, time.Local)
  104. startStr = start.Format(format)
  105. }
  106. end = start.Add(time.Minute * time.Duration(minutes))
  107. endStr = end.Format(format)
  108. // create sha1 encode string
  109. sh := sha1.New()
  110. _, _ = sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
  111. encoded := hex.EncodeToString(sh.Sum(nil))
  112. code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
  113. return code
  114. }
  115. // HashEmail hashes email address to MD5 string.
  116. // https://en.gravatar.com/site/implement/hash/
  117. func HashEmail(email string) string {
  118. return EncodeMD5(strings.ToLower(strings.TrimSpace(email)))
  119. }
  120. // DefaultAvatarLink the default avatar link
  121. func DefaultAvatarLink() string {
  122. return setting.AppSubURL + "/img/avatar_default.png"
  123. }
  124. // DefaultAvatarSize is a sentinel value for the default avatar size, as
  125. // determined by the avatar-hosting service.
  126. const DefaultAvatarSize = -1
  127. // libravatarURL returns the URL for the given email. This function should only
  128. // be called if a federated avatar service is enabled.
  129. func libravatarURL(email string) (*url.URL, error) {
  130. urlStr, err := setting.LibravatarService.FromEmail(email)
  131. if err != nil {
  132. log.Error("LibravatarService.FromEmail(email=%s): error %v", email, err)
  133. return nil, err
  134. }
  135. u, err := url.Parse(urlStr)
  136. if err != nil {
  137. log.Error("Failed to parse libravatar url(%s): error %v", urlStr, err)
  138. return nil, err
  139. }
  140. return u, nil
  141. }
  142. // SizedAvatarLink returns a sized link to the avatar for the given email
  143. // address.
  144. func SizedAvatarLink(email string, size int) string {
  145. var avatarURL *url.URL
  146. if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
  147. var err error
  148. avatarURL, err = libravatarURL(email)
  149. if err != nil {
  150. return DefaultAvatarLink()
  151. }
  152. } else if !setting.DisableGravatar {
  153. // copy GravatarSourceURL, because we will modify its Path.
  154. copyOfGravatarSourceURL := *setting.GravatarSourceURL
  155. avatarURL = &copyOfGravatarSourceURL
  156. avatarURL.Path = path.Join(avatarURL.Path, HashEmail(email))
  157. } else {
  158. return DefaultAvatarLink()
  159. }
  160. vals := avatarURL.Query()
  161. vals.Set("d", "identicon")
  162. if size != DefaultAvatarSize {
  163. vals.Set("s", strconv.Itoa(size))
  164. }
  165. avatarURL.RawQuery = vals.Encode()
  166. return avatarURL.String()
  167. }
  168. // SizedAvatarLinkWithDomain returns a sized link to the avatar for the given email
  169. // address.
  170. func SizedAvatarLinkWithDomain(email string, size int) string {
  171. var avatarURL *url.URL
  172. if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
  173. var err error
  174. avatarURL, err = libravatarURL(email)
  175. if err != nil {
  176. return DefaultAvatarLink()
  177. }
  178. } else if !setting.DisableGravatar {
  179. // copy GravatarSourceURL, because we will modify its Path.
  180. copyOfGravatarSourceURL := *setting.GravatarSourceURL
  181. avatarURL = &copyOfGravatarSourceURL
  182. avatarURL.Path = path.Join(avatarURL.Path, HashEmail(email))
  183. } else {
  184. return DefaultAvatarLink()
  185. }
  186. vals := avatarURL.Query()
  187. vals.Set("d", "identicon")
  188. if size != DefaultAvatarSize {
  189. vals.Set("s", strconv.Itoa(size))
  190. }
  191. avatarURL.RawQuery = vals.Encode()
  192. return avatarURL.String()
  193. }
  194. // FileSize calculates the file size and generate user-friendly string.
  195. func FileSize(s int64) string {
  196. return humanize.IBytes(uint64(s))
  197. }
  198. // PrettyNumber produces a string form of the given number in base 10 with
  199. // commas after every three orders of magnitud
  200. func PrettyNumber(v int64) string {
  201. return humanize.Comma(v)
  202. }
  203. // Subtract deals with subtraction of all types of number.
  204. func Subtract(left interface{}, right interface{}) interface{} {
  205. var rleft, rright int64
  206. var fleft, fright float64
  207. var isInt = true
  208. switch v := left.(type) {
  209. case int:
  210. rleft = int64(v)
  211. case int8:
  212. rleft = int64(v)
  213. case int16:
  214. rleft = int64(v)
  215. case int32:
  216. rleft = int64(v)
  217. case int64:
  218. rleft = v
  219. case float32:
  220. fleft = float64(v)
  221. isInt = false
  222. case float64:
  223. fleft = v
  224. isInt = false
  225. }
  226. switch v := right.(type) {
  227. case int:
  228. rright = int64(v)
  229. case int8:
  230. rright = int64(v)
  231. case int16:
  232. rright = int64(v)
  233. case int32:
  234. rright = int64(v)
  235. case int64:
  236. rright = v
  237. case float32:
  238. fright = float64(v)
  239. isInt = false
  240. case float64:
  241. fright = v
  242. isInt = false
  243. }
  244. if isInt {
  245. return rleft - rright
  246. }
  247. return fleft + float64(rleft) - (fright + float64(rright))
  248. }
  249. // EllipsisString returns a truncated short string,
  250. // it appends '...' in the end of the length of string is too large.
  251. func EllipsisString(str string, length int) string {
  252. if length <= 3 {
  253. return "..."
  254. }
  255. if utf8.RuneCountInString(str) <= length {
  256. return str
  257. }
  258. return string([]rune(str)[:length-3]) + "..."
  259. }
  260. // TruncateString returns a truncated string with given limit,
  261. // it returns input string if length is not reached limit.
  262. func TruncateString(str string, limit int) string {
  263. if utf8.RuneCountInString(str) < limit {
  264. return str
  265. }
  266. return string([]rune(str)[:limit])
  267. }
  268. // StringsToInt64s converts a slice of string to a slice of int64.
  269. func StringsToInt64s(strs []string) ([]int64, error) {
  270. ints := make([]int64, len(strs))
  271. for i := range strs {
  272. n, err := com.StrTo(strs[i]).Int64()
  273. if err != nil {
  274. return ints, err
  275. }
  276. ints[i] = n
  277. }
  278. return ints, nil
  279. }
  280. // Int64sToStrings converts a slice of int64 to a slice of string.
  281. func Int64sToStrings(ints []int64) []string {
  282. strs := make([]string, len(ints))
  283. for i := range ints {
  284. strs[i] = strconv.FormatInt(ints[i], 10)
  285. }
  286. return strs
  287. }
  288. // Int64sToMap converts a slice of int64 to a int64 map.
  289. func Int64sToMap(ints []int64) map[int64]bool {
  290. m := make(map[int64]bool)
  291. for _, i := range ints {
  292. m[i] = true
  293. }
  294. return m
  295. }
  296. // Int64sContains returns if a int64 in a slice of int64
  297. func Int64sContains(intsSlice []int64, a int64) bool {
  298. for _, c := range intsSlice {
  299. if c == a {
  300. return true
  301. }
  302. }
  303. return false
  304. }
  305. // IsLetter reports whether the rune is a letter (category L).
  306. // https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257
  307. func IsLetter(ch rune) bool {
  308. return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
  309. }
  310. // IsTextFile returns true if file content format is plain text or empty.
  311. func IsTextFile(data []byte) bool {
  312. if len(data) == 0 {
  313. return true
  314. }
  315. return strings.Contains(http.DetectContentType(data), "text/")
  316. }
  317. // IsImageFile detects if data is an image format
  318. func IsImageFile(data []byte) bool {
  319. return strings.Contains(http.DetectContentType(data), "image/")
  320. }
  321. // IsPDFFile detects if data is a pdf format
  322. func IsPDFFile(data []byte) bool {
  323. return strings.Contains(http.DetectContentType(data), "application/pdf")
  324. }
  325. // IsVideoFile detects if data is an video format
  326. func IsVideoFile(data []byte) bool {
  327. return strings.Contains(http.DetectContentType(data), "video/")
  328. }
  329. // IsAudioFile detects if data is an video format
  330. func IsAudioFile(data []byte) bool {
  331. return strings.Contains(http.DetectContentType(data), "audio/")
  332. }
  333. // EntryIcon returns the octicon class for displaying files/directories
  334. func EntryIcon(entry *git.TreeEntry) string {
  335. switch {
  336. case entry.IsLink():
  337. te, err := entry.FollowLink()
  338. if err != nil {
  339. log.Debug(err.Error())
  340. return "file-symlink-file"
  341. }
  342. if te.IsDir() {
  343. return "file-symlink-directory"
  344. }
  345. return "file-symlink-file"
  346. case entry.IsDir():
  347. return "file-directory"
  348. case entry.IsSubModule():
  349. return "file-submodule"
  350. }
  351. return "file"
  352. }
  353. // SetupGiteaRoot Sets GITEA_ROOT if it is not already set and returns the value
  354. func SetupGiteaRoot() string {
  355. giteaRoot := os.Getenv("GITEA_ROOT")
  356. if giteaRoot == "" {
  357. _, filename, _, _ := runtime.Caller(0)
  358. giteaRoot = strings.TrimSuffix(filename, "modules/base/tool.go")
  359. wd, err := os.Getwd()
  360. if err != nil {
  361. rel, err := filepath.Rel(giteaRoot, wd)
  362. if err != nil && strings.HasPrefix(filepath.ToSlash(rel), "../") {
  363. giteaRoot = wd
  364. }
  365. }
  366. if _, err := os.Stat(filepath.Join(giteaRoot, "gitea")); os.IsNotExist(err) {
  367. giteaRoot = ""
  368. } else if err := os.Setenv("GITEA_ROOT", giteaRoot); err != nil {
  369. giteaRoot = ""
  370. }
  371. }
  372. return giteaRoot
  373. }