module_shop_act_upload_image.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package modules
  2. import (
  3. "bytes"
  4. "image"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. _ "image/jpeg"
  10. _ "image/png"
  11. "golang-fave/engine/wrapper"
  12. "golang-fave/utils"
  13. )
  14. func (this *Modules) RegisterAction_ShopUploadImage() *Action {
  15. return this.newAction(AInfo{
  16. WantDB: true,
  17. Mount: "shop-upload-image",
  18. WantAdmin: true,
  19. }, func(wrap *wrapper.Wrapper) {
  20. pf_id := wrap.R.FormValue("id")
  21. pf_count := wrap.R.FormValue("count")
  22. if !utils.IsNumeric(pf_id) {
  23. wrap.MsgError(`Inner system error`)
  24. return
  25. }
  26. if !utils.IsNumeric(pf_count) {
  27. wrap.MsgError(`Inner system error`)
  28. return
  29. }
  30. pf_count_int := utils.StrToInt(pf_count)
  31. if pf_count_int <= 0 {
  32. wrap.MsgError(`Inner system error`)
  33. return
  34. }
  35. // Proccess all files
  36. for i := 1; i <= pf_count_int; i++ {
  37. post_field_name := "file_" + utils.IntToStr(i-1)
  38. if file, handler, err := wrap.R.FormFile(post_field_name); err == nil {
  39. if handler.Filename != "" {
  40. if fileBytes, err := ioutil.ReadAll(file); err == nil {
  41. if _, _, err := image.Decode(bytes.NewReader(fileBytes)); err == nil {
  42. if err := os.MkdirAll(wrap.DHtdocs+string(os.PathSeparator)+"products"+string(os.PathSeparator)+"images"+string(os.PathSeparator)+pf_id, os.ModePerm); err == nil {
  43. target_file_name := utils.Int64ToStr(time.Now().Unix()+int64(i-1)) + filepath.Ext(handler.Filename)
  44. target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + target_file_name
  45. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  46. // Block rows
  47. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  48. return err
  49. }
  50. // Insert row
  51. if _, err := tx.Exec(
  52. `INSERT INTO shop_product_images SET
  53. product_id = ?,
  54. filename = ?
  55. ;`,
  56. utils.StrToInt(pf_id),
  57. target_file_name,
  58. ); err != nil {
  59. return err
  60. }
  61. // Write file to disk
  62. if err := ioutil.WriteFile(target_file_full, fileBytes, 0664); err != nil {
  63. return err
  64. }
  65. return nil
  66. }); err == nil {
  67. wrap.Write(`$('#list-images').append('<div class="attached-img"><a href="/products/images/` + pf_id + `/` + target_file_name + `" title="` + target_file_name + `" target="_blank"><img src="/products/images/` + pf_id + `/thumb-0-` + target_file_name + `" onerror="fave.ShopProductsRetryImage(this);" /></a><a href="javascript:fave.ShopProductsDeleteImage(this, ` + pf_id + `, \'` + target_file_name + `\');">Delete</a></div>');`)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. file.Close()
  74. }
  75. }
  76. wrap.RecreateProductXmlFile()
  77. wrap.ResetCacheBlocks()
  78. })
  79. }