common.go 3.9 KB

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