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.

utils.go 6.5 kB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
6 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
6 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
6 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2017 The Gitea 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 utils
  5. import (
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. )
  12. // GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
  13. func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) {
  14. qCreatedBefore := strings.Trim(ctx.Query("before"), " ")
  15. if qCreatedBefore != "" {
  16. createdBefore, err := time.Parse(time.RFC3339, qCreatedBefore)
  17. if err != nil {
  18. return 0, 0, err
  19. }
  20. if !createdBefore.IsZero() {
  21. before = createdBefore.Unix()
  22. }
  23. }
  24. qCreatedAfter := strings.Trim(ctx.Query("since"), " ")
  25. if qCreatedAfter != "" {
  26. createdAfter, err := time.Parse(time.RFC3339, qCreatedAfter)
  27. if err != nil {
  28. return 0, 0, err
  29. }
  30. if !createdAfter.IsZero() {
  31. since = createdAfter.Unix()
  32. }
  33. }
  34. return before, since, nil
  35. }
  36. // GetListOptions returns list options using the page and limit parameters
  37. func GetListOptions(ctx *context.APIContext) models.ListOptions {
  38. return models.ListOptions{
  39. Page: ctx.QueryInt("page"),
  40. PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
  41. }
  42. }
  43. // JudgeLanguageBySuffix get language from fileName
  44. func JudgeLanguageBySuffix(filename string) string {
  45. if len(filename) == 0 {
  46. return "shell"
  47. }
  48. arr := strings.Split(filename, ".")
  49. suffix := strings.ToLower(arr[len(arr)-1])
  50. suffixLanguageMap := map[string]string{
  51. "py": "python",
  52. "c": "cpp",
  53. "h": "cpp",
  54. "g4": "cpp",
  55. "sy": "cpp",
  56. "cc": "cpp",
  57. "cxx": "cpp",
  58. "c++": "cpp",
  59. "cu": "cpp",
  60. "cpp": "cpp",
  61. "dynamips": "cpp",
  62. "java": "java",
  63. "php": "php",
  64. "html": "html",
  65. "css": "css",
  66. "scss": "scss",
  67. "go": "go",
  68. "r": "r",
  69. "graphql": "graphql",
  70. "swift": "swift",
  71. "xml": "xml",
  72. "yaml": "yaml",
  73. "json": "json",
  74. "lua": "lua",
  75. "scheme": "scheme",
  76. "less": "less",
  77. "ini": "ini",
  78. "jpg": "jpg",
  79. "jpeg": "jpeg",
  80. "png": "png",
  81. "gif": "gif",
  82. "webp": "webp",
  83. "bmp": "bmp",
  84. "avi": "avi",
  85. "mp4": "mp4",
  86. "mov": "mov",
  87. "mp3": "mp3",
  88. "wav": "wav",
  89. "ogg": "ogg",
  90. "coffee": "coffeescript",
  91. "litcoffee": "coffeescript",
  92. "js": "javascript",
  93. "vue": "javascript",
  94. "ejs": "html",
  95. "cs": "csharp",
  96. "kt": "kotlin",
  97. "md": "markdown",
  98. "sql": "mysql",
  99. "ctrl": "mysql",
  100. "m": "objective-c",
  101. "mm": "objective-c",
  102. "pas": "pascal",
  103. "perl": "perl",
  104. "pl": "perl",
  105. "rb": "ruby",
  106. "rs": "rust", "rust": "rust",
  107. "tsx": "typescript",
  108. "ipynb": "json",
  109. "sh": "shell",
  110. "bash": "shell",
  111. }
  112. if suffixLanguageMap[suffix] == "" {
  113. return "shell"
  114. } else {
  115. return suffixLanguageMap[suffix]
  116. }
  117. }
  118. // JudgeFileTypeBySuffix get language from fileName
  119. func JudgeFileTypeBySuffix(filename string) string {
  120. if len(filename) == 0 {
  121. return "other"
  122. }
  123. arr := strings.Split(filename, ".")
  124. suffix := strings.ToLower(arr[len(arr)-1])
  125. suffixFileTypeMap := map[string]string{
  126. "py": "txt",
  127. "h": "txt",
  128. "c": "txt",
  129. "cpp": "txt",
  130. "cc": "txt",
  131. "java": "txt",
  132. "php": "txt",
  133. "html": "txt",
  134. "css": "txt",
  135. "scss": "txt",
  136. "go": "txt",
  137. "r": "txt",
  138. "graphql": "txt",
  139. "swift": "txt",
  140. "xml": "txt",
  141. "yaml": "txt",
  142. "json": "txt",
  143. "lua": "txt",
  144. "scheme": "txt",
  145. "less": "txt",
  146. "ini": "txt",
  147. "coffee": "txt",
  148. "litcoffee": "txt",
  149. "js": "txt",
  150. "cs": "txt",
  151. "kt": "txt",
  152. "md": "txt",
  153. "sql": "txt",
  154. "m": "txt",
  155. "mm": "txt",
  156. "pas": "txt",
  157. "perl": "txt",
  158. "ejs": "txt",
  159. "pl": "txt",
  160. "rb": "txt",
  161. "rs": "txt",
  162. "rust": "txt",
  163. "sh": "txt",
  164. "makefile": "txt",
  165. "circ": "txt",
  166. "readme": "txt",
  167. "yml": "txt",
  168. "sml": "txt",
  169. "conf": "txt",
  170. "txt": "txt",
  171. "gitignore": "txt",
  172. "in": "txt",
  173. "cu": "txt",
  174. "gemfile": "txt",
  175. "scala": "txt",
  176. "net": "txt",
  177. "l": "txt",
  178. "v": "txt",
  179. "config": "txt",
  180. "properties": "txt",
  181. "log": "txt",
  182. "htm": "txt",
  183. "cnf": "txt",
  184. "hex": "txt",
  185. "bat": "txt",
  186. "asm": "txt",
  187. "bash": "txt",
  188. "ts": "txt",
  189. "tsx": "txt",
  190. "sass": "txt",
  191. "jsx": "txt",
  192. "jsp": "txt",
  193. "gitkeep": "txt",
  194. "sv": "txt",
  195. "hql": "txt",
  196. "y": "txt",
  197. "jj": "txt",
  198. "pls": "txt",
  199. "sol": "txt",
  200. "ignore": "txt",
  201. "ctrl": "txt",
  202. "vue": "txt",
  203. "tex": "txt",
  204. "bib": "txt",
  205. "cls": "txt",
  206. "bst": "txt",
  207. "toc": "txt",
  208. "sty": "txt",
  209. "g4": "txt",
  210. "sy": "txt",
  211. "jpg": "image",
  212. "jpeg": "image",
  213. "png": "image",
  214. "gif": "image",
  215. "webp": "image",
  216. "bmp": "image",
  217. "avi": "video",
  218. "mp4": "video",
  219. "mov": "video",
  220. "mp3": "audio",
  221. "wav": "audio",
  222. "ogg": "audio",
  223. "pptm": "office",
  224. "pptx": "office",
  225. "ppt": "office",
  226. "pot": "office",
  227. "pps": "office",
  228. "ppa": "office",
  229. "potx": "office",
  230. "ppsx": "office",
  231. "ppam": "office",
  232. "potm": "office",
  233. "ppsm": "office",
  234. "doc": "office",
  235. "docx": "office",
  236. "dot": "office",
  237. "dotx": "office",
  238. "docm": "office",
  239. "dotm": "office",
  240. "xls": "office",
  241. "xlsx": "office",
  242. "csv": "office",
  243. "xlt": "office",
  244. "xla": "office",
  245. "xltx": "office",
  246. "xlsm": "office",
  247. "xltm": "office",
  248. "xlam": "office",
  249. "xlsb": "office",
  250. "pdf": "pdf",
  251. "ipynb": "ipynb",
  252. }
  253. if suffixFileTypeMap[suffix] == "" {
  254. return "other"
  255. } else {
  256. return suffixFileTypeMap[suffix]
  257. }
  258. }