module_shop_act_upload_image.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 := 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(func(tx *wrapper.Tx) error {
  48. // Block rows
  49. if _, err := tx.Exec("SELECT id FROM 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. `INSERT INTO shop_product_images SET
  55. product_id = ?,
  56. filename = ?,
  57. ord = ?
  58. ;`,
  59. utils.StrToInt(pf_id),
  60. target_file_name,
  61. (utils.GetCurrentUnixTimestamp() + int64(i-1)),
  62. )
  63. if err != nil {
  64. return err
  65. }
  66. // Get inserted post id
  67. lastID, err = res.LastInsertId()
  68. if err != nil {
  69. return err
  70. }
  71. // Write file to disk
  72. if err := ioutil.WriteFile(target_file_full, fileBytes, 0664); err != nil {
  73. return err
  74. }
  75. return nil
  76. }); err == nil {
  77. 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>');`)
  78. }
  79. }
  80. }
  81. }
  82. }
  83. file.Close()
  84. }
  85. }
  86. wrap.RecreateProductImgFiles()
  87. wrap.RecreateProductXmlFile()
  88. wrap.ResetCacheBlocks()
  89. })
  90. }