1
0
Fork 0

Compare commits

...

17 Commits

Author SHA1 Message Date
Alexander Andreev df3ea75ca9
Added a benchmark for ScanDirectory(). 2023-09-20 04:56:08 +04:00
Alexander Andreev cbe0292e4c
Before allocation of these three vars try to read a dir first. 2023-09-20 04:55:38 +04:00
Alexander Andreev d6463e81e8
A struct DirStats was renamed to DirStat. 2023-09-20 04:07:24 +04:00
Alexander Andreev 99e39fc6c1
Added FLAGS, reorganised targets ${TARGET} and web/*.jade.go. Removed all target. Added clean .PHONY target, removed ${TARGET} from .PHONY. In web/*.jade.go added check for jade executable existence. 2023-09-20 04:06:54 +04:00
Alexander Andreev 5cc9b12030
Updated httpr to v0.3.2. 2023-09-20 04:04:09 +04:00
Alexander Andreev 8b39e60a01
Reorganised CSS. Set min-height for body to 100vh. 2023-08-23 04:49:30 +04:00
Alexander Andreev c06eaecbfc
Removed unused ids. 2023-08-23 04:40:39 +04:00
Alexander Andreev f23e4d713b
Moved error styling to main.css. 2023-08-23 04:40:23 +04:00
Alexander Andreev 1a5d32f1b9
Put my full name in a footer. 2023-08-23 04:33:45 +04:00
Alexander Andreev 775eef657e
Removed id and classes from a logo. 2023-08-23 04:31:57 +04:00
Alexander Andreev 06961fc7c4
Return an error text message if file handling is turned off. 2023-08-23 04:23:51 +04:00
Alexander Andreev 391f589a90
Version set to 23.32.0. 2023-08-13 04:06:34 +04:00
Alexander Andreev 404f88c099
Fix for double slashes. 2023-08-13 03:18:17 +04:00
Alexander Andreev 54bc8d744d
Updated HttpServer to the latest implementation. 2023-08-13 02:25:04 +04:00
Alexander Andreev 6cf74599cc
A separate base.jade template was made. An index.jade rewritten to use it. 2023-08-13 02:24:36 +04:00
Alexander Andreev 4657319d52
Added an error page. 2023-08-13 02:23:47 +04:00
Alexander Andreev c5ffe37c52
Updated httpr to 0.3.1. 2023-08-13 01:58:06 +04:00
14 changed files with 227 additions and 150 deletions

View File

@ -6,19 +6,20 @@ SYSDDIR=${SYSDDIR_:/%=%}
DESTDIR:=
PREFIX:=/usr/local
VERSION=23.24.0
VERSION=23.32.0
FLAGS=-buildmode=pie -modcacherw -mod=readonly -trimpath
LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}" -tags osusergo,netgo
all: web/*.jade.go ${TARGET}
.PHONY: run install uninstall clean
.PHONY: ${TARGET}
${TARGET}:
go build -o bin/$@ ${LDFLAGS} cmd/$@/main.go
${TARGET}: web/*.jade.go
go build -o bin/$@ ${LDFLAGS} ${FLAGS} cmd/$@/main.go
web/*.jade.go: web/templates/*.jade
ifeq (,$(wildcard $(shell go env GOPATH)/bin/jade))
go install github.com/Joker/jade/cmd/jade@latest
endif
go generate web/web.go
run:
@ -33,3 +34,7 @@ uninstall:
rm ${DESTDIR}${PREFIX}/bin/${TARGET}
rm ${DESTDIR}/${SYSDDIR}/${TARGET}.service
clean:
rm -f web/*.jade.go
go clean

View File

@ -1,6 +1,6 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=dwelling-files
pkgver=23.24.0
pkgver=23.32.0
pkgrel=1
pkgdesc="Arav's dwelling / Files"
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')

View File

@ -7,10 +7,8 @@ import (
"fmt"
"log"
"net/http"
"net/netip"
"os"
"os/signal"
"strings"
"syscall"
"git.arav.su/Arav/httpr"
@ -32,23 +30,6 @@ func main() {
return
}
var network string
if !strings.ContainsRune(*listenAddress, ':') {
network = "unix"
defer os.Remove(*listenAddress)
} else {
ap, err := netip.ParseAddrPort(*listenAddress)
if err != nil {
log.Fatalln(err)
}
if ap.Addr().Is4() {
network = "tcp4"
} else if ap.Addr().Is6() {
network = "tcp6"
}
}
hand := dwhttp.New(directoryPath, !*enableFileHandler)
r := httpr.New()
@ -58,7 +39,7 @@ func main() {
r.Handler(http.MethodGet, "/", hand.Index)
srv := dwhttp.NewHttpServer(r)
if err := srv.Start(network, *listenAddress); err != nil {
if err := srv.Start(*listenAddress); err != nil {
log.Fatalln(err)
}

2
go.mod
View File

@ -2,4 +2,4 @@ module dwelling-files
go 1.16
require git.arav.su/Arav/httpr v0.2.0
require git.arav.su/Arav/httpr v0.3.2

4
go.sum
View File

@ -1,2 +1,2 @@
git.arav.su/Arav/httpr v0.2.0 h1:rtwUVl4ZDfvMf9DeLktxvups5GDY0ARVcUuUHR7Eb9E=
git.arav.su/Arav/httpr v0.2.0/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=
git.arav.su/Arav/httpr v0.3.2 h1:a+ifu+9+FnQe6p/Kd4kgTDKAFN6zBOJjBTMjbAuHxVk=
git.arav.su/Arav/httpr v0.3.2/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=

View File

@ -6,6 +6,7 @@ import (
"dwelling-files/web"
"log"
"net/http"
"strings"
"git.arav.su/Arav/httpr"
)
@ -30,22 +31,26 @@ func New(directoryPath *string, noFileHandling bool) *FilesHandlers {
}
func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
path := "/" + httpr.Param(r, "filepath") + "/"
path := "/" + httpr.Param(r, "filepath")
if !strings.HasSuffix(path, "/") {
path += "/"
}
currentPath := files.CurrentPath(path)
entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)
if err != nil {
log.Println(err)
Error(w, http.StatusNotFound, "", "", r.Referer())
return
}
web.Index(utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
web.Index("Files", utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
}
func (h *FilesHandlers) File(w http.ResponseWriter, r *http.Request) {
if h.noFileHandling {
w.WriteHeader(http.StatusServiceUnavailable)
Error(w, http.StatusServiceUnavailable, "File handling is turned off.", "", r.Referer())
return
}
r.URL.Path = httpr.Param(r, "filepath")
@ -56,3 +61,8 @@ func RobotsTxt(w http.ResponseWriter, r *http.Request) {
fc, _ := web.AssetsGetFile("robots.txt")
w.Write(fc)
}
func Error(w http.ResponseWriter, code int, reason, message, referer string) {
w.WriteHeader(code)
web.ErrorXXX("/ "+http.StatusText(code), reason, message, referer, code, w)
}

View File

@ -5,12 +5,15 @@ import (
"log"
"net"
"net/http"
"net/netip"
"os"
"strings"
"time"
)
type HttpServer struct {
s http.Server
s http.Server
addr net.Addr
}
func NewHttpServer(r http.Handler) *HttpServer {
@ -20,7 +23,23 @@ func NewHttpServer(r http.Handler) *HttpServer {
Handler: r}}
}
func (s *HttpServer) Start(network, address string) error {
func (s *HttpServer) Start(address string) error {
var network string
if !strings.ContainsRune(address, ':') {
network = "unix"
} else {
ap, err := netip.ParseAddrPort(address)
if err != nil {
return err
}
if ap.Addr().Is4() {
network = "tcp4"
} else if ap.Addr().Is6() {
network = "tcp6"
}
}
listener, err := net.Listen(network, address)
if err != nil {
return err
@ -30,6 +49,8 @@ func (s *HttpServer) Start(network, address string) error {
os.Chmod(address, 0777)
}
s.addr = listener.Addr()
go func() {
if err = s.s.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Fatalln(err)
@ -43,6 +64,10 @@ func (s *HttpServer) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if s.addr.Network() == "unix" {
defer os.Remove(s.addr.String())
}
if err := s.s.Shutdown(ctx); err != nil {
return err
}

View File

@ -9,7 +9,7 @@ import (
const FileDateFormat = "2006-01-02 15:04:05 MST"
type DirStats struct {
type DirStat struct {
Files int64
FilesSize string
Directories int64
@ -22,16 +22,16 @@ type DirEntry struct {
Size string
}
func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStats, err error) {
var dirEntries []DirEntry = make([]DirEntry, 0)
var fileEntries []DirEntry = make([]DirEntry, 0)
var totalFilesSize int64 = 0
func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err error) {
dir, err := os.ReadDir(path)
if err != nil {
return
}
var dirEntries []DirEntry = make([]DirEntry, 0)
var fileEntries []DirEntry = make([]DirEntry, 0)
var totalFilesSize int64 = 0
for _, ent := range dir {
entry, _ := ent.Info()

13
pkg/files/files_test.go Normal file
View File

@ -0,0 +1,13 @@
package files
import "testing"
const path = "/mnt/data/music/Various"
const urlBase = "/srv/ftp/"
func BenchmarkScanDirectory(b *testing.B) {
for i := 0; i < b.N; i++ {
/*e, _, _ :=*/ ScanDirectory(path, urlBase)
// b.Log(e[len(e)-1], len(e))
}
}

View File

@ -30,6 +30,8 @@
::placeholder { color: var(--primary-color); }
.hidden { display: none; }
a {
color: var(--primary-color);
text-decoration: none; }
@ -58,8 +60,6 @@ h2 {
font-size: 1.4rem;
margin: 1rem 0; }
.hidden { display: none; }
html { margin-left: calc(100vw - 100%); }
body {
@ -69,6 +69,7 @@ body {
font-size: 1.1rem;
margin: 0 auto;
max-width: 960px;
min-height: 100vh;
width: 98%; }
header {
@ -76,24 +77,22 @@ header {
flex-wrap: wrap;
justify-content: space-between; }
#logo {
display: block;
width: 360px; }
header svg { width: 360px; }
#logo text { fill: var(--text-color); }
header svg text { fill: var(--text-color); }
#logo .logo {
header svg text:first-child {
font-size: 2rem;
font-variant-caps: small-caps;
font-weight: bold; }
header svg text:last-child { font-size: .88rem; }
@media screen and (-webkit-min-device-pixel-ratio:0) {
#logo .logo { font-size: 2.082rem; } }
header svg text:first-child { font-size: 2.082rem; } }
@-moz-document url-prefix() {
#logo .logo { font-size: 2rem; } }
#logo .under { font-size: .88rem; }
header svg text:first-child { font-size: 2rem; } }
nav { margin-top: .5rem; }
@ -105,6 +104,60 @@ nav h1 {
section { margin-top: 1rem; }
input[name="filter"] {
background-color: var(--background-color);
border: none;
border-bottom: 1px solid var(--primary-color);
color: var(--text-color);
font: inherit;
width: 100%; }
table {
overflow-y: scroll;
width: 100%; }
tr { vertical-align: top; }
tr:hover,
tr:focus-within {
background-color: var(--primary-color);
color: white; }
tr:hover a,
tr:focus-within a { color: white; }
th { text-align: left; }
th:nth-child(2),
th:last-child {
width: 1%;
white-space: nowrap; }
th,
td { line-break: strict; }
th:nth-child(2),
td:nth-child(2) { padding: 0 1rem; }
td a {
display: block;
width: 100%; }
td a:hover { transition: none; }
td:nth-child(2),
td:last-child { white-space: nowrap; }
thead tr th.clickable { cursor: pointer; }
thead tr th.clickable:hover { color: var(--secondary-color); }
thead tr th.clickable:not(.sort-up):not(.sort-down)::after { content: '⇅'; }
thead tr th.clickable.sort-up::after { content: '↑'; }
thead tr th.clickable.sort-down::after { content: '↓'; }
#overlay {
align-items: center;
background-color: var(--overlay-background-color);
@ -152,51 +205,13 @@ section { margin-top: 1rem; }
background-color: var(--background-color);
color: var(--primary-color); }
input[name="filter"] {
background-color: var(--background-color);
border: none;
border-bottom: 1px solid var(--primary-color);
color: var(--text-color);
font: inherit;
width: 100%; }
#error {
font-size: 3.5rem;
line-height: 5rem;
text-align: center;
margin: 6rem 0; }
table { overflow-y: scroll; width: 100%; }
tr { vertical-align: top; }
tr:hover,
tr:focus-within { background-color: var(--primary-color); color: white; }
tr:hover a,
tr:focus-within a { color: white; }
th { text-align: left; }
th:nth-child(2),
th:last-child { width: 1%; white-space: nowrap; }
th,
td { line-break: strict; }
th:nth-child(2),
td:nth-child(2) { padding: 0 1rem; }
td a { display: block; width: 100%; }
td a:hover { transition: none; }
td:nth-child(2),
td:last-child { white-space: nowrap; }
thead tr th.clickable { cursor: pointer; }
thead tr th.clickable:hover { color: var(--secondary-color); }
thead tr th.clickable:not(.sort-up):not(.sort-down)::after { content: '⇅'; }
thead tr th.clickable.sort-up::after { content: '↑'; }
thead tr th.clickable.sort-down::after { content: '↓'; }
#error h1 { font-size: 8rem; }
footer {
font-size: .8rem;
@ -206,7 +221,7 @@ footer {
@media screen and (max-width: 640px) {
header { display: block; }
#logo {
header svg {
margin: 0 auto;
width: 100%; }

23
web/templates/base.jade Normal file
View File

@ -0,0 +1,23 @@
doctype html
html(lang='en')
head
block head
title Arav's dwelling / #{title}
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
meta(name='theme-color' content='#cd2682')
meta(name='description' content='My file share.')
link(rel='icon' type='image/svg+xml' href='/assets/img/favicon.svg' sizes='any')
link(href='/assets/css/main.css' rel='stylesheet')
script(src='/assets/js/main.js' defer='')
body
header
svg(viewBox='0 -25 216 40')
text Arav's dwelling
text(y='11') Welcome to my sacred place, wanderer
nav
block nav
block content
footer
| 2017&mdash;2023 Alexander &quot;Arav&quot; Andreev &lt;#[a(href='mailto:me@arav.su') me@arav.su]&gt; #[a(href='/privacy') Privacy statements]

View File

@ -0,0 +1,21 @@
extends base.jade
block meta_description
meta(name='description' content=http.StatusText(code))
block nav
a(href='/') Back to index page
h1 #{http.StatusText(code)}
block content
:go:func ErrorXXX(title, reason, message, referer string, code int)
section#error
h1 #{code}
| #{http.StatusText(code)}
if reason != ""
p #{reason}
if message != ""
p #{message}
if referer != ""
section
h2 #[a(href=referer) Go back]

View File

@ -1,56 +1,39 @@
:go:func Index(mainSite, currentPath string, stats *files.DirStats, items *[]files.DirEntry, r *http.Request)
extends base.jade
:go:import "dwelling-files/pkg/files"
:go:import "dwelling-files/pkg/utils"
block nav
a(href=mainSite) Back to main website
h1 Files
doctype html
html(lang='en')
head
title Arav's dwelling / Files
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
meta(name='theme-color' content='#cd2682')
meta(name='description' content='My file share.')
link(rel='icon' type='image/svg+xml' href='/assets/img/favicon.svg' sizes='any')
link(href='/assets/css/main.css' rel='stylesheet')
script(src='/assets/js/main.js' defer='')
body
header
svg#logo(viewBox='0 -25 216 40')
text.logo Arav's dwelling
text.under(y='11') Welcome to my sacred place, wanderer
nav
a(href=mainSite) Back to main website
h1 Files
section#files
span!= currentPath
p Files: #{stats.Files} (#{stats.FilesSize}); Directories: #{stats.Directories}.
input.hidden(type="text", name="filter" placeholder="Type in to filter this directory (case insensitive)")
table
thead
tr
th Name
th Date
th Size
tr(tabindex=0)
td #[a(href="../") ../]
tbody
each item, i in *items
tr(tabindex=i+1)
td #[a(href=item.Link) #{item.Name}]
td!= utils.ToClientTimezone(item.Datetime, r).Format(files.FileDateFormat)
td= item.Size
section
span!= currentPath
section#usage
p On a page use up and down arrow keys to navigate through list. Use home and end keys to go to the start and end of a list. Use Ctrl+Backspace to return to a parent directory.
p In an overlay use a mouse wheel to change a scale of a video or a picture. Use left and right arrow keys to go through media. Use space key to toggle pause. Use escape key to close an overlay, or click outside a media. An audio volume is being kept across site using LocalStorage API.
footer
| 2017&mdash;2023 Arav &lt;#[a(href='mailto:me@arav.su') me@arav.su]&gt; #[a(href=mainSite+'/privacy') Privacy statements]
div#overlay
button(name='prev') &#10096;
block content
:go:func Index(title, mainSite, currentPath string, stats *files.DirStat, items *[]files.DirEntry, r *http.Request)
:go:import "dwelling-files/pkg/files"
:go:import "dwelling-files/pkg/utils"
section
span!= currentPath
p Files: #{stats.Files} (#{stats.FilesSize}); Directories: #{stats.Directories}.
input.hidden(type="text", name="filter" placeholder="Type in to filter this directory (case insensitive)")
table
thead
tr
th Name
th Date
th Size
tr(tabindex=0)
td #[a(href="../") ../]
tbody
each item, i in *items
tr(tabindex=i+1)
td #[a(href=item.Link) #{item.Name}]
td!= utils.ToClientTimezone(item.Datetime, r).Format(files.FileDateFormat)
td= item.Size
section
span!= currentPath
section
p On a page use up and down arrow keys to navigate through list. Use home and end keys to go to the start and end of a list. Use Ctrl+Backspace to return to a parent directory.
p In an overlay use a mouse wheel to change a scale of a video or a picture. Use left and right arrow keys to go through media. Use space key to toggle pause. Use escape key to close an overlay, or click outside a media. An audio volume is being kept across site using LocalStorage API.
div#overlay
button(name='prev') &#10096;
div
div
div
span
button(name='next') &#10097;
span
button(name='next') &#10097;

View File

@ -7,6 +7,7 @@ import (
)
//go:generate $GOPATH/bin/jade -pkg=web -stdbuf -writer templates/index.jade
//go:generate $GOPATH/bin/jade -pkg=web -stdbuf -writer templates/errorXXX.jade
//go:embed assets
var assetsDir embed.FS