common.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package main
  2. import (
  3. "os"
  4. "log"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "errors"
  9. "encoding/base64"
  10. "bufio"
  11. "io/ioutil"
  12. "time"
  13. )
  14. /*
  15. SYSTEM COMMON FUNCTIONS
  16. This is a parsed copy of arozos core common functions
  17. Make your life of writing module much easier
  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 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 loadImageAsBase64(filepath string) (string, error){
  126. if !fileExists(filepath){
  127. return "", errors.New("File not exists")
  128. }
  129. f, _ := os.Open(filepath)
  130. reader := bufio.NewReader(f)
  131. content, _ := ioutil.ReadAll(reader)
  132. encoded := base64.StdEncoding.EncodeToString(content)
  133. return string(encoded), nil
  134. }
  135. func pushToSliceIfNotExist(slice []string, newItem string) []string {
  136. itemExists := false
  137. for _, item := range slice{
  138. if item == newItem{
  139. itemExists = true
  140. }
  141. }
  142. if !itemExists{
  143. slice = append(slice, newItem)
  144. }
  145. return slice
  146. }
  147. func removeFromSliceIfExists(slice []string, target string) []string {
  148. newSlice := []string{}
  149. for _, item := range slice{
  150. if item != target{
  151. newSlice = append(newSlice, item)
  152. }
  153. }
  154. return newSlice;
  155. }