module_shop_act_upload_image.go 3.4 KB

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