module_api.go 6.0 KB

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