common.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package metadata
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "time"
  11. )
  12. /*
  13. The paramter move function (mv)
  14. You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in
  15. r (HTTP Request Object)
  16. getParamter (string, aka $_GET['This string])
  17. Will return
  18. Paramter string (if any)
  19. Error (if error)
  20. */
  21. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  22. if postMode == false {
  23. //Access the paramter via GET
  24. keys, ok := r.URL.Query()[getParamter]
  25. if !ok || len(keys[0]) < 1 {
  26. //log.Println("Url Param " + getParamter +" is missing")
  27. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  28. }
  29. // Query()["key"] will return an array of items,
  30. // we only want the single item.
  31. key := keys[0]
  32. return string(key), nil
  33. } else {
  34. //Access the parameter via POST
  35. r.ParseForm()
  36. x := r.Form.Get(getParamter)
  37. if len(x) == 0 || x == "" {
  38. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  39. }
  40. return string(x), nil
  41. }
  42. }
  43. func stringInSlice(a string, list []string) bool {
  44. for _, b := range list {
  45. if b == a {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. func fileExists(filename string) bool {
  52. _, err := os.Stat(filename)
  53. if os.IsNotExist(err) {
  54. return false
  55. }
  56. return true
  57. }
  58. func isDir(path string) bool {
  59. if fileExists(path) == false {
  60. return false
  61. }
  62. fi, err := os.Stat(path)
  63. if err != nil {
  64. log.Fatal(err)
  65. return false
  66. }
  67. switch mode := fi.Mode(); {
  68. case mode.IsDir():
  69. return true
  70. case mode.IsRegular():
  71. return false
  72. }
  73. return false
  74. }
  75. func inArray(arr []string, str string) bool {
  76. for _, a := range arr {
  77. if a == str {
  78. return true
  79. }
  80. }
  81. return false
  82. }
  83. func timeToString(targetTime time.Time) string {
  84. return targetTime.Format("2006-01-02 15:04:05")
  85. }
  86. func loadImageAsBase64(filepath string) (string, error) {
  87. if !fileExists(filepath) {
  88. return "", errors.New("File not exists")
  89. }
  90. f, _ := os.Open(filepath)
  91. reader := bufio.NewReader(f)
  92. content, _ := ioutil.ReadAll(reader)
  93. encoded := base64.StdEncoding.EncodeToString(content)
  94. return string(encoded), nil
  95. }
  96. func pushToSliceIfNotExist(slice []string, newItem string) []string {
  97. itemExists := false
  98. for _, item := range slice {
  99. if item == newItem {
  100. itemExists = true
  101. }
  102. }
  103. if !itemExists {
  104. slice = append(slice, newItem)
  105. }
  106. return slice
  107. }
  108. func removeFromSliceIfExists(slice []string, target string) []string {
  109. newSlice := []string{}
  110. for _, item := range slice {
  111. if item != target {
  112. newSlice = append(newSlice, item)
  113. }
  114. }
  115. return newSlice
  116. }