common.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strconv"
  11. "time"
  12. )
  13. /*
  14. Basic Response Functions
  15. Send response with ease
  16. */
  17. //Send text response with given w and message as string
  18. func sendTextResponse(w http.ResponseWriter, msg string) {
  19. w.Write([]byte(msg))
  20. }
  21. //Send JSON response, with an extra json header
  22. func sendJSONResponse(w http.ResponseWriter, json string) {
  23. w.Header().Set("Content-Type", "application/json")
  24. w.Write([]byte(json))
  25. }
  26. func sendErrorResponse(w http.ResponseWriter, errMsg string) {
  27. w.Header().Set("Content-Type", "application/json")
  28. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  29. }
  30. func sendOK(w http.ResponseWriter) {
  31. w.Header().Set("Content-Type", "application/json")
  32. w.Write([]byte("\"OK\""))
  33. }
  34. /*
  35. The paramter move function (mv)
  36. You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in
  37. r (HTTP Request Object)
  38. getParamter (string, aka $_GET['This string])
  39. Will return
  40. Paramter string (if any)
  41. Error (if error)
  42. */
  43. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  44. if postMode == false {
  45. //Access the paramter via GET
  46. keys, ok := r.URL.Query()[getParamter]
  47. if !ok || len(keys[0]) < 1 {
  48. //log.Println("Url Param " + getParamter +" is missing")
  49. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  50. }
  51. // Query()["key"] will return an array of items,
  52. // we only want the single item.
  53. key := keys[0]
  54. return string(key), nil
  55. } else {
  56. //Access the parameter via POST
  57. r.ParseForm()
  58. x := r.Form.Get(getParamter)
  59. if len(x) == 0 || x == "" {
  60. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  61. }
  62. return string(x), nil
  63. }
  64. }
  65. func stringInSlice(a string, list []string) bool {
  66. for _, b := range list {
  67. if b == a {
  68. return true
  69. }
  70. }
  71. return false
  72. }
  73. func fileExists(filename string) bool {
  74. _, err := os.Stat(filename)
  75. if os.IsNotExist(err) {
  76. return false
  77. }
  78. return true
  79. }
  80. func IsDir(path string) bool {
  81. if fileExists(path) == false {
  82. return false
  83. }
  84. fi, err := os.Stat(path)
  85. if err != nil {
  86. log.Fatal(err)
  87. return false
  88. }
  89. switch mode := fi.Mode(); {
  90. case mode.IsDir():
  91. return true
  92. case mode.IsRegular():
  93. return false
  94. }
  95. return false
  96. }
  97. func inArray(arr []string, str string) bool {
  98. for _, a := range arr {
  99. if a == str {
  100. return true
  101. }
  102. }
  103. return false
  104. }
  105. func timeToString(targetTime time.Time) string {
  106. return targetTime.Format("2006-01-02 15:04:05")
  107. }
  108. func IntToString(number int) string {
  109. return strconv.Itoa(number)
  110. }
  111. func StringToInt(number string) (int, error) {
  112. return strconv.Atoi(number)
  113. }
  114. func StringToInt64(number string) (int64, error) {
  115. i, err := strconv.ParseInt(number, 10, 64)
  116. if err != nil {
  117. return -1, err
  118. }
  119. return i, nil
  120. }
  121. func Int64ToString(number int64) string {
  122. convedNumber := strconv.FormatInt(number, 10)
  123. return convedNumber
  124. }
  125. func GetUnixTime() int64 {
  126. return time.Now().Unix()
  127. }
  128. func LoadImageAsBase64(filepath string) (string, error) {
  129. if !fileExists(filepath) {
  130. return "", errors.New("File not exists")
  131. }
  132. f, _ := os.Open(filepath)
  133. reader := bufio.NewReader(f)
  134. content, _ := ioutil.ReadAll(reader)
  135. encoded := base64.StdEncoding.EncodeToString(content)
  136. return string(encoded), nil
  137. }
  138. func PushToSliceIfNotExist(slice []string, newItem string) []string {
  139. itemExists := false
  140. for _, item := range slice {
  141. if item == newItem {
  142. itemExists = true
  143. }
  144. }
  145. if !itemExists {
  146. slice = append(slice, newItem)
  147. }
  148. return slice
  149. }
  150. func RemoveFromSliceIfExists(slice []string, target string) []string {
  151. newSlice := []string{}
  152. for _, item := range slice {
  153. if item != target {
  154. newSlice = append(newSlice, item)
  155. }
  156. }
  157. return newSlice
  158. }