leech/html/html.go

140 lines
3.4 KiB
Go
Raw Normal View History

2024-04-27 12:16:43 +02:00
package html
import (
"fmt"
2024-06-10 21:36:26 +02:00
"leech/thumbnail"
2024-04-27 12:16:43 +02:00
"strings"
"github.com/dustin/go-humanize"
)
type Entry struct {
IsDir bool
Name string
Size int64
}
func FileListPage(req string, entries []Entry) string {
2024-04-27 13:59:53 +02:00
header := fmt.Sprintf(`
2024-04-27 12:16:43 +02:00
<!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);
2024-05-25 23:24:13 +02:00
background-image: url(/assets/images/logo-64px-padded.png);
2024-04-27 12:16:43 +02:00
background-blend-mode: difference;
text-align: center;
2024-06-10 22:10:25 +02:00
font-size: 32px;
2024-04-27 12:16:43 +02:00
}
#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;
}
a {
color: var(--text-color);
text-decoration: none;
2024-06-10 22:10:25 +02:00
font-size: 32px;
2024-04-27 12:16:43 +02:00
}
.entry {
display: flex;
}
.entry-name {
2024-06-10 22:10:25 +02:00
margin-left: 16px;
2024-04-27 12:16:43 +02:00
margin-right: auto;
}
2024-06-10 22:13:43 +02:00
.entry-icon{
width: 48px;
height: 48px;
}
2024-04-27 12:16:43 +02:00
#leech {
image-rendering: pixelated;
transform-origin: center center;
height:256px;
width:auto;
}
</style>
<!-- Title Element-->
2024-04-27 13:59:53 +02:00
<title>%s | leech.ontake.dev</title>
2024-04-27 12:16:43 +02:00
<!-- 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" />
2024-04-27 13:59:53 +02:00
<meta property="og:title" content="%s | leech.ontake.dev" />
2024-04-28 00:45:42 +02:00
<meta name="viewport" content="width=device-width, initial-scale=0.1" />
2024-04-27 21:11:51 +02:00
<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>
2024-04-27 12:16:43 +02:00
</head>
<body>
2024-04-27 13:59:53 +02:00
<div id="content"><img src="/assets/images/leech.png" id="leech"></img>`, req, req)
2024-04-27 12:16:43 +02:00
2024-06-10 22:17:13 +02:00
footer := `</div><br><div id="footer">powered by leech. ontake. 2024.</div></body></html>`
2024-04-27 12:16:43 +02:00
body := ""
entriesWithParent := []Entry{}
if req != "" {
entriesWithParent = append(entriesWithParent, Entry{
IsDir: true,
Name: "..",
Size: -1,
})
}
entriesWithParent = append(entriesWithParent, entries...)
for _, dirEntry := range entriesWithParent {
formattedSize := ""
if dirEntry.Size != -1 {
formattedSize = humanize.Bytes(uint64(dirEntry.Size))
}
2024-06-10 21:36:26 +02:00
2024-04-28 00:45:42 +02:00
link := "/" + req + "/" + dirEntry.Name
2024-04-27 12:16:43 +02:00
if dirEntry.Name == ".." {
2024-04-28 00:45:42 +02:00
splitReq := strings.Split(req, "/")
link = "/" + strings.Join(splitReq[:len(splitReq)-1], "/")
2024-04-27 12:16:43 +02:00
}
if req == "" {
link = "/" + req + dirEntry.Name
}
if !dirEntry.IsDir {
2024-04-28 00:45:42 +02:00
link = "/serve/" + req + "/" + dirEntry.Name
2024-04-27 12:16:43 +02:00
}
2024-06-10 21:36:26 +02:00
icon := "/thumb/" + req + "/" + dirEntry.Name
if !thumbnail.IsSupportedFileType(dirEntry.Name) {
icon = "/assets/images/fileicon.png"
}
if dirEntry.IsDir {
icon = "/assets/images/diricon.png"
}
2024-06-10 22:13:43 +02:00
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)
2024-04-27 12:16:43 +02:00
}
return header + body + footer
}