package Drivers import ( "AnP/Models" "AnP/Utils" "encoding/json" "mime" "os" "strings" "time" ) type URLPathDriver struct { anp Models.AnPModel root_paths []string is_dos bool slash string } func NewURLPathDriver(anp Models.AnPModel) URLPathDriver { var driver URLPathDriver = URLPathDriver{ anp: anp, root_paths: []string{""}, is_dos: false, slash: "/", } var path string = GetCurrentDirectory() if path != "" { driver.is_dos = strings.Contains(path, "\\") driver.root_paths = append(driver.root_paths, path) if driver.is_dos { driver.slash = "\\" } for range 3 { path = Utils.Patterns.RE_PARENT_PATH.ReplaceAllString(path, "$1") if path == "" { break } driver.root_paths = append(driver.root_paths, path) } } return driver } func GetCurrentDirectory() string { var path string = "" executable_path, exception := os.Executable() if exception != nil { command_path, exception := os.Getwd() if exception == nil { path = command_path } } else { path = executable_path } return path } func (_self URLPathDriver) FixPath(path string) string { return Utils.Patterns.RE_SLASH.ReplaceAllString(path, _self.slash) } func (_self URLPathDriver) GetAbsolutePath(path string) string { for _, root := range _self.root_paths { var full_path string = root if full_path != "" { full_path += _self.slash } full_path = _self.FixPath(full_path + path) if _, error := os.Stat(full_path); error == nil { path = full_path break } } return path } func (_self URLPathDriver) LoadFile(path string) string { data, _ := os.ReadFile(_self.GetAbsolutePath(path)) return string(data) } func (_self URLPathDriver) LoadBinaryFile(path string) []byte { data, _ := os.ReadFile(_self.GetAbsolutePath(path)) return data } func (_self URLPathDriver) LoadJSON(inputs any, callback func(any), only_dictionaries bool) { switch group := inputs.(type) { case map[string]any: callback(group) case []any: if only_dictionaries { for _, subgroup := range group { _self.LoadJSON(subgroup, callback, only_dictionaries) } } else { callback(group) } case string: var data any if json.Unmarshal([]byte(group), &data) != nil { json.Unmarshal([]byte(_self.LoadFile(group)), &data) } _self.LoadJSON(data, callback, only_dictionaries) } } func (_self URLPathDriver) GetSlash() string { return _self.slash } func GetExtension(path string) string { var matches Utils.PatternMatch = Utils.Patterns.Match(path, Utils.Patterns.RE_EXTENSION) if matches.Ok { return matches.Groups[1] } return "" } func GetMime(path string) string { var mime_type string = mime.TypeByExtension("." + GetExtension(path)) if mime_type != "" { return mime_type } return "application/octet-stream" } func PathExists(path string) bool { _, error := os.Stat(path) return error == nil } func FileExists(path string) bool { stat, error := os.Stat(path) return error == nil && !stat.IsDir() } func DirectoryExists(path string) bool { stat, error := os.Stat(path) return error == nil && stat.IsDir() } func LastDateModified(path string) *time.Time { stat, error := os.Stat(path) if error == nil { var date time.Time = stat.ModTime() return &date } return nil }