static.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package agi
  2. import (
  3. "net/url"
  4. "path/filepath"
  5. "strings"
  6. )
  7. //Return the script root of the current executing script
  8. func getScriptRoot(scriptFile string, scriptScope string) string {
  9. //Get the script root from the script path
  10. webRootAbs, _ := filepath.Abs(scriptScope)
  11. webRootAbs = filepath.ToSlash(filepath.Clean(webRootAbs) + "/")
  12. scriptFileAbs, _ := filepath.Abs(scriptFile)
  13. scriptFileAbs = filepath.ToSlash(filepath.Clean(scriptFileAbs))
  14. scriptRoot := strings.Replace(scriptFileAbs, webRootAbs, "", 1)
  15. scriptRoot = strings.Split(scriptRoot, "/")[0]
  16. return scriptRoot
  17. }
  18. //For handling special url decode in the request
  19. func specialURIDecode(inputPath string) string {
  20. inputPath = strings.ReplaceAll(inputPath, "+", "{{plus_sign}}")
  21. inputPath, _ = url.QueryUnescape(inputPath)
  22. inputPath = strings.ReplaceAll(inputPath, "{{plus_sign}}", "+")
  23. return inputPath
  24. }
  25. func specialGlob(path string) ([]string, error) {
  26. files, err := filepath.Glob(path)
  27. if err != nil {
  28. return []string{}, err
  29. }
  30. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  31. if len(files) == 0 {
  32. //Handle reverse check. Replace all [ and ] with *
  33. newSearchPath := strings.ReplaceAll(path, "[", "?")
  34. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  35. newSearchPath = strings.ReplaceAll(newSearchPath, ":", "?")
  36. //Scan with all the similar structure except [ and ]
  37. tmpFilelist, _ := filepath.Glob(newSearchPath)
  38. for _, file := range tmpFilelist {
  39. file = filepath.ToSlash(file)
  40. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  41. files = append(files, file)
  42. }
  43. }
  44. }
  45. }
  46. //Convert all filepaths to slash
  47. for i := 0; i < len(files); i++ {
  48. files[i] = filepath.ToSlash(files[i])
  49. }
  50. return files, nil
  51. }