template.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dirserv
  2. import (
  3. "fmt"
  4. "mime"
  5. "path/filepath"
  6. "strings"
  7. "imuslab.com/arozos/mod/filesystem/arozfs"
  8. "imuslab.com/arozos/mod/filesystem/hidden"
  9. )
  10. func getPageHeader(pathname string) string {
  11. return `<!DOCTYPE HTML>
  12. <html>
  13. <head>
  14. <meta name="viewport" content="width=device-width, initial-scale=0.8" />
  15. <title>Index of ` + pathname + `</title>
  16. <style>
  17. body{
  18. padding: 14px;
  19. color: #2e2e2e;
  20. font-family: Arial;
  21. }
  22. hr{
  23. border: 0px;
  24. border-top: 1px solid #e8e8e8;
  25. }
  26. td{
  27. padding-left: 8px;
  28. border-left: 1px solid #dbdbdb;
  29. word-wrap: break-word;
  30. overflow-wrap: break-word;
  31. max-width: 25vw;
  32. }
  33. td.fx{
  34. border-left: 0px;
  35. }
  36. .textfield{
  37. min-width: 60px;
  38. text-align: left;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2>Index of ` + pathname + `</h2>
  44. <hr>
  45. <table>
  46. <tr>
  47. <th></th>
  48. <th class="textfield">Name</th>
  49. <th class="textfield">Last Modify</th>
  50. <th class="textfield">Size</th>
  51. <th class="textfield"></th>
  52. </tr>
  53. `
  54. }
  55. func getItemHTML(displayText string, link string, isDir bool, modTime string, size string) string {
  56. icon := "📄"
  57. downloadBtn := ""
  58. hiddenStyle := ""
  59. if isDir {
  60. icon = "📁"
  61. isHidden, _ := hidden.IsHidden(link, true)
  62. if isHidden {
  63. //Hidden folder
  64. icon = "📁"
  65. hiddenStyle = "filter: alpha(opacity=50); opacity: 0.5; zoom: 1;"
  66. }
  67. size = "-"
  68. } else {
  69. fileMime := mime.TypeByExtension(filepath.Ext(link))
  70. if strings.HasPrefix(fileMime, "audio/") {
  71. icon = "♫"
  72. } else if strings.HasPrefix(fileMime, "video/") {
  73. icon = "🎞"
  74. } else if strings.HasPrefix(fileMime, "image/") {
  75. icon = "🖼️"
  76. } else if strings.HasPrefix(fileMime, "text/") {
  77. icon = "📝"
  78. }
  79. //fmt.Println(fileMime, filepath.Ext(displayText))
  80. //Check if hidden file
  81. isHidden, _ := hidden.IsHidden(link, true)
  82. if isHidden {
  83. //Hidden folder
  84. hiddenStyle = "filter: alpha(opacity=50); opacity: 0.5; zoom: 1;"
  85. }
  86. downloadBtn = `<a href="` + link + `" download>Download</a>`
  87. }
  88. return `<tr style="` + hiddenStyle + `">
  89. <td class="fx">` + icon + `</td>
  90. <td><a href="` + link + `">` + displayText + `</a></td>
  91. <td>` + modTime + `</td>
  92. <td>` + size + `</td>
  93. <td>` + downloadBtn + `</td>
  94. </tr>`
  95. }
  96. func getBackButton(currentPath string) string {
  97. backPath := arozfs.ToSlash(filepath.Dir(currentPath))
  98. return `<tr>
  99. <td class="fx">←</td>
  100. <td colspan="3"><a href="` + backPath + `">Back</a></td>
  101. </tr>`
  102. }
  103. func getPageFooter() string {
  104. return `</table><hr>
  105. <img src="/img/public/compatibility.png" style="display: inline-block; width: 120px;"></img>
  106. </body>
  107. </html>`
  108. }
  109. /*
  110. Utilities
  111. */
  112. func byteCountIEC(b int64) string {
  113. const unit = 1024
  114. if b < unit {
  115. return fmt.Sprintf("%d B", b)
  116. }
  117. div, exp := int64(unit), 0
  118. for n := b / unit; n >= unit; n /= unit {
  119. div *= unit
  120. exp++
  121. }
  122. return fmt.Sprintf("%.1f %ciB",
  123. float64(b)/float64(div), "KMGTPE"[exp])
  124. }