common.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package common
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "time"
  11. )
  12. /*
  13. Common
  14. Some commonly used functions in ArozOS
  15. */
  16. //Response related
  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 FileExists(filename string) bool {
  65. _, err := os.Stat(filename)
  66. if os.IsNotExist(err) {
  67. return false
  68. }
  69. return true
  70. }
  71. func IsDir(path string) bool {
  72. if FileExists(path) == false {
  73. return false
  74. }
  75. fi, err := os.Stat(path)
  76. if err != nil {
  77. log.Fatal(err)
  78. return false
  79. }
  80. switch mode := fi.Mode(); {
  81. case mode.IsDir():
  82. return true
  83. case mode.IsRegular():
  84. return false
  85. }
  86. return false
  87. }
  88. func TimeToString(targetTime time.Time) string {
  89. return targetTime.Format("2006-01-02 15:04:05")
  90. }
  91. func LoadImageAsBase64(filepath string) (string, error) {
  92. if !FileExists(filepath) {
  93. return "", errors.New("File not exists")
  94. }
  95. f, _ := os.Open(filepath)
  96. reader := bufio.NewReader(f)
  97. content, _ := ioutil.ReadAll(reader)
  98. encoded := base64.StdEncoding.EncodeToString(content)
  99. return string(encoded), nil
  100. }