common.go 3.4 KB

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