directory.go 527 B

1234567891011121314151617181920212223242526
  1. package helper
  2. import (
  3. "github.com/uozi-tech/cosy/logger"
  4. "path/filepath"
  5. "strings"
  6. )
  7. // IsUnderDirectory checks if the path is under the directory
  8. func IsUnderDirectory(path, directory string) bool {
  9. absPath, err := filepath.Abs(path)
  10. if err != nil {
  11. logger.Error(err)
  12. return false
  13. }
  14. absDirectory, err := filepath.Abs(directory)
  15. if err != nil {
  16. logger.Error(err)
  17. return false
  18. }
  19. absDirectory = filepath.Clean(absDirectory) + string(filepath.Separator)
  20. return strings.HasPrefix(absPath, absDirectory)
  21. }