module_shop_act_upload_image.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package modules
  2. import (
  3. "bytes"
  4. "context"
  5. "image"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. _ "image/jpeg"
  12. _ "image/png"
  13. "golang-fave/engine/utils"
  14. "golang-fave/engine/wrapper"
  15. )
  16. func (this *Modules) RegisterAction_ShopUploadImage() *Action {
  17. return this.newAction(AInfo{
  18. Mount: "shop-upload-image",
  19. WantAdmin: true,
  20. }, func(wrap *wrapper.Wrapper) {
  21. pf_id := utils.Trim(wrap.R.FormValue("id"))
  22. pf_count := utils.Trim(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. var lastID int64 = 0
  47. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  48. // Block rows
  49. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  50. return err
  51. }
  52. // Insert row
  53. res, err := tx.Exec(
  54. ctx,
  55. `INSERT INTO fave_shop_product_images SET
  56. product_id = ?,
  57. filename = ?,
  58. ord = ?
  59. ;`,
  60. utils.StrToInt(pf_id),
  61. target_file_name,
  62. (utils.GetCurrentUnixTimestamp() + int64(i-1)),
  63. )
  64. if err != nil {
  65. return err
  66. }
  67. // Get inserted post id
  68. lastID, err = res.LastInsertId()
  69. if err != nil {
  70. return err
  71. }
  72. // Write file to disk
  73. if err := ioutil.WriteFile(target_file_full, fileBytes, 0664); err != nil {
  74. return err
  75. }
  76. return nil
  77. }); err == nil {
  78. wrap.Write(`$('#list-images').append('<div class="attached-img" data-id="` + utils.Int64ToStr(lastID) + `"><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>');`)
  79. }
  80. }
  81. }
  82. }
  83. }
  84. file.Close()
  85. }
  86. }
  87. wrap.RecreateProductImgFiles()
  88. wrap.RecreateProductXmlFile()
  89. wrap.ResetCacheBlocks()
  90. })
  91. }