path.go 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. package jyutil
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "os"
  8. "path/filepath"
  9. "time"
  10. )
  11. func GetFilePath(str, filetype string) string {
  12. var tmp = time.Now().Format("20060102150405001")
  13. var name = tmp + common.GetComplexRandom(8, 0, 0) + "." + str
  14. uploadPath := g.Cfg("global").MustGet(ctx, "upload_path").String()
  15. var pathStr = uploadPath + filetype + "/" + tmp[:4] + "/" + tmp[4:6] + "/" + tmp[6:8] + "/"
  16. if err := os.MkdirAll(pathStr, 0700); err != nil {
  17. g.Log().Errorf(context.Background(), "创建文件夹 %s异常 %v", pathStr, err)
  18. }
  19. return pathStr + name
  20. }
  21. func GetRequestPath(absolutePath string) string {
  22. uploadPath := g.Cfg("global").MustGet(ctx, "upload_path").String()
  23. lastPart := filepath.Base(uploadPath)
  24. relativePath, err := filepath.Rel(uploadPath, absolutePath)
  25. if err != nil {
  26. g.Log().Errorf(context.Background(), "获取相对路径时发生错误 %s异常 %v", absolutePath, err)
  27. return ""
  28. }
  29. return fmt.Sprintf("/%s/%s", lastPart, relativePath)
  30. }