module_shop_act_upload_image.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if !utils.IsNumeric(pf_id) {
  22. wrap.MsgError(`Inner system error`)
  23. return
  24. }
  25. // Read file from request
  26. file, handler, err := wrap.R.FormFile("file")
  27. if err != nil {
  28. wrap.MsgError(err.Error())
  29. return
  30. }
  31. defer file.Close()
  32. // Check file name
  33. if handler.Filename == "" {
  34. wrap.MsgError(`Inner system error`)
  35. return
  36. }
  37. // Read file to bytes
  38. fileBytes, err := ioutil.ReadAll(file)
  39. if err != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. // Check if file is really an image
  44. if _, _, err := image.Decode(bytes.NewReader(fileBytes)); err != nil {
  45. wrap.MsgError(err.Error())
  46. return
  47. }
  48. // Create dirs
  49. if err := os.MkdirAll(wrap.DHtdocs+string(os.PathSeparator)+"products"+string(os.PathSeparator)+"images"+string(os.PathSeparator)+pf_id, os.ModePerm); err != nil {
  50. wrap.MsgError(err.Error())
  51. return
  52. }
  53. target_file_name := utils.Int64ToStr(time.Now().Unix()) + filepath.Ext(handler.Filename)
  54. target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + target_file_name
  55. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  56. // Block rows
  57. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  58. return err
  59. }
  60. // Insert row
  61. if _, err := tx.Exec(
  62. `INSERT INTO shop_product_images SET
  63. product_id = ?,
  64. filename = ?
  65. ;`,
  66. utils.StrToInt(pf_id),
  67. target_file_name,
  68. ); 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.MsgError(err.Error())
  78. return
  79. }
  80. wrap.Write(`$('#list-images').append('<div class="attached-img"><a href="/products/images/` + pf_id + `/` + target_file_name + `" target="_blank">` + target_file_name + `</a>, <a href="javascript:fave.ShopProductsDeleteImage(this, ` + pf_id + `, \'` + target_file_name + `\');">Delete</a></div>');`)
  81. })
  82. }