module_api.go 12 KB

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