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 10 kB

11 years ago
11 years ago
11 years ago
11 years ago
10 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. "code.gitea.io/gitea/modules/git"
  23. "code.gitea.io/gitea/modules/log"
  24. "code.gitea.io/gitea/modules/setting"
  25. "github.com/dustin/go-humanize"
  26. "github.com/unknwon/com"
  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. // EncodeSha256 string to sha1 hex value.
  41. func EncodeSha256(str string) string {
  42. h := sha256.New()
  43. _, _ = h.Write([]byte(str))
  44. return hex.EncodeToString(h.Sum(nil))
  45. }
  46. // ShortSha is basically just truncating.
  47. // It is DEPRECATED and will be removed in the future.
  48. func ShortSha(sha1 string) string {
  49. return TruncateString(sha1, 10)
  50. }
  51. // BasicAuthDecode decode basic auth string
  52. func BasicAuthDecode(encoded string) (string, string, error) {
  53. s, err := base64.StdEncoding.DecodeString(encoded)
  54. if err != nil {
  55. return "", "", err
  56. }
  57. auth := strings.SplitN(string(s), ":", 2)
  58. return auth[0], auth[1], nil
  59. }
  60. // BasicAuthEncode encode basic auth string
  61. func BasicAuthEncode(username, password string) string {
  62. return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  63. }
  64. // VerifyTimeLimitCode verify time limit code
  65. func VerifyTimeLimitCode(data string, minutes int, code string) bool {
  66. if len(code) <= 18 {
  67. return false
  68. }
  69. // split code
  70. start := code[:12]
  71. lives := code[12:18]
  72. if d, err := com.StrTo(lives).Int(); err == nil {
  73. minutes = d
  74. }
  75. // right active code
  76. retCode := CreateTimeLimitCode(data, minutes, start)
  77. if retCode == code && minutes > 0 {
  78. // check time is expired or not
  79. before, _ := time.ParseInLocation("200601021504", start, time.Local)
  80. now := time.Now()
  81. if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. // TimeLimitCodeLength default value for time limit code
  88. const TimeLimitCodeLength = 12 + 6 + 40
  89. // CreateTimeLimitCode create a time limit code
  90. // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
  91. func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
  92. format := "200601021504"
  93. var start, end time.Time
  94. var startStr, endStr string
  95. if startInf == nil {
  96. // Use now time create code
  97. start = time.Now()
  98. startStr = start.Format(format)
  99. } else {
  100. // use start string create code
  101. startStr = startInf.(string)
  102. start, _ = time.ParseInLocation(format, startStr, time.Local)
  103. startStr = start.Format(format)
  104. }
  105. end = start.Add(time.Minute * time.Duration(minutes))
  106. endStr = end.Format(format)
  107. // create sha1 encode string
  108. sh := sha1.New()
  109. _, _ = sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
  110. encoded := hex.EncodeToString(sh.Sum(nil))
  111. code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
  112. return code
  113. }
  114. // HashEmail hashes email address to MD5 string.
  115. // https://en.gravatar.com/site/implement/hash/
  116. func HashEmail(email string) string {
  117. return EncodeMD5(strings.ToLower(strings.TrimSpace(email)))
  118. }
  119. // DefaultAvatarLink the default avatar link
  120. func DefaultAvatarLink() string {
  121. return setting.AppSubURL + "/img/avatar_default.png"
  122. }
  123. // DefaultAvatarSize is a sentinel value for the default avatar size, as
  124. // determined by the avatar-hosting service.
  125. const DefaultAvatarSize = -1
  126. // libravatarURL returns the URL for the given email. This function should only
  127. // be called if a federated avatar service is enabled.
  128. func libravatarURL(email string) (*url.URL, error) {
  129. urlStr, err := setting.LibravatarService.FromEmail(email)
  130. if err != nil {
  131. log.Error("LibravatarService.FromEmail(email=%s): error %v", email, err)
  132. return nil, err
  133. }
  134. u, err := url.Parse(urlStr)
  135. if err != nil {
  136. log.Error("Failed to parse libravatar url(%s): error %v", urlStr, err)
  137. return nil, err
  138. }
  139. return u, nil
  140. }
  141. // SizedAvatarLink returns a sized link to the avatar for the given email
  142. // address.
  143. func SizedAvatarLink(email string, size int) string {
  144. var avatarURL *url.URL
  145. if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
  146. var err error
  147. avatarURL, err = libravatarURL(email)
  148. if err != nil {
  149. return DefaultAvatarLink()
  150. }
  151. } else if !setting.DisableGravatar {
  152. // copy GravatarSourceURL, because we will modify its Path.
  153. copyOfGravatarSourceURL := *setting.GravatarSourceURL
  154. avatarURL = &copyOfGravatarSourceURL
  155. avatarURL.Path = path.Join(avatarURL.Path, HashEmail(email))
  156. } else {
  157. return DefaultAvatarLink()
  158. }
  159. vals := avatarURL.Query()
  160. vals.Set("d", "identicon")
  161. if size != DefaultAvatarSize {
  162. vals.Set("s", strconv.Itoa(size))
  163. }
  164. avatarURL.RawQuery = vals.Encode()
  165. return avatarURL.String()
  166. }
  167. // AvatarLink returns relative avatar link to the site domain by given email,
  168. // which includes app sub-url as prefix. However, it is possible
  169. // to return full URL if user enables Gravatar-like service.
  170. func AvatarLink(email string) string {
  171. return SizedAvatarLink(email, DefaultAvatarSize)
  172. }
  173. // FileSize calculates the file size and generate user-friendly string.
  174. func FileSize(s int64) string {
  175. return humanize.IBytes(uint64(s))
  176. }
  177. // PrettyNumber produces a string form of the given number in base 10 with
  178. // commas after every three orders of magnitud
  179. func PrettyNumber(v int64) string {
  180. return humanize.Comma(v)
  181. }
  182. // Subtract deals with subtraction of all types of number.
  183. func Subtract(left interface{}, right interface{}) interface{} {
  184. var rleft, rright int64
  185. var fleft, fright float64
  186. var isInt = true
  187. switch v := left.(type) {
  188. case int:
  189. rleft = int64(v)
  190. case int8:
  191. rleft = int64(v)
  192. case int16:
  193. rleft = int64(v)
  194. case int32:
  195. rleft = int64(v)
  196. case int64:
  197. rleft = v
  198. case float32:
  199. fleft = float64(v)
  200. isInt = false
  201. case float64:
  202. fleft = v
  203. isInt = false
  204. }
  205. switch v := right.(type) {
  206. case int:
  207. rright = int64(v)
  208. case int8:
  209. rright = int64(v)
  210. case int16:
  211. rright = int64(v)
  212. case int32:
  213. rright = int64(v)
  214. case int64:
  215. rright = v
  216. case float32:
  217. fright = float64(v)
  218. isInt = false
  219. case float64:
  220. fright = v
  221. isInt = false
  222. }
  223. if isInt {
  224. return rleft - rright
  225. }
  226. return fleft + float64(rleft) - (fright + float64(rright))
  227. }
  228. // EllipsisString returns a truncated short string,
  229. // it appends '...' in the end of the length of string is too large.
  230. func EllipsisString(str string, length int) string {
  231. if length <= 3 {
  232. return "..."
  233. }
  234. if len(str) <= length {
  235. return str
  236. }
  237. return str[:length-3] + "..."
  238. }
  239. // TruncateString returns a truncated string with given limit,
  240. // it returns input string if length is not reached limit.
  241. func TruncateString(str string, limit int) string {
  242. if len(str) < limit {
  243. return str
  244. }
  245. return str[:limit]
  246. }
  247. // StringsToInt64s converts a slice of string to a slice of int64.
  248. func StringsToInt64s(strs []string) ([]int64, error) {
  249. ints := make([]int64, len(strs))
  250. for i := range strs {
  251. n, err := com.StrTo(strs[i]).Int64()
  252. if err != nil {
  253. return ints, err
  254. }
  255. ints[i] = n
  256. }
  257. return ints, nil
  258. }
  259. // Int64sToStrings converts a slice of int64 to a slice of string.
  260. func Int64sToStrings(ints []int64) []string {
  261. strs := make([]string, len(ints))
  262. for i := range ints {
  263. strs[i] = strconv.FormatInt(ints[i], 10)
  264. }
  265. return strs
  266. }
  267. // Int64sToMap converts a slice of int64 to a int64 map.
  268. func Int64sToMap(ints []int64) map[int64]bool {
  269. m := make(map[int64]bool)
  270. for _, i := range ints {
  271. m[i] = true
  272. }
  273. return m
  274. }
  275. // Int64sContains returns if a int64 in a slice of int64
  276. func Int64sContains(intsSlice []int64, a int64) bool {
  277. for _, c := range intsSlice {
  278. if c == a {
  279. return true
  280. }
  281. }
  282. return false
  283. }
  284. // IsLetter reports whether the rune is a letter (category L).
  285. // https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257
  286. func IsLetter(ch rune) bool {
  287. return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
  288. }
  289. // IsTextFile returns true if file content format is plain text or empty.
  290. func IsTextFile(data []byte) bool {
  291. if len(data) == 0 {
  292. return true
  293. }
  294. return strings.Contains(http.DetectContentType(data), "text/")
  295. }
  296. // IsImageFile detects if data is an image format
  297. func IsImageFile(data []byte) bool {
  298. return strings.Contains(http.DetectContentType(data), "image/")
  299. }
  300. // IsPDFFile detects if data is a pdf format
  301. func IsPDFFile(data []byte) bool {
  302. return strings.Contains(http.DetectContentType(data), "application/pdf")
  303. }
  304. // IsVideoFile detects if data is an video format
  305. func IsVideoFile(data []byte) bool {
  306. return strings.Contains(http.DetectContentType(data), "video/")
  307. }
  308. // IsAudioFile detects if data is an video format
  309. func IsAudioFile(data []byte) bool {
  310. return strings.Contains(http.DetectContentType(data), "audio/")
  311. }
  312. // EntryIcon returns the octicon class for displaying files/directories
  313. func EntryIcon(entry *git.TreeEntry) string {
  314. switch {
  315. case entry.IsLink():
  316. te, err := entry.FollowLink()
  317. if err != nil {
  318. log.Debug(err.Error())
  319. return "file-symlink-file"
  320. }
  321. if te.IsDir() {
  322. return "file-symlink-directory"
  323. }
  324. return "file-symlink-file"
  325. case entry.IsDir():
  326. return "file-directory"
  327. case entry.IsSubModule():
  328. return "file-submodule"
  329. }
  330. return "file"
  331. }
  332. // SetupGiteaRoot Sets GITEA_ROOT if it is not already set and returns the value
  333. func SetupGiteaRoot() string {
  334. giteaRoot := os.Getenv("GITEA_ROOT")
  335. if giteaRoot == "" {
  336. _, filename, _, _ := runtime.Caller(0)
  337. giteaRoot = strings.TrimSuffix(filename, "modules/base/tool.go")
  338. wd, err := os.Getwd()
  339. if err != nil {
  340. rel, err := filepath.Rel(giteaRoot, wd)
  341. if err != nil && strings.HasPrefix(filepath.ToSlash(rel), "../") {
  342. giteaRoot = wd
  343. }
  344. }
  345. if _, err := os.Stat(filepath.Join(giteaRoot, "gitea")); os.IsNotExist(err) {
  346. giteaRoot = ""
  347. } else if err := os.Setenv("GITEA_ROOT", giteaRoot); err != nil {
  348. giteaRoot = ""
  349. }
  350. }
  351. return giteaRoot
  352. }