package jyutil import ( "app.yhyue.com/moapp/jybase/common" "context" "fmt" "github.com/gogf/gf/v2/frame/g" "os" "path/filepath" "time" ) func GetFilePath(str, filetype string) string { var tmp = time.Now().Format("20060102150405001") var name = tmp + common.GetComplexRandom(8, 0, 0) + "." + str uploadPath := g.Cfg().MustGet(ctx, "upload_path").String() var pathStr = uploadPath + filetype + "/" + tmp[:4] + "/" + tmp[4:6] + "/" + tmp[6:8] + "/" if err := os.MkdirAll(pathStr, 0700); err != nil { g.Log().Errorf(context.Background(), "创建文件夹 %s异常 %v", pathStr, err) } return pathStr + name } func GetRequestPath(absolutePath string) string { uploadPath := g.Cfg().MustGet(ctx, "upload_path").String() lastPart := filepath.Base(uploadPath) relativePath, err := filepath.Rel(uploadPath, absolutePath) if err != nil { g.Log().Errorf(context.Background(), "获取相对路径时发生错误 %s异常 %v", absolutePath, err) return "" } return fmt.Sprintf("/%s/%s", lastPart, relativePath) }