module_templates_act_delete_file.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package modules
  2. import (
  3. "context"
  4. "os"
  5. "strings"
  6. "golang-fave/engine/utils"
  7. "golang-fave/engine/wrapper"
  8. )
  9. func (this *Modules) RegisterAction_TemplatesDeleteThemeFile() *Action {
  10. return this.newAction(AInfo{
  11. Mount: "templates-delete-file",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_file := utils.Trim(wrap.R.FormValue("file"))
  15. if pf_file == "" {
  16. wrap.MsgError(`Please specify file name`)
  17. return
  18. }
  19. if wrap.IsSystemMountedTemplateFile(pf_file) {
  20. wrap.MsgError(`You can't delete system mounted template`)
  21. return
  22. }
  23. template_file := wrap.DTemplate + string(os.PathSeparator) + pf_file
  24. if !utils.IsFileExists(template_file) {
  25. wrap.MsgError(`File is not exists`)
  26. return
  27. }
  28. if err := os.Remove(template_file); err != nil {
  29. wrap.MsgError(err.Error())
  30. return
  31. }
  32. // Update pages
  33. tmpl_name := pf_file
  34. if i := strings.LastIndex(tmpl_name, "."); i > -1 {
  35. tmpl_name = tmpl_name[:i]
  36. }
  37. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  38. _, err := tx.Exec(
  39. ctx,
  40. `UPDATE fave_pages SET
  41. template = ?
  42. WHERE
  43. template = ?
  44. ;`,
  45. "page",
  46. tmpl_name,
  47. )
  48. if err != nil {
  49. return err
  50. }
  51. return nil
  52. }); err != nil {
  53. wrap.MsgError(err.Error())
  54. return
  55. }
  56. wrap.ResetCacheBlocks()
  57. wrap.Write(`window.location='/cp/templates/';`)
  58. })
  59. }