module_shop_act_upload_delete.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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;", utils.StrToInt(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;", utils.StrToInt(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 = ?;", utils.StrToInt(pf_id), pf_file); err != nil {
  33. return err
  34. }
  35. // Delete thumbnails
  36. if err := wrap.RemoveProductImageThumbnails(pf_id, "thumb-*-"+pf_file); err != nil {
  37. return err
  38. }
  39. // Delete file
  40. target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + pf_file
  41. if err := os.Remove(target_file_full); err != nil {
  42. return err
  43. }
  44. return nil
  45. }); err != nil {
  46. wrap.MsgError(err.Error())
  47. return
  48. }
  49. // Delete products XML cache
  50. wrap.RemoveProductXmlCacheFile()
  51. wrap.Write(`$('#list-images a').each(function(i, e) { if($(e).attr('title') == '` + pf_file + `') { $(e).parent().remove(); return; } });`)
  52. })
  53. }