module_api.go 6.3 KB

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