common.go 3.2 KB

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