common.go 2.8 KB

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