module_shop_act_upload_delete.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package modules
  2. import (
  3. "context"
  4. "os"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. func (this *Modules) RegisterAction_ShopUploadDelete() *Action {
  9. return this.newAction(AInfo{
  10. WantDB: true,
  11. Mount: "shop-upload-delete",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_id := utils.Trim(wrap.R.FormValue("id"))
  15. pf_file := utils.Trim(wrap.R.FormValue("file"))
  16. if !utils.IsNumeric(pf_id) {
  17. wrap.MsgError(`Inner system error`)
  18. return
  19. }
  20. if pf_file == "" {
  21. wrap.MsgError(`Inner system error`)
  22. return
  23. }
  24. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  25. // Block rows
  26. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  27. return err
  28. }
  29. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_product_images WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  30. return err
  31. }
  32. // Delete row
  33. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_product_images WHERE product_id = ? AND filename = ?;", utils.StrToInt(pf_id), pf_file); err != nil {
  34. return err
  35. }
  36. // Delete file
  37. target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + pf_file
  38. _ = os.Remove(target_file_full)
  39. // Delete thumbnails
  40. if err := wrap.RemoveProductImageThumbnails(pf_id, "thumb-*-"+pf_file); err != nil {
  41. return err
  42. }
  43. return nil
  44. }); err != nil {
  45. wrap.MsgError(err.Error())
  46. return
  47. }
  48. wrap.RecreateProductXmlFile()
  49. wrap.ResetCacheBlocks()
  50. wrap.Write(`$('#list-images a').each(function(i, e) { if($(e).attr('title') == '` + pf_file + `') { $(e).parent().remove(); return; } });`)
  51. })
  52. }