You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

path.go 2.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "io/ioutil"
  17. "net/url"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. )
  22. func FileExist(path string) bool {
  23. if _, err := os.Stat(path); os.IsNotExist(err) {
  24. return false
  25. }
  26. return true
  27. }
  28. func GetPath(path string) string {
  29. return filepath.Dir(path)
  30. }
  31. func EnsureFileFolderExists(path string) {
  32. p := GetPath(path)
  33. if !FileExist(p) {
  34. err := os.MkdirAll(p, os.ModePerm)
  35. if err != nil {
  36. panic(err)
  37. }
  38. }
  39. }
  40. func RemoveExt(filename string) string {
  41. return filename[:len(filename)-len(filepath.Ext(filename))]
  42. }
  43. func ListFiles(path string) []string {
  44. res := []string{}
  45. files, err := ioutil.ReadDir(path)
  46. if err != nil {
  47. panic(err)
  48. }
  49. for _, f := range files {
  50. if !f.IsDir() {
  51. res = append(res, f.Name())
  52. }
  53. }
  54. return res
  55. }
  56. func FilterQuery(urlString string, blackList []string) string {
  57. urlData, err := url.Parse(urlString)
  58. if err != nil {
  59. return urlString
  60. }
  61. queries := urlData.Query()
  62. retQuery := make(url.Values)
  63. inBlackList := false
  64. for key, value := range queries {
  65. inBlackList = false
  66. for _, blackListItem := range blackList {
  67. if blackListItem == key {
  68. inBlackList = true
  69. break
  70. }
  71. }
  72. if !inBlackList {
  73. retQuery[key] = value
  74. }
  75. }
  76. if len(retQuery) > 0 {
  77. return urlData.Path + "?" + strings.ReplaceAll(retQuery.Encode(), "%2F", "/")
  78. } else {
  79. return urlData.Path
  80. }
  81. }
  82. func CopyFile(dest string, src string) {
  83. bs, err := os.ReadFile(src)
  84. if err != nil {
  85. panic(err)
  86. }
  87. err = os.WriteFile(dest, bs, 0o644)
  88. if err != nil {
  89. panic(err)
  90. }
  91. }