module_files_act_upload.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package modules
  2. import (
  3. "io"
  4. "os"
  5. "strings"
  6. "golang-fave/engine/utils"
  7. "golang-fave/engine/wrapper"
  8. )
  9. func (this *Modules) RegisterAction_FilesUpload() *Action {
  10. return this.newAction(AInfo{
  11. Mount: "files-upload",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_count := utils.Trim(wrap.R.FormValue("count"))
  15. pf_path := utils.Trim(wrap.R.FormValue("path"))
  16. if !utils.IsNumeric(pf_count) {
  17. wrap.MsgError(`Inner system error`)
  18. return
  19. }
  20. pf_count_int := utils.StrToInt(pf_count)
  21. if pf_count_int <= 0 {
  22. wrap.MsgError(`Inner system error`)
  23. return
  24. }
  25. if pf_path == "" {
  26. wrap.MsgError(`Please specify files path`)
  27. return
  28. }
  29. for i := 1; i <= pf_count_int; i++ {
  30. post_field_name := "file_" + utils.IntToStr(i-1)
  31. if file, handler, err := wrap.R.FormFile(post_field_name); err == nil {
  32. if handler.Filename != "" {
  33. filename := utils.SafeFilePath(pf_path + handler.Filename)
  34. target := strings.Join([]string{wrap.DHtdocs, "public"}, string(os.PathSeparator)) + filename
  35. if f, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE, 0666); err == nil {
  36. io.Copy(f, file)
  37. f.Close()
  38. }
  39. }
  40. file.Close()
  41. }
  42. }
  43. path := "/"
  44. i := strings.LastIndex(pf_path, string(os.PathSeparator))
  45. if i != -1 {
  46. path = pf_path[:i+1]
  47. }
  48. // Set path
  49. wrap.Write(`$('#sys-modal-files-manager .dialog-path span').html('` + path + `');`)
  50. // Refresh table
  51. wrap.Write(`fave.FilesManagerLoadData('` + path + `');`)
  52. })
  53. }