module_api.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package modules
  2. import (
  3. "net/http"
  4. "os"
  5. "golang-fave/assets"
  6. "golang-fave/engine/fetdata"
  7. "golang-fave/engine/wrapper"
  8. "golang-fave/utils"
  9. )
  10. func (this *Modules) RegisterModule_Api() *Module {
  11. return this.newModule(MInfo{
  12. WantDB: true,
  13. Mount: "api",
  14. Name: "Api",
  15. Order: 803,
  16. System: true,
  17. Icon: assets.SysSvgIconPage,
  18. Sub: &[]MISub{},
  19. }, func(wrap *wrapper.Wrapper) {
  20. if len(wrap.UrlArgs) == 5 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "product-image" && (wrap.UrlArgs[2] == "thumb-0" || wrap.UrlArgs[2] == "thumb-1" || wrap.UrlArgs[2] == "thumb-2" || wrap.UrlArgs[2] == "thumb-3" || wrap.UrlArgs[2] == "thumb-full") {
  21. thumb_type := wrap.UrlArgs[2]
  22. product_id := wrap.UrlArgs[3]
  23. file_name := wrap.UrlArgs[4]
  24. original_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + file_name
  25. if !utils.IsFileExists(original_file) {
  26. // User error 404 page
  27. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  28. return
  29. }
  30. width := (*wrap.Config).Shop.Thumbnails.Thumbnail0[0]
  31. height := (*wrap.Config).Shop.Thumbnails.Thumbnail0[1]
  32. resize := false
  33. if thumb_type == "thumb-1" {
  34. width = (*wrap.Config).Shop.Thumbnails.Thumbnail1[0]
  35. height = (*wrap.Config).Shop.Thumbnails.Thumbnail1[1]
  36. if (*wrap.Config).Shop.Thumbnails.Thumbnail1[2] == 1 {
  37. resize = true
  38. }
  39. } else if thumb_type == "thumb-2" {
  40. width = (*wrap.Config).Shop.Thumbnails.Thumbnail2[0]
  41. height = (*wrap.Config).Shop.Thumbnails.Thumbnail2[1]
  42. if (*wrap.Config).Shop.Thumbnails.Thumbnail2[2] == 1 {
  43. resize = true
  44. }
  45. } else if thumb_type == "thumb-3" {
  46. width = (*wrap.Config).Shop.Thumbnails.Thumbnail3[0]
  47. height = (*wrap.Config).Shop.Thumbnails.Thumbnail3[1]
  48. if (*wrap.Config).Shop.Thumbnails.Thumbnail3[2] == 1 {
  49. resize = true
  50. }
  51. } else if thumb_type == "thumb-full" {
  52. width = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[0]
  53. height = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[1]
  54. if (*wrap.Config).Shop.Thumbnails.ThumbnailFull[2] == 1 {
  55. resize = true
  56. }
  57. }
  58. target_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + thumb_type + "-" + file_name
  59. if !utils.IsFileExists(target_file) {
  60. data, ok, ext, err := this.api_GenerateImage(wrap, width, height, resize, original_file)
  61. if err != nil {
  62. // System error 500
  63. utils.SystemErrorPageEngine(wrap.W, err)
  64. return
  65. }
  66. if !ok {
  67. // User error 404 page
  68. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  69. return
  70. }
  71. // Save file
  72. if file, err := os.Create(target_file); err == nil {
  73. file.Write(data)
  74. }
  75. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  76. wrap.W.Header().Set("Content-Type", ext)
  77. wrap.W.Write(data)
  78. } else {
  79. http.ServeFile(wrap.W, wrap.R, target_file)
  80. }
  81. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
  82. if (*wrap.Config).API.XML.Enabled == 1 {
  83. // Fix url
  84. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  85. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  86. return
  87. }
  88. // XML
  89. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  90. wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
  91. wrap.W.WriteHeader(http.StatusOK)
  92. wrap.W.Write([]byte(this.api_GenerateXml(wrap)))
  93. } else {
  94. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  95. wrap.W.WriteHeader(http.StatusNotFound)
  96. wrap.W.Write([]byte("Disabled!"))
  97. }
  98. } else if len(wrap.UrlArgs) == 1 {
  99. // Fix url
  100. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  101. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  102. return
  103. }
  104. // Some info
  105. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  106. wrap.W.WriteHeader(http.StatusOK)
  107. wrap.W.Write([]byte("Fave engine API mount point!"))
  108. } else {
  109. // User error 404 page
  110. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  111. return
  112. }
  113. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  114. // No any page for back-end
  115. return "", "", ""
  116. })
  117. }