module_templates_act_edit_theme_file.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package modules
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. func (this *Modules) RegisterAction_TemplatesEditThemeFile() *Action {
  9. return this.newAction(AInfo{
  10. Mount: "templates-edit-theme-file",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_file := utils.Trim(wrap.R.FormValue("file"))
  14. pf_content := wrap.R.FormValue("content")
  15. if pf_file == "" {
  16. wrap.MsgError(`Please specify file name`)
  17. return
  18. }
  19. template_file := wrap.DTemplate + string(os.PathSeparator) + pf_file
  20. if !utils.IsFileExists(template_file) {
  21. wrap.MsgError(`File is not exists`)
  22. return
  23. }
  24. if utils.IsDir(template_file) {
  25. wrap.MsgError(`It's not regular file`)
  26. return
  27. }
  28. files := this.templates_GetThemeFiles(wrap)
  29. if len(files) <= 0 {
  30. wrap.MsgError(`No any file found in theme folder`)
  31. return
  32. }
  33. if !utils.InArrayString(files, pf_file) {
  34. wrap.MsgError(`File is not found`)
  35. return
  36. }
  37. // Save content to file
  38. err := ioutil.WriteFile(template_file, []byte(pf_content), 0664)
  39. if err != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. wrap.ResetCacheBlocks()
  44. // Reload current page
  45. wrap.Write(`window.location.reload(false);`)
  46. })
  47. }