common.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package common
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strings"
  11. "time"
  12. )
  13. /*
  14. Common
  15. Some commonly used functions in ArozOS
  16. */
  17. //Response related
  18. func SendTextResponse(w http.ResponseWriter, msg string) {
  19. w.Write([]byte(msg))
  20. }
  21. //Send JSON response, with an extra json header
  22. func SendJSONResponse(w http.ResponseWriter, json string) {
  23. w.Header().Set("Content-Type", "application/json")
  24. w.Write([]byte(json))
  25. }
  26. func SendErrorResponse(w http.ResponseWriter, errMsg string) {
  27. w.Header().Set("Content-Type", "application/json")
  28. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  29. }
  30. func SendOK(w http.ResponseWriter) {
  31. w.Header().Set("Content-Type", "application/json")
  32. w.Write([]byte("\"OK\""))
  33. }
  34. /*
  35. The paramter move function (mv)
  36. You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in
  37. r (HTTP Request Object)
  38. getParamter (string, aka $_GET['This string])
  39. Will return
  40. Paramter string (if any)
  41. Error (if error)
  42. */
  43. func Mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  44. if postMode == false {
  45. //Access the paramter via GET
  46. keys, ok := r.URL.Query()[getParamter]
  47. if !ok || len(keys[0]) < 1 {
  48. //log.Println("Url Param " + getParamter +" is missing")
  49. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  50. }
  51. // Query()["key"] will return an array of items,
  52. // we only want the single item.
  53. key := keys[0]
  54. return string(key), nil
  55. } else {
  56. //Access the parameter via POST
  57. r.ParseForm()
  58. x := r.Form.Get(getParamter)
  59. if len(x) == 0 || x == "" {
  60. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  61. }
  62. return string(x), nil
  63. }
  64. }
  65. func FileExists(filename string) bool {
  66. _, err := os.Stat(filename)
  67. if os.IsNotExist(err) {
  68. return false
  69. }
  70. return true
  71. }
  72. func IsDir(path string) bool {
  73. if FileExists(path) == false {
  74. return false
  75. }
  76. fi, err := os.Stat(path)
  77. if err != nil {
  78. log.Fatal(err)
  79. return false
  80. }
  81. switch mode := fi.Mode(); {
  82. case mode.IsDir():
  83. return true
  84. case mode.IsRegular():
  85. return false
  86. }
  87. return false
  88. }
  89. func TimeToString(targetTime time.Time) string {
  90. return targetTime.Format("2006-01-02 15:04:05")
  91. }
  92. func LoadImageAsBase64(filepath string) (string, error) {
  93. if !FileExists(filepath) {
  94. return "", errors.New("File not exists")
  95. }
  96. f, _ := os.Open(filepath)
  97. reader := bufio.NewReader(f)
  98. content, _ := ioutil.ReadAll(reader)
  99. encoded := base64.StdEncoding.EncodeToString(content)
  100. return string(encoded), nil
  101. }
  102. //Use for redirections
  103. func ConstructRelativePathFromRequestURL(requestURI string, redirectionLocation string) string {
  104. if strings.Count(requestURI, "/") == 1 {
  105. //Already root level
  106. return redirectionLocation
  107. }
  108. for i := 0; i < strings.Count(requestURI, "/")-1; i++ {
  109. redirectionLocation = "../" + redirectionLocation
  110. }
  111. return redirectionLocation
  112. }