utils.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package utils
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "time"
  9. )
  10. /*
  11. Common
  12. Some commonly used functions in ArozOS
  13. */
  14. // Response related
  15. func SendTextResponse(w http.ResponseWriter, msg string) {
  16. w.Write([]byte(msg))
  17. }
  18. // Send JSON response, with an extra json header
  19. func SendJSONResponse(w http.ResponseWriter, json string) {
  20. w.Header().Set("Content-Type", "application/json")
  21. w.Write([]byte(json))
  22. }
  23. func SendErrorResponse(w http.ResponseWriter, errMsg string) {
  24. w.Header().Set("Content-Type", "application/json")
  25. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  26. }
  27. func SendOK(w http.ResponseWriter) {
  28. w.Header().Set("Content-Type", "application/json")
  29. w.Write([]byte("\"OK\""))
  30. }
  31. /*
  32. The paramter move function (mv)
  33. You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in
  34. r (HTTP Request Object)
  35. getParamter (string, aka $_GET['This string])
  36. Will return
  37. Paramter string (if any)
  38. Error (if error)
  39. */
  40. /*
  41. func Mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  42. if postMode == false {
  43. //Access the paramter via GET
  44. keys, ok := r.URL.Query()[getParamter]
  45. if !ok || len(keys[0]) < 1 {
  46. //log.Println("Url Param " + getParamter +" is missing")
  47. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  48. }
  49. // Query()["key"] will return an array of items,
  50. // we only want the single item.
  51. key := keys[0]
  52. return string(key), nil
  53. } else {
  54. //Access the parameter via POST
  55. r.ParseForm()
  56. x := r.Form.Get(getParamter)
  57. if len(x) == 0 || x == "" {
  58. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  59. }
  60. return string(x), nil
  61. }
  62. }
  63. */
  64. // Get GET parameter
  65. func GetPara(r *http.Request, key string) (string, error) {
  66. keys, ok := r.URL.Query()[key]
  67. if !ok || len(keys[0]) < 1 {
  68. return "", errors.New("invalid " + key + " given")
  69. } else {
  70. return keys[0], nil
  71. }
  72. }
  73. // Get POST paramter
  74. func PostPara(r *http.Request, key string) (string, error) {
  75. r.ParseForm()
  76. x := r.Form.Get(key)
  77. if x == "" {
  78. return "", errors.New("invalid " + key + " given")
  79. } else {
  80. return x, nil
  81. }
  82. }
  83. func FileExists(filename string) bool {
  84. _, err := os.Stat(filename)
  85. if os.IsNotExist(err) {
  86. return false
  87. }
  88. return true
  89. }
  90. func IsDir(path string) bool {
  91. if FileExists(path) == false {
  92. return false
  93. }
  94. fi, err := os.Stat(path)
  95. if err != nil {
  96. log.Fatal(err)
  97. return false
  98. }
  99. switch mode := fi.Mode(); {
  100. case mode.IsDir():
  101. return true
  102. case mode.IsRegular():
  103. return false
  104. }
  105. return false
  106. }
  107. func TimeToString(targetTime time.Time) string {
  108. return targetTime.Format("2006-01-02 15:04:05")
  109. }
  110. // Use for redirections
  111. func ConstructRelativePathFromRequestURL(requestURI string, redirectionLocation string) string {
  112. if strings.Count(requestURI, "/") == 1 {
  113. //Already root level
  114. return redirectionLocation
  115. }
  116. for i := 0; i < strings.Count(requestURI, "/")-1; i++ {
  117. redirectionLocation = "../" + redirectionLocation
  118. }
  119. return redirectionLocation
  120. }
  121. // Check if given string in a given slice
  122. func StringInArray(arr []string, str string) bool {
  123. for _, a := range arr {
  124. if a == str {
  125. return true
  126. }
  127. }
  128. return false
  129. }
  130. func StringInArrayIgnoreCase(arr []string, str string) bool {
  131. smallArray := []string{}
  132. for _, item := range arr {
  133. smallArray = append(smallArray, strings.ToLower(item))
  134. }
  135. return StringInArray(smallArray, strings.ToLower(str))
  136. }