pathinfo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package gold
  2. import (
  3. "errors"
  4. "net"
  5. "net/url"
  6. "os"
  7. _path "path"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. )
  12. type pathInfo struct {
  13. Obj *url.URL
  14. URI string
  15. Base string
  16. Path string
  17. Root string
  18. File string
  19. FileType string
  20. ParentURI string
  21. AclURI string
  22. AclFile string
  23. MetaURI string
  24. MetaFile string
  25. Extension string
  26. MaybeRDF bool
  27. IsDir bool
  28. Exists bool
  29. ModTime time.Time
  30. Size int64
  31. }
  32. func (req *httpRequest) pathInfo(path string) (*pathInfo, error) {
  33. res := &pathInfo{}
  34. if len(path) == 0 {
  35. return nil, errors.New("missing resource path")
  36. }
  37. // hack - if source URI contains "one%2b+%2btwo" then it is
  38. // normally decoded to "one+ +two", but Go parses it to
  39. // "one+++two", so we replace the plus with a blank space
  40. // strings.Replace(path, "+", "%20", -1)
  41. p, err := url.Parse(path)
  42. if err != nil {
  43. return nil, err
  44. }
  45. res.Base = p.Scheme + "://" + p.Host
  46. res.Root = req.Server.Config.DataRoot
  47. // include host and port if running in vhosts mode
  48. host, port, _ := net.SplitHostPort(p.Host)
  49. if len(host) == 0 {
  50. host = p.Host
  51. }
  52. if len(port) > 0 {
  53. host += ":" + port
  54. }
  55. if req.Server.Config.Vhosts {
  56. res.Root = req.Server.Config.DataRoot + host + "/"
  57. res.Base = p.Scheme + "://" + host
  58. }
  59. // p.Path = p.String()[len(p.Scheme+"://"+p.Host):]
  60. if strings.HasPrefix(p.Path, "/") && len(p.Path) > 0 {
  61. p.Path = strings.TrimLeft(p.Path, "/")
  62. }
  63. if len(p.Path) == 0 {
  64. res.URI = p.String() + "/"
  65. } else {
  66. res.URI = p.String()
  67. }
  68. res.Obj = p
  69. res.File = p.Path
  70. res.Path = p.Path
  71. if req.Server.Config.Vhosts {
  72. res.File = res.Root + p.Path
  73. } else if len(req.Server.Config.DataRoot) > 0 {
  74. res.File = req.Server.Config.DataRoot + p.Path
  75. }
  76. res.Exists = true
  77. res.IsDir = false
  78. // check if file exits first
  79. if stat, err := os.Stat(res.File); os.IsNotExist(err) {
  80. res.Exists = false
  81. } else {
  82. res.ModTime = stat.ModTime()
  83. res.Size = stat.Size()
  84. // Add missing trailing slashes for dirs
  85. if stat.IsDir() {
  86. res.IsDir = true
  87. if !strings.HasSuffix(res.Path, "/") && len(res.Path) > 1 {
  88. res.Path += "/"
  89. res.File += "/"
  90. res.URI += "/"
  91. }
  92. } else {
  93. res.FileType, res.Extension, res.MaybeRDF = MimeLookup(res.File)
  94. if len(res.FileType) == 0 {
  95. res.FileType, err = GuessMimeType(res.File)
  96. if err != nil {
  97. req.Server.debug.Println(err)
  98. }
  99. }
  100. }
  101. }
  102. if len(res.Extension) == 0 {
  103. res.Extension = _path.Ext(res.File)
  104. }
  105. if strings.HasSuffix(res.Path, "/") {
  106. if filepath.Dir(filepath.Dir(res.Path)) == "." {
  107. res.ParentURI = res.Base + "/"
  108. } else {
  109. res.ParentURI = res.Base + "/" + filepath.Dir(filepath.Dir(res.Path)) + "/"
  110. }
  111. } else {
  112. res.ParentURI = res.Base + "/" + filepath.Dir(res.Path) + "/"
  113. }
  114. if strings.HasSuffix(res.Path, req.Server.Config.ACLSuffix) {
  115. res.AclURI = res.URI
  116. res.AclFile = res.File
  117. res.MetaURI = res.URI
  118. res.MetaFile = res.File
  119. } else if strings.HasSuffix(res.Path, req.Server.Config.MetaSuffix) {
  120. res.AclURI = res.URI + req.Server.Config.ACLSuffix
  121. res.AclFile = res.File + req.Server.Config.ACLSuffix
  122. res.MetaURI = res.URI
  123. res.MetaFile = res.File
  124. } else {
  125. res.AclURI = res.URI + req.Server.Config.ACLSuffix
  126. res.AclFile = res.File + req.Server.Config.ACLSuffix
  127. res.MetaURI = res.URI + req.Server.Config.MetaSuffix
  128. res.MetaFile = res.File + req.Server.Config.MetaSuffix
  129. }
  130. return res, nil
  131. }