module_files_act_list.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package modules
  2. import (
  3. "fmt"
  4. "html"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "golang-fave/engine/assets"
  9. "golang-fave/engine/builder"
  10. "golang-fave/engine/utils"
  11. "golang-fave/engine/wrapper"
  12. )
  13. func (this *Modules) RegisterAction_FilesList() *Action {
  14. return this.newAction(AInfo{
  15. Mount: "files-list",
  16. WantAdmin: true,
  17. }, func(wrap *wrapper.Wrapper) {
  18. pf_path := utils.SafeFilePath(utils.Trim(wrap.R.FormValue("path")))
  19. // Set path
  20. wrap.Write(`fave.FilesManagerSetPath('` + pf_path + `');`)
  21. // Render table
  22. start_dir := strings.Join([]string{wrap.DHtdocs, "public"}, string(os.PathSeparator)) + pf_path + "*"
  23. str_dirs := ""
  24. str_files := ""
  25. nothing := true
  26. if files, err := filepath.Glob(start_dir); err == nil {
  27. for _, file := range files {
  28. file_name := file
  29. i := strings.LastIndex(file_name, string(os.PathSeparator))
  30. if i != -1 {
  31. file_name = file_name[i+1:]
  32. }
  33. if utils.IsDir(file) {
  34. if nothing {
  35. nothing = false
  36. }
  37. actions := builder.DataTableAction(&[]builder.DataTableActionRow{
  38. {
  39. Icon: assets.SysSvgIconView,
  40. Href: "/public" + pf_path + file_name + "/",
  41. Hint: "View",
  42. Target: "_blank",
  43. },
  44. {
  45. Icon: assets.SysSvgIconRemove,
  46. Href: "javascript:fave.FilesManagerRemoveFolder(\\'" + pf_path + file_name + "\\',\\'Are you sure want to delete folder?\\');",
  47. Hint: "Delete",
  48. Classes: "delete",
  49. },
  50. })
  51. str_dirs += `<tr class="dir"><td class="col_name"><a href="javascript:fave.FilesManagerLoadData(\'` + pf_path + file_name + `/` + `\');"><span class="text-dotted">` + html.EscapeString(file_name) + `</span></a></td><td class="col_type"><b>DIR</b></td><td class="col_action">` + actions + `</td></tr>`
  52. } else {
  53. if nothing {
  54. nothing = false
  55. }
  56. actions := builder.DataTableAction(&[]builder.DataTableActionRow{
  57. {
  58. Icon: assets.SysSvgIconView,
  59. Href: "/public" + pf_path + file_name,
  60. Hint: "View",
  61. Target: "_blank",
  62. },
  63. {
  64. Icon: assets.SysSvgIconRemove,
  65. Href: "javascript:fave.FilesManagerRemoveFile(\\'" + pf_path + file_name + "\\',\\'Are you sure want to delete file?\\');",
  66. Hint: "Delete",
  67. Classes: "delete",
  68. },
  69. })
  70. size_value := float64(utils.GetFileSize(file))
  71. size_text := "Bytes"
  72. if size_value > 1024 {
  73. size_value = size_value / 1024
  74. size_text = "Kb"
  75. }
  76. if size_value > 1024 {
  77. size_value = size_value / 1024
  78. size_text = "Mb"
  79. }
  80. if size_value > 1024 {
  81. size_value = size_value / 1024
  82. size_text = "Gb"
  83. }
  84. str_size := fmt.Sprintf("%5.2f %s", size_value, size_text)
  85. str_files += `<tr class="file"><td class="col_name"><span class="text-dotted">` + html.EscapeString(file_name) + `</span></td><td class="col_type">` + str_size + `</td><td class="col_action">` + actions + `</td></tr>`
  86. }
  87. }
  88. }
  89. if pf_path != "/" {
  90. str_dirs = `<tr class="dir"><td class="col_name"><a href="javascript:fave.FilesManagerLoadDataUp(\'` + pf_path + `\');">..</a></td><td class="col_type">&nbsp;</td><td class="col_action">&nbsp;</td></tr>` + str_dirs
  91. }
  92. str_nothing := ``
  93. if nothing {
  94. str_nothing = `<tr><td colspan="50">No data</td></tr>`
  95. }
  96. table := `<table class="table data-table table-striped table-bordered table-hover table_fm_files"><thead><tr><th class="col_name">File name</th><th class="col_type">Size</th><th class="col_action">Action</th></tr></thead><tbody>` + str_dirs + str_files + str_nothing + `</tbody></table>`
  97. wrap.Write(`$('#sys-modal-files-manager .dialog-data').html('` + table + `');`)
  98. // Enable buttons
  99. wrap.Write(`fave.FilesManagerEnableDisableButtons(false);`)
  100. })
  101. }