module_templates_act_create_theme_file.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_TemplatesCreateThemeFile() *Action {
  9. return this.newAction(AInfo{
  10. Mount: "templates-create-theme-file",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_name := utils.Trim(wrap.R.FormValue("name"))
  14. pf_content := wrap.R.FormValue("content")
  15. if pf_name == "" {
  16. wrap.MsgError(`Please specify file name`)
  17. return
  18. }
  19. if !utils.IsValidTemplateFileName(pf_name) {
  20. wrap.MsgError(`Bad template file name`)
  21. return
  22. }
  23. template_file := wrap.DTemplate + string(os.PathSeparator) + pf_name + ".html"
  24. if utils.IsFileExists(template_file) {
  25. wrap.MsgError(`File is already exists`)
  26. return
  27. }
  28. // Save content to file
  29. err := ioutil.WriteFile(template_file, []byte(pf_content), 0664)
  30. if err != nil {
  31. wrap.MsgError(err.Error())
  32. return
  33. }
  34. wrap.ResetCacheBlocks()
  35. wrap.Write(`window.location='/cp/templates/?file=` + pf_name + `.html';`)
  36. })
  37. }