module_api.go 6.3 KB

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