common.go 3.9 KB

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