Just WebDAV

This commit is contained in:
Neale Pickett 2023-03-04 11:49:39 -07:00
parent a896abbbe0
commit 0c94a7da09
4 changed files with 40 additions and 63 deletions

View File

@ -1,11 +1,15 @@
FROM golang:1-bullseye AS build FROM golang:1 AS build
RUN apt-get update
RUN apt-get -y --no-install-recommends install libavutil-dev libswscale-dev libavformat-dev libgraphicsmagick-dev
WORKDIR /src WORKDIR /src
COPY go.* ./ COPY go.* ./
COPY pkg ./pkg/ COPY pkg ./pkg/
COPY cmd ./cmd/ COPY cmd ./cmd/
COPY web /web
RUN go get ./... RUN go get ./...
RUN go install ./... RUN CGO_ENABLED=0 GOOS=linux go install ./...
ENTRYPOINT ["/go/bin/simpleauth"]
FROM alpine AS runtime
WORKDIR /target
COPY --from=build /go/bin/ .
FROM scratch
COPY --from=runtime /target /
ENTRYPOINT ["/webfs"]

21
LICENSE.md Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Neale Pickett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.

8
README.md Normal file
View File

@ -0,0 +1,8 @@
# webfs
It's a WebDAV server.
That's all it does. It serves WebDAV.
I front-end it with Caddy,
which handles authentication and access controls.

View File

@ -1,73 +1,17 @@
package main package main
import ( import (
"bytes"
"context"
"flag" "flag"
"fmt"
"github.com/bakape/thumbnailer"
"golang.org/x/net/webdav" "golang.org/x/net/webdav"
"log" "log"
"net/http" "net/http"
"os"
"path"
"path/filepath"
"strconv"
) )
type Handler struct {
webdav.Handler
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
width, _ := strconv.Atoi(req.FormValue("w"))
height, _ := strconv.Atoi(req.FormValue("h"))
if (width > 0) || (height > 0) {
h.ServeThumbnail(w, req, uint(width), uint(height))
return
}
h.Handler.ServeHTTP(w, req)
}
func (h *Handler) ServeThumbnail(w http.ResponseWriter, req *http.Request, width uint, height uint) {
srcPath := path.Clean(req.URL.Path)
srcFile, err := h.FileSystem.OpenFile(context.Background(), srcPath, os.O_RDONLY, 0664)
if err != nil {
http.NotFound(w, req)
return
}
defer srcFile.Close()
srcFileInfo, err := srcFile.Stat()
if err != nil {
http.NotFound(w, req)
return
}
var opts thumbnailer.Options
opts.ThumbDims.Width = width
opts.ThumbDims.Height = height
_, thumb, err := thumbnailer.Process(srcFile, opts)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ext := "jpeg"
if thumb.IsPNG {
ext = "png"
}
fileName := filepath.Base(srcPath)
thumbFileName := fmt.Sprintf("%s.thumb.%s", fileName, ext)
thumbRs := bytes.NewReader(thumb.Image.Data)
http.ServeContent(w, req, thumbFileName, srcFileInfo.ModTime(), thumbRs)
}
func main() { func main() {
address := flag.String("a", "localhost:8080", "Address to listen to.") address := flag.String("a", "localhost:8080", "Address to listen to.")
flag.Parse() flag.Parse()
handler := &Handler{} handler := &webdav.Handler{}
handler.FileSystem = webdav.Dir(".") handler.FileSystem = webdav.Dir(".")
handler.LockSystem = webdav.NewMemLS() handler.LockSystem = webdav.NewMemLS()