feat: add big preview mode

This commit is contained in:
Louis Dalibard 2025-01-01 04:13:21 +01:00
parent 3833bd1951
commit 7c2fa7d6c0
2 changed files with 57 additions and 10 deletions

View File

@ -14,7 +14,28 @@ type Entry struct {
Size int64
}
func FileListPage(req string, entries []Entry) string {
func FileListPage(req string, entries []Entry, big_preview_mode bool) string {
headerCSS1 := ""
if !big_preview_mode {
headerCSS1 = "display: flex;"
} else {
headerCSS1 = "width: 320px;"
}
headerCSS2 := ""
if !big_preview_mode {
headerCSS2 = `width: 48px;height: 48px;`
} else {
headerCSS2 = `width: 256px;height: 256px;`
}
linkSuffixToggle := ""
linkSuffix := ""
if big_preview_mode {
linkSuffixToggle = ""
linkSuffix = "?big_preview=true"
} else {
linkSuffixToggle = "?big_preview=true"
linkSuffix = ""
}
header := fmt.Sprintf(`
<!DOCTYPE html>
<html lang="en">
@ -58,6 +79,7 @@ func FileListPage(req string, entries []Entry) string {
}
#footer {
text-align: left;
margin-bottom: 64px;
}
a {
color: var(--text-color);
@ -65,15 +87,15 @@ func FileListPage(req string, entries []Entry) string {
font-size: 32px;
}
.entry {
display: flex;
%s
}
.entry-name {
margin-left: 16px;
margin-right: auto;
}
.entry-icon{
width: 48px;
height: 48px;
%s
image-rendering: pixelated;
}
#leech {
image-rendering: pixelated;
@ -81,6 +103,16 @@ func FileListPage(req string, entries []Entry) string {
height:256px;
width:auto;
}
.left-entry{
margin-right: auto;
}
.row {
display: flex;
}
#toggle-big-preview-link {
text-decoration-line: underline;
text-decoration-style: wavy;
}
</style>
<!-- Title Element-->
<title>%s | leech.ontake.dev</title>
@ -94,9 +126,9 @@ func FileListPage(req string, entries []Entry) string {
<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>
</head>
<body>
<div id="content"><img src="/assets/images/leech.png" id="leech"></img>`, req, req)
<div id="content"><img src="/assets/images/leech.png" id="leech"></img>`, headerCSS1, headerCSS2, req, req)
footer := `</div><br><div id="footer">powered by leech. ontake. 2024.</div></body></html>`
footer := fmt.Sprintf(`</div><br><div id="footer"><a id="toggle-big-preview-link" href="./%s">Toggle big preview mode.</a><br><br>powered by leech. ontake. 2024.</div></body></html>`, linkSuffixToggle)
body := ""
entriesWithParent := []Entry{}
@ -108,7 +140,7 @@ func FileListPage(req string, entries []Entry) string {
})
}
entriesWithParent = append(entriesWithParent, entries...)
for _, dirEntry := range entriesWithParent {
for i, dirEntry := range entriesWithParent {
formattedSize := ""
if dirEntry.Size != -1 {
formattedSize = humanize.Bytes(uint64(dirEntry.Size))
@ -133,7 +165,21 @@ func FileListPage(req string, entries []Entry) string {
if dirEntry.IsDir {
icon = "/assets/images/diricon.png"
}
body += fmt.Sprintf(`<a href="%s"><div class="entry"><img class="entry-icon" src="%s"></img><div class="entry-name">%s</div><div class="entry-size">%s</div></div></a>`, link, icon, dirEntry.Name, formattedSize)
newLine := ""
alignCSSClass := ""
if big_preview_mode {
if i%2 == 0 {
alignCSSClass = `class="left-entry"`
if i != 0 {
newLine += "</div>"
}
newLine += "<div class=\"row\">"
}
}
body += fmt.Sprintf(`%s<a href="%s%s" %s><div class="entry"><img class="entry-icon" src="%s"></img><div class="entry-name">%s</div><div class="entry-size">%s</div></div></a>`, newLine, link, linkSuffix, alignCSSClass, icon, dirEntry.Name, formattedSize)
}
if big_preview_mode {
body += "</div>"
}
return header + body + footer
}

View File

@ -43,6 +43,7 @@ func HandleList(c *fiber.Ctx) error {
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
bigPreview := c.QueryBool("big_preview")
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
if req == "" {
keys := make([]string, 0, len(config.Config.ServeDirs))
@ -64,7 +65,7 @@ func HandleList(c *fiber.Ctx) error {
})
}
return c.SendString(html.FileListPage(req, entries))
return c.SendString(html.FileListPage(req, entries, bigPreview))
} else {
pathSlice := strings.Split(req, "/")
pathBase, ok := config.Config.ServeDirs[pathSlice[0]]
@ -94,7 +95,7 @@ func HandleList(c *fiber.Ctx) error {
IsDir: f.IsDir(),
})
}
return c.SendString(html.FileListPage(req, entries))
return c.SendString(html.FileListPage(req, entries, bigPreview))
} else {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
}