|
- // Copyright 2017 The Gitea Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
-
- package utils
-
- import (
- "strings"
- "time"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/convert"
- )
-
- // GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
- func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) {
- qCreatedBefore := strings.Trim(ctx.Query("before"), " ")
- if qCreatedBefore != "" {
- createdBefore, err := time.Parse(time.RFC3339, qCreatedBefore)
- if err != nil {
- return 0, 0, err
- }
- if !createdBefore.IsZero() {
- before = createdBefore.Unix()
- }
- }
-
- qCreatedAfter := strings.Trim(ctx.Query("since"), " ")
- if qCreatedAfter != "" {
- createdAfter, err := time.Parse(time.RFC3339, qCreatedAfter)
- if err != nil {
- return 0, 0, err
- }
- if !createdAfter.IsZero() {
- since = createdAfter.Unix()
- }
- }
- return before, since, nil
- }
-
- // GetListOptions returns list options using the page and limit parameters
- func GetListOptions(ctx *context.APIContext) models.ListOptions {
- return models.ListOptions{
- Page: ctx.QueryInt("page"),
- PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
- }
- }
-
- // JudgeLanguageBySuffix get language from fileName
- func JudgeLanguageBySuffix(filename string) string {
- if len(filename) == 0 {
- return "shell"
- }
- arr := strings.Split(filename, ".")
- suffix := strings.ToLower(arr[len(arr)-1])
- suffixLanguageMap := map[string]string{
- "py": "python",
- "c": "cpp",
- "h": "cpp",
- "g4": "cpp",
- "sy": "cpp",
- "cc": "cpp",
- "cxx": "cpp",
- "c++": "cpp",
- "cu": "cpp",
- "cpp": "cpp",
- "dynamips": "cpp",
- "java": "java",
- "php": "php",
- "html": "html",
- "css": "css",
- "scss": "scss",
- "go": "go",
- "r": "r",
- "graphql": "graphql",
- "swift": "swift",
- "xml": "xml",
- "yaml": "yaml",
- "json": "json",
- "lua": "lua",
- "scheme": "scheme",
- "less": "less",
- "ini": "ini",
- "jpg": "jpg",
- "jpeg": "jpeg",
- "png": "png",
- "gif": "gif",
- "webp": "webp",
- "bmp": "bmp",
- "avi": "avi",
- "mp4": "mp4",
- "mov": "mov",
- "mp3": "mp3",
- "wav": "wav",
- "ogg": "ogg",
- "coffee": "coffeescript",
- "litcoffee": "coffeescript",
- "js": "javascript",
- "vue": "javascript",
- "ejs": "html",
- "cs": "csharp",
- "kt": "kotlin",
- "md": "markdown",
- "sql": "mysql",
- "ctrl": "mysql",
- "m": "objective-c",
- "mm": "objective-c",
- "pas": "pascal",
- "perl": "perl",
- "pl": "perl",
- "rb": "ruby",
- "rs": "rust", "rust": "rust",
- "tsx": "typescript",
- "ipynb": "json",
- "sh": "shell",
- "bash": "shell",
- }
-
- if suffixLanguageMap[suffix] == "" {
- return "shell"
- } else {
- return suffixLanguageMap[suffix]
- }
-
- }
-
- // JudgeFileTypeBySuffix get language from fileName
- func JudgeFileTypeBySuffix(filename string) string {
- if len(filename) == 0 {
- return "other"
- }
- arr := strings.Split(filename, ".")
- suffix := strings.ToLower(arr[len(arr)-1])
- suffixFileTypeMap := map[string]string{
- "py": "txt",
- "h": "txt",
- "c": "txt",
- "cpp": "txt",
- "cc": "txt",
- "java": "txt",
- "php": "txt",
- "html": "txt",
- "css": "txt",
- "scss": "txt",
- "go": "txt",
- "r": "txt",
- "graphql": "txt",
- "swift": "txt",
- "xml": "txt",
- "yaml": "txt",
- "json": "txt",
- "lua": "txt",
- "scheme": "txt",
- "less": "txt",
- "ini": "txt",
- "coffee": "txt",
- "litcoffee": "txt",
- "js": "txt",
- "cs": "txt",
- "kt": "txt",
- "md": "txt",
- "sql": "txt",
- "m": "txt",
- "mm": "txt",
- "pas": "txt",
- "perl": "txt",
- "ejs": "txt",
- "pl": "txt",
- "rb": "txt",
- "rs": "txt",
- "rust": "txt",
- "sh": "txt",
- "makefile": "txt",
- "circ": "txt",
- "readme": "txt",
- "yml": "txt",
- "sml": "txt",
- "conf": "txt",
- "txt": "txt",
- "gitignore": "txt",
- "in": "txt",
- "cu": "txt",
- "gemfile": "txt",
- "scala": "txt",
- "net": "txt",
- "l": "txt",
- "v": "txt",
- "config": "txt",
- "properties": "txt",
- "log": "txt",
- "htm": "txt",
- "cnf": "txt",
- "hex": "txt",
- "bat": "txt",
- "asm": "txt",
- "bash": "txt",
- "ts": "txt",
- "tsx": "txt",
- "sass": "txt",
- "jsx": "txt",
- "jsp": "txt",
- "gitkeep": "txt",
- "sv": "txt",
- "hql": "txt",
- "y": "txt",
- "jj": "txt",
- "pls": "txt",
- "sol": "txt",
- "ignore": "txt",
- "ctrl": "txt",
- "vue": "txt",
- "tex": "txt",
- "bib": "txt",
- "cls": "txt",
- "bst": "txt",
- "toc": "txt",
- "sty": "txt",
- "g4": "txt",
- "sy": "txt",
- "jpg": "image",
- "jpeg": "image",
- "png": "image",
- "gif": "image",
- "webp": "image",
- "bmp": "image",
- "avi": "video",
- "mp4": "video",
- "mov": "video",
- "mp3": "audio",
- "wav": "audio",
- "ogg": "audio",
- "pptm": "office",
- "pptx": "office",
- "ppt": "office",
- "pot": "office",
- "pps": "office",
- "ppa": "office",
- "potx": "office",
- "ppsx": "office",
- "ppam": "office",
- "potm": "office",
- "ppsm": "office",
- "doc": "office",
- "docx": "office",
- "dot": "office",
- "dotx": "office",
- "docm": "office",
- "dotm": "office",
- "xls": "office",
- "xlsx": "office",
- "csv": "office",
- "xlt": "office",
- "xla": "office",
- "xltx": "office",
- "xlsm": "office",
- "xltm": "office",
- "xlam": "office",
- "xlsb": "office",
- "pdf": "pdf",
- "ipynb": "ipynb",
- }
- if suffixFileTypeMap[suffix] == "" {
- return "other"
- } else {
- return suffixFileTypeMap[suffix]
- }
- }
|