static.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package wrapper
  2. import (
  3. "io"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "strings"
  8. Images "golang-fave/engine/wrapper/resources/images"
  9. Others "golang-fave/engine/wrapper/resources/others"
  10. Styles "golang-fave/engine/wrapper/resources/styles"
  11. Templates "golang-fave/engine/wrapper/resources/templates"
  12. )
  13. func (e *Wrapper) isFileExists(file string) bool {
  14. if fi, err := os.Stat(file); !os.IsNotExist(err) {
  15. if err == nil {
  16. fmode := fi.Mode()
  17. if !fmode.IsDir() {
  18. return true
  19. }
  20. }
  21. }
  22. return false
  23. }
  24. func (e *Wrapper) getFileContentType(file string, of *os.File) string {
  25. fContentType := ""
  26. if strings.HasSuffix(file, ".htm") {
  27. fContentType = "text/html"
  28. } else if strings.HasSuffix(file, ".html") {
  29. fContentType = "text/html"
  30. } else if strings.HasSuffix(file, ".txt") {
  31. fContentType = "text/plain"
  32. } else if strings.HasSuffix(file, ".php") {
  33. fContentType = "text/plain"
  34. } else if strings.HasSuffix(file, ".css") {
  35. fContentType = "text/css"
  36. } else if strings.HasSuffix(file, ".png") {
  37. fContentType = "image/png"
  38. } else if strings.HasSuffix(file, ".jpg") {
  39. fContentType = "image/jpeg"
  40. } else if strings.HasSuffix(file, ".jpeg") {
  41. fContentType = "image/jpeg"
  42. } else {
  43. fContentType = "application/octet-stream"
  44. }
  45. return fContentType
  46. }
  47. func (e *Wrapper) staticResource() bool {
  48. if e.R.URL.Path == "/assets/sys/styles.css" {
  49. (*e.W).Header().Set("Content-Type", "text/css")
  50. (*e.W).Write(Styles.File_assets_sys_styles_css)
  51. return true
  52. } else if e.R.URL.Path == "/assets/sys/logo.svg" {
  53. (*e.W).Header().Set("Content-Type", "image/svg+xml")
  54. (*e.W).Write(Others.File_assets_sys_logo_svg)
  55. return true
  56. } else if e.R.URL.Path == "/assets/sys/bg.png" {
  57. (*e.W).Header().Set("Content-Type", "image/png")
  58. (*e.W).Write(Images.File_assets_sys_bg_png)
  59. return true
  60. } else if e.R.URL.Path == "/assets/sys/logo.png" {
  61. (*e.W).Header().Set("Content-Type", "image/png")
  62. (*e.W).Write(Images.File_assets_sys_logo_png)
  63. return true
  64. } else if e.R.URL.Path == "/assets/sys/fave.ico" {
  65. (*e.W).Header().Set("Content-Type", "image/x-icon")
  66. (*e.W).Write(Others.File_assets_sys_fave_ico)
  67. return true
  68. }
  69. return false
  70. }
  71. func (e *Wrapper) staticFile() bool {
  72. file := e.R.URL.Path
  73. if file == "/" {
  74. if e.isFileExists(e.DirVhostHome + "/htdocs" + "/index.htm") {
  75. file = "/index.htm"
  76. } else if e.isFileExists(e.DirVhostHome + "/htdocs" + "/index.html") {
  77. file = "/index.html"
  78. }
  79. }
  80. if file != "/" {
  81. if e.isFileExists(e.DirVhostHome + "/htdocs" + file) {
  82. of, err := os.Open(e.DirVhostHome + "/htdocs" + file)
  83. defer of.Close()
  84. if err == nil {
  85. fstat, _ := of.Stat()
  86. fsize := strconv.FormatInt(fstat.Size(), 10)
  87. (*e.W).Header().Add("Content-Type", e.getFileContentType(file, of))
  88. (*e.W).Header().Add("Content-Length", fsize)
  89. of.Seek(0, 0)
  90. io.Copy(*e.W, of)
  91. return true
  92. }
  93. }
  94. }
  95. return false
  96. }
  97. func (e *Wrapper) printPageDefault() {
  98. (*e.W).Header().Set("Content-Type", "text/html")
  99. (*e.W).Write(Templates.PageDefault)
  100. }
  101. func (e *Wrapper) printPage404() {
  102. (*e.W).WriteHeader(http.StatusNotFound)
  103. (*e.W).Header().Set("Content-Type", "text/html")
  104. (*e.W).Write(Templates.PageError404)
  105. }