template.go 2.6 KB

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