module_shop_act_upload_image.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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="WaitForFave(function(){fave.ShopProductsRetryImage(this);});" /></a><a class="remove" href="javascript:fave.ShopProductsDeleteImage(this, ` + pf_id + `, \'` + target_file_name + `\');"><svg viewBox="1 1 11 14" width="10" height="12" class="sicon" version="1.1"><path fill-rule="evenodd" d="M11 2H9c0-.55-.45-1-1-1H5c-.55 0-1 .45-1 1H2c-.55 0-1 .45-1 1v1c0 .55.45 1 1 1v9c0 .55.45 1 1 1h7c.55 0 1-.45 1-1V5c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 12H3V5h1v8h1V5h1v8h1V5h1v8h1V5h1v9zm1-10H2V3h9v1z"></path></svg></a></div>');`)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. file.Close()
  74. }
  75. }
  76. wrap.RecreateProductImgFiles()
  77. wrap.RecreateProductXmlFile()
  78. wrap.ResetCacheBlocks()
  79. })
  80. }