leech/html/html.go

186 lines
4.5 KiB
Go

package html
import (
"fmt"
"leech/thumbnail"
"strings"
"github.com/dustin/go-humanize"
)
type Entry struct {
IsDir bool
Name string
Size int64
}
func FileListPage(req string, entries []Entry, big_preview_mode bool) string {
headerCSS1 := ""
if !big_preview_mode {
headerCSS1 = "display: flex;"
} else {
headerCSS1 = "width: 512px;"
}
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">
<head>
<!-- Stylesheet-->
<style>
@import url("https://fonts.googleapis.com/css2?family=DotGothic16&display=swap");
:root {
--text-color: #9e5272;
--text-color-accent: #793a73;
--bg-color: #181a1b;
--bg-sec-color: #f0d8e3;
color: var(--text-color);
}
body {
margin: 0;
position: relative;
font-family: "DotGothic16", sans-serif;
font-weight: 400;
font-style: normal;
background-color: var(--bg-color);
background-image: url(/assets/images/logo-64px-padded.png);
background-blend-mode: difference;
text-align: center;
font-size: 32px;
}
#content,#footer {
position: relative;
display: inline-block;
min-width: 720px;
max-width: 1280px;
width: 100vw;
}
#content {
min-height: calc(100vh - 256px);
padding: 128px 32px 32px 32px;
text-align: center;
}
#footer {
text-align: left;
margin-bottom: 64px;
}
a {
color: var(--text-color);
text-decoration: none;
font-size: 32px;
}
.entry {
%s
}
.entry-name {
margin-left: 16px;
margin-right: auto;
}
.entry-icon{
%s
image-rendering: pixelated;
}
#leech {
image-rendering: pixelated;
transform-origin: center center;
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>
<!-- Icon-->
<link rel="icon" type="image/webp" href="/assets/images/leech.png" />
<!-- Metadata-->
<meta property="og:type" content="website" />
<meta property="og:url" content="https://leech.ontake.dev" />
<meta property="og:title" content="%s | leech.ontake.dev" />
<meta name="viewport" content="width=device-width, initial-scale=0.1" />
<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>`, headerCSS1, headerCSS2, req, req)
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{}
if req != "" {
entriesWithParent = append(entriesWithParent, Entry{
IsDir: true,
Name: "..",
Size: -1,
})
}
entriesWithParent = append(entriesWithParent, entries...)
for i, dirEntry := range entriesWithParent {
formattedSize := ""
if dirEntry.Size != -1 {
formattedSize = humanize.Bytes(uint64(dirEntry.Size))
}
link := "/" + req + "/" + dirEntry.Name
if dirEntry.Name == ".." {
splitReq := strings.Split(req, "/")
link = "/" + strings.Join(splitReq[:len(splitReq)-1], "/")
}
if req == "" {
link = "/" + req + dirEntry.Name
}
if !dirEntry.IsDir {
link = "/serve/" + req + "/" + dirEntry.Name
}
icon := "/thumb/" + req + "/" + dirEntry.Name
if !thumbnail.IsSupportedFileType(dirEntry.Name) {
icon = "/assets/images/fileicon.png"
}
if dirEntry.IsDir {
icon = "/assets/images/diricon.png"
}
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
}