module_shop_act_upload_delete.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package modules
  2. import (
  3. "os"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_ShopUploadDelete() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "shop-upload-delete",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := wrap.R.FormValue("id")
  14. pf_file := wrap.R.FormValue("file")
  15. if !utils.IsNumeric(pf_id) {
  16. wrap.MsgError(`Inner system error`)
  17. return
  18. }
  19. if pf_file == "" {
  20. wrap.MsgError(`Inner system error`)
  21. return
  22. }
  23. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  24. // Block rows
  25. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", pf_id); err != nil {
  26. return err
  27. }
  28. if _, err := tx.Exec("SELECT product_id FROM shop_product_images WHERE product_id = ? FOR UPDATE;", pf_id); err != nil {
  29. return err
  30. }
  31. // Delete row
  32. if _, err := tx.Exec("DELETE FROM shop_product_images WHERE product_id = ? AND filename = ?;", pf_id, pf_file); err != nil {
  33. return err
  34. }
  35. // Delete file
  36. target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + pf_file
  37. os.Remove(target_file_full)
  38. return nil
  39. }); err != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. })
  44. }