module_api.go 5.7 KB

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