Browse Source

Delete attached images action

Vova Tkach 5 years ago
parent
commit
24b0257f89
3 changed files with 54 additions and 1 deletions
  1. 1 1
      assets/cp.scripts.js
  2. 0 0
      assets/cp.scripts.js.go
  3. 53 0
      modules/module_shop_act_upload_delete.go

+ 1 - 1
assets/cp.scripts.js

@@ -3867,8 +3867,8 @@
 						file: filename,
 					}
 				}).done(function(data) {
-					if(IsDebugMode()) console.log('done', data);
 					$(button).parent().remove();
+					if(IsDebugMode()) console.log('done', data);
 					AjaxDone(data);
 				}).fail(function(xhr, status, error) {
 					if(IsDebugMode()) console.log('fail', xhr, status, error);

File diff suppressed because it is too large
+ 0 - 0
assets/cp.scripts.js.go


+ 53 - 0
modules/module_shop_act_upload_delete.go

@@ -0,0 +1,53 @@
+package modules
+
+import (
+	"os"
+
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+func (this *Modules) RegisterAction_ShopUploadDelete() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "shop-upload-delete",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_id := wrap.R.FormValue("id")
+		pf_file := wrap.R.FormValue("file")
+
+		if !utils.IsNumeric(pf_id) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if pf_file == "" {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			// Block rows
+			if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", pf_id); err != nil {
+				return err
+			}
+			if _, err := tx.Exec("SELECT product_id FROM shop_product_images WHERE product_id = ? FOR UPDATE;", pf_id); err != nil {
+				return err
+			}
+
+			// Delete row
+			if _, err := tx.Exec("DELETE FROM shop_product_images WHERE product_id = ? AND filename = ?;", pf_id, pf_file); err != nil {
+				return err
+			}
+
+			// Delete file
+			target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + pf_file
+			os.Remove(target_file_full)
+
+			return nil
+		}); err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+	})
+}

Some files were not shown because too many files changed in this diff