module_shop_act_upload_delete.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. Mount: "shop-upload-delete",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. pf_file := utils.Trim(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(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  24. // Block rows
  25. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  26. return err
  27. }
  28. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_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(ctx, "DELETE FROM fave_shop_product_images WHERE product_id = ? AND filename = ?;", utils.StrToInt(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. // Delete thumbnails
  39. if err := wrap.RemoveProductImageThumbnails(pf_id, "thumb-*-"+pf_file); err != nil {
  40. return err
  41. }
  42. return nil
  43. }); err != nil {
  44. wrap.MsgError(err.Error())
  45. return
  46. }
  47. wrap.RecreateProductXmlFile()
  48. wrap.ResetCacheBlocks()
  49. wrap.Write(`$('#list-images a').each(function(i, e) { if($(e).attr('title') == '` + pf_file + `') { $(e).parent().remove(); return; } });`)
  50. })
  51. }