module_api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package modules
  2. import (
  3. "bufio"
  4. "bytes"
  5. "html"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "golang-fave/assets"
  12. "golang-fave/engine/fetdata"
  13. "golang-fave/engine/wrapper"
  14. "golang-fave/utils"
  15. "github.com/disintegration/imaging"
  16. )
  17. func (this *Modules) api_GenerateImage(wrap *wrapper.Wrapper, width, height, color int, filename string) ([]byte, bool, string, error) {
  18. file_ext := ""
  19. if strings.ToLower(filepath.Ext(filename)) == ".png" {
  20. file_ext = "image/png"
  21. } else if strings.ToLower(filepath.Ext(filename)) == ".jpg" {
  22. file_ext = "image/jpeg"
  23. } else if strings.ToLower(filepath.Ext(filename)) == ".jpeg" {
  24. file_ext = "image/jpeg"
  25. }
  26. src, err := imaging.Open(filename)
  27. if err != nil {
  28. return []byte(""), false, file_ext, err
  29. }
  30. src = imaging.Fill(src, width, height, imaging.Center, imaging.Lanczos)
  31. var out_bytes bytes.Buffer
  32. out := bufio.NewWriter(&out_bytes)
  33. if file_ext == "image/png" {
  34. imaging.Encode(out, src, imaging.PNG)
  35. } else if file_ext == "image/jpeg" {
  36. imaging.Encode(out, src, imaging.JPEG)
  37. } else {
  38. return []byte(""), false, file_ext, nil
  39. }
  40. return out_bytes.Bytes(), true, file_ext, nil
  41. }
  42. func (this *Modules) api_GenerateXmlCurrencies(wrap *wrapper.Wrapper) string {
  43. result := ``
  44. rows, err := wrap.DB.Query(
  45. `SELECT
  46. code,
  47. coefficient
  48. FROM
  49. shop_currencies
  50. ORDER BY
  51. id ASC
  52. ;`,
  53. )
  54. if err == nil {
  55. defer rows.Close()
  56. values := make([]string, 2)
  57. scan := make([]interface{}, len(values))
  58. for i := range values {
  59. scan[i] = &values[i]
  60. }
  61. for rows.Next() {
  62. err = rows.Scan(scan...)
  63. if err == nil {
  64. result += `<currency id="` + html.EscapeString(string(values[0])) + `" rate="` + html.EscapeString(string(values[1])) + `"/>`
  65. }
  66. }
  67. }
  68. return result
  69. }
  70. func (this *Modules) api_GenerateXmlCategories(wrap *wrapper.Wrapper) string {
  71. result := ``
  72. rows, err := wrap.DB.Query(
  73. `SELECT
  74. data.id,
  75. data.user,
  76. data.name,
  77. data.alias,
  78. data.lft,
  79. data.rgt,
  80. MAX(data.parent_id) AS parent_id
  81. FROM
  82. (
  83. SELECT
  84. node.id,
  85. node.user,
  86. node.name,
  87. node.alias,
  88. node.lft,
  89. node.rgt,
  90. parent.id AS parent_id
  91. FROM
  92. shop_cats AS node,
  93. shop_cats AS parent
  94. WHERE
  95. node.lft BETWEEN parent.lft AND parent.rgt AND
  96. node.id > 1
  97. ORDER BY
  98. node.lft ASC
  99. ) AS data
  100. WHERE
  101. data.id <> data.parent_id
  102. GROUP BY
  103. data.id
  104. ORDER BY
  105. data.lft ASC
  106. ;`,
  107. )
  108. if err == nil {
  109. defer rows.Close()
  110. values := make([]string, 7)
  111. scan := make([]interface{}, len(values))
  112. for i := range values {
  113. scan[i] = &values[i]
  114. }
  115. for rows.Next() {
  116. err = rows.Scan(scan...)
  117. if err == nil {
  118. if utils.StrToInt(string(values[6])) > 1 {
  119. result += `<category id="` + html.EscapeString(string(values[0])) + `" parentId="` + html.EscapeString(string(values[6])) + `">` + html.EscapeString(string(values[2])) + `</category>`
  120. } else {
  121. result += `<category id="` + html.EscapeString(string(values[0])) + `">` + html.EscapeString(string(values[2])) + `</category>`
  122. }
  123. }
  124. }
  125. }
  126. return result
  127. }
  128. func (this *Modules) api_GenerateXmlOfferPictures(wrap *wrapper.Wrapper, product_id int) string {
  129. result := ``
  130. rows, err := wrap.DB.Query(
  131. `SELECT
  132. shop_product_images.product_id,
  133. shop_product_images.filename
  134. FROM
  135. shop_product_images
  136. WHERE
  137. shop_product_images.product_id = ?
  138. ;`,
  139. product_id,
  140. )
  141. if err == nil {
  142. defer rows.Close()
  143. values := make([]string, 2)
  144. scan := make([]interface{}, len(values))
  145. for i := range values {
  146. scan[i] = &values[i]
  147. }
  148. for rows.Next() {
  149. err = rows.Scan(scan...)
  150. if err == nil {
  151. result += `<picture>` + html.EscapeString((*wrap.Config).API.XML.Url) + `products/images/` + html.EscapeString(string(values[0])) + `/` + html.EscapeString(string(values[1])) + `</picture>`
  152. }
  153. }
  154. }
  155. return result
  156. }
  157. func (this *Modules) api_GenerateXmlOfferAttributes(wrap *wrapper.Wrapper, product_id int) string {
  158. result := ``
  159. filter_ids := []int{}
  160. filter_names := map[int]string{}
  161. filter_values := map[int][]string{}
  162. rows, err := wrap.DB.Query(
  163. `SELECT
  164. shop_filters.id,
  165. shop_filters.filter,
  166. shop_filters_values.name
  167. FROM
  168. shop_filter_product_values
  169. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  170. LEFT JOIN shop_filters ON shop_filters.id = shop_filters_values.filter_id
  171. WHERE
  172. shop_filter_product_values.product_id = ?
  173. ORDER BY
  174. shop_filters.filter ASC,
  175. shop_filters_values.name ASC
  176. ;`,
  177. product_id,
  178. )
  179. if err == nil {
  180. defer rows.Close()
  181. values := make([]string, 3)
  182. scan := make([]interface{}, len(values))
  183. for i := range values {
  184. scan[i] = &values[i]
  185. }
  186. for rows.Next() {
  187. err = rows.Scan(scan...)
  188. if err == nil {
  189. if !utils.InArrayInt(filter_ids, utils.StrToInt(string(values[0]))) {
  190. filter_ids = append(filter_ids, utils.StrToInt(string(values[0])))
  191. }
  192. filter_names[utils.StrToInt(string(values[0]))] = html.EscapeString(string(values[1]))
  193. filter_values[utils.StrToInt(string(values[0]))] = append(filter_values[utils.StrToInt(string(values[0]))], string(values[2]))
  194. }
  195. }
  196. }
  197. for _, filter_id := range filter_ids {
  198. result += `<param name="` + html.EscapeString(filter_names[filter_id]) + `">` + html.EscapeString(strings.Join(filter_values[filter_id], ", ")) + `</param>`
  199. }
  200. return result
  201. }
  202. func (this *Modules) api_GenerateXmlOffers(wrap *wrapper.Wrapper) string {
  203. result := ``
  204. rows, err := wrap.DB.Query(
  205. `SELECT
  206. shop_products.id,
  207. shop_currencies.code,
  208. shop_products.price,
  209. shop_products.name,
  210. shop_products.alias,
  211. shop_products.vendor,
  212. shop_products.quantity,
  213. shop_products.category,
  214. shop_products.content
  215. FROM
  216. shop_products
  217. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  218. WHERE
  219. shop_products.active = 1 AND
  220. shop_products.category > 1
  221. ORDER BY
  222. shop_products.id
  223. ;`,
  224. )
  225. if err == nil {
  226. defer rows.Close()
  227. values := make([]string, 9)
  228. scan := make([]interface{}, len(values))
  229. for i := range values {
  230. scan[i] = &values[i]
  231. }
  232. for rows.Next() {
  233. err = rows.Scan(scan...)
  234. if err == nil {
  235. result += `<offer id="` + html.EscapeString(string(values[0])) + `" available="true">`
  236. result += `<url>` + html.EscapeString((*wrap.Config).API.XML.Url) + `shop/` + html.EscapeString(string(values[4])) + `/</url>`
  237. result += `<price>` + utils.Float64ToStrF(utils.StrToFloat64(string(values[2])), "%.2f") + `</price>`
  238. result += `<currencyId>` + html.EscapeString(string(values[1])) + `</currencyId>`
  239. result += `<categoryId>` + html.EscapeString(string(values[7])) + `</categoryId>`
  240. result += this.api_GenerateXmlOfferPictures(wrap, utils.StrToInt(string(values[0])))
  241. result += `<vendor>` + html.EscapeString(string(values[5])) + `</vendor>`
  242. result += `<stock_quantity>` + html.EscapeString(string(values[6])) + `</stock_quantity>`
  243. result += `<name>` + html.EscapeString(string(values[3])) + `</name>`
  244. result += `<description><![CDATA[` + string(values[8]) + `]]></description>`
  245. result += this.api_GenerateXmlOfferAttributes(wrap, utils.StrToInt(string(values[0])))
  246. result += `</offer>`
  247. }
  248. }
  249. }
  250. return result
  251. }
  252. func (this *Modules) api_GenerateXml(wrap *wrapper.Wrapper) string {
  253. return `<?xml version="1.0" encoding="UTF-8"?>
  254. <!DOCTYPE yml_catalog SYSTEM "shops.dtd">
  255. <yml_catalog date="` + time.Unix(int64(time.Now().Unix()), 0).Format("2006-01-02 15:04") + `">
  256. <shop>
  257. <name>` + html.EscapeString((*wrap.Config).API.XML.Name) + `</name>
  258. <company>` + html.EscapeString((*wrap.Config).API.XML.Company) + `</company>
  259. <url>` + html.EscapeString((*wrap.Config).API.XML.Url) + `</url>
  260. <currencies>` + this.api_GenerateXmlCurrencies(wrap) + `</currencies>
  261. <categories>` + this.api_GenerateXmlCategories(wrap) + `</categories>
  262. <offers>` + this.api_GenerateXmlOffers(wrap) + `</offers>
  263. </shop>
  264. </yml_catalog>`
  265. }
  266. func (this *Modules) RegisterModule_Api() *Module {
  267. return this.newModule(MInfo{
  268. WantDB: true,
  269. Mount: "api",
  270. Name: "Api",
  271. Order: 803,
  272. System: true,
  273. Icon: assets.SysSvgIconPage,
  274. Sub: &[]MISub{},
  275. }, func(wrap *wrapper.Wrapper) {
  276. // http://localhost:8080/api/product-image/thumb-cp/1/1565650500.png/
  277. // http://localhost:8080/api/product-image/thumb-1/1/1565650500.png/
  278. // http://localhost:8080/api/product-image/thumb-2/1/1565650500.png/
  279. // http://localhost:8080/api/product-image/thumb-3/1/1565650500.png/
  280. if len(wrap.UrlArgs) == 5 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "product-image" && (wrap.UrlArgs[2] == "thumb-cp" || wrap.UrlArgs[2] == "thumb-1" || wrap.UrlArgs[2] == "thumb-2" || wrap.UrlArgs[2] == "thumb-3") {
  281. thumb_type := wrap.UrlArgs[2]
  282. product_id := wrap.UrlArgs[3]
  283. file_name := wrap.UrlArgs[4]
  284. original_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + file_name
  285. if !utils.IsFileExists(original_file) {
  286. // User error 404 page
  287. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  288. return
  289. }
  290. width := (*wrap.Config).Shop.Thumbnails.ControlPanel[0]
  291. height := (*wrap.Config).Shop.Thumbnails.ControlPanel[1]
  292. if thumb_type == "thumb-1" {
  293. width = (*wrap.Config).Shop.Thumbnails.Thumbnail1[0]
  294. height = (*wrap.Config).Shop.Thumbnails.Thumbnail1[1]
  295. } else if thumb_type == "thumb-2" {
  296. width = (*wrap.Config).Shop.Thumbnails.Thumbnail2[0]
  297. height = (*wrap.Config).Shop.Thumbnails.Thumbnail2[1]
  298. } else if thumb_type == "thumb-3" {
  299. width = (*wrap.Config).Shop.Thumbnails.Thumbnail3[0]
  300. height = (*wrap.Config).Shop.Thumbnails.Thumbnail3[1]
  301. }
  302. data, ok, ext, err := this.api_GenerateImage(wrap, width, height, 0, original_file)
  303. if err != nil {
  304. // System error 500
  305. utils.SystemErrorPageEngine(wrap.W, err)
  306. return
  307. }
  308. if !ok {
  309. // User error 404 page
  310. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  311. return
  312. }
  313. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  314. wrap.W.Header().Set("Content-Type", ext)
  315. wrap.W.Write(data)
  316. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
  317. if (*wrap.Config).API.XML.Enabled == 1 {
  318. // Fix url
  319. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  320. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  321. return
  322. }
  323. // XML
  324. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  325. wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
  326. wrap.W.WriteHeader(http.StatusOK)
  327. wrap.W.Write([]byte(this.api_GenerateXml(wrap)))
  328. } else {
  329. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  330. wrap.W.WriteHeader(http.StatusNotFound)
  331. wrap.W.Write([]byte("Disabled!"))
  332. }
  333. } else if len(wrap.UrlArgs) == 1 {
  334. // Fix url
  335. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  336. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  337. return
  338. }
  339. // Some info
  340. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  341. wrap.W.WriteHeader(http.StatusOK)
  342. wrap.W.Write([]byte("Fave engine API mount point!"))
  343. } else {
  344. // User error 404 page
  345. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  346. return
  347. }
  348. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  349. // No any page for back-end
  350. return "", "", ""
  351. })
  352. }