all repos — fsrv @ de1dfeb667f0ed81e9acfc66198f7ba42b311bd3

filehost server for x.icyphox.sh

Rework to serve files ourselves
Anirudh Oppiliappan x@icyphox.sh
Sun, 27 Nov 2022 14:28:47 +0530
commit

de1dfeb667f0ed81e9acfc66198f7ba42b311bd3

parent

c2d44280e58720f9047698f846387c7d3470b603

3 files changed, 59 insertions(+), 45 deletions(-)

jump to
M go.modgo.mod

@@ -2,4 +2,7 @@ module git.icyphox.sh/fsrv

go 1.16 -require github.com/h2non/filetype v1.1.1 +require ( + github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03 + github.com/h2non/filetype v1.1.1 +)
M go.sumgo.sum

@@ -1,2 +1,4 @@

+github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03 h1:r07xZN3ENBWdxGuU/feCsnpsgHJ7+3uLm7cq9S0sqoI= +github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03/go.mod h1:1rjOQiOqQlmMdUMuvlJFjldqTnE/tQULE7qPIu4aq3U= github.com/h2non/filetype v1.1.1 h1:xvOwnXKAckvtLWsN398qS9QhlxlnVXBjXBydK2/UFB4= github.com/h2non/filetype v1.1.1/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
M main.gomain.go

@@ -11,6 +11,8 @@ "os"

"path/filepath" "strings" "time" + + "github.com/alexedwards/flow" ) type settings struct {

@@ -44,54 +46,57 @@ }

return key } -func (s *settings) uploadFile(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case "POST": - key := getKey(r) - useragent := r.Header.Get("User-Agent") - if key != s.key { - fmt.Fprintf(w, "incorrect key") - log.Printf("incorrect key: %+v", key) - return - } - r.ParseMultipartForm(512 << 20) - file, handler, err := r.FormFile("file") - if err != nil { - log.Println(err) - return - } - defer file.Close() - log.Printf("file: %+v\t%+v bytes", handler.Filename, handler.Size) +func (s *settings) upload(w http.ResponseWriter, r *http.Request) { + key := getKey(r) + useragent := r.Header.Get("User-Agent") + if key != s.key { + fmt.Fprintf(w, "incorrect key") + log.Printf("incorrect key: %+v", key) + return + } + r.ParseMultipartForm(512 << 20) + file, handler, err := r.FormFile("file") + if err != nil { + log.Println(err) + return + } + defer file.Close() + log.Printf("file: %+v\t%+v bytes", handler.Filename, handler.Size) - ext := filepath.Ext(handler.Filename) - fileBytes, err := io.ReadAll(file) - if err != nil { - log.Println(err) - } + ext := filepath.Ext(handler.Filename) + fileBytes, err := io.ReadAll(file) + if err != nil { + log.Println(err) + } - newFile := randName(5) + ext - diskFile := filepath.Join(s.storepath, newFile) - os.WriteFile(diskFile, fileBytes, 0644) - log.Printf("wrote: %+v", diskFile) - abs, err := filepath.Abs(diskFile) - if err != nil { - log.Println(err) - } - runHooks(abs) + newFile := randName(5) + ext + diskFile := filepath.Join(s.storepath, newFile) + os.WriteFile(diskFile, fileBytes, 0644) + log.Printf("wrote: %+v", diskFile) + abs, err := filepath.Abs(diskFile) + if err != nil { + log.Println(err) + } + runHooks(abs) - fileUrl := s.url + "/" + newFile - if strings.Contains(useragent, "curl/") || strings.Contains(useragent, "Igloo/") { - fmt.Fprintf(w, "%v", fileUrl) - } else { - http.Redirect(w, r, fileUrl, http.StatusSeeOther) - } - case "GET": - http.ServeFile(w, r, s.index) - default: - fmt.Fprintf(w, "unsupported method") + fileUrl := s.url + "/" + newFile + if strings.Contains(useragent, "curl/") || strings.Contains(useragent, "Igloo/") { + fmt.Fprintf(w, "%v", fileUrl) + } else { + http.Redirect(w, r, fileUrl, http.StatusSeeOther) } } +func (s *settings) indexpage(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, s.index) +} + +func (s *settings) servefile(w http.ResponseWriter, r *http.Request) { + f := flow.Param(r.Context(), "file") + log.Printf("serving file: %s", f) + http.ServeFile(w, r, filepath.Join(s.storepath, f)) +} + func (s *settings) readSettings() { flag.StringVar(&s.url, "url", "localhost", "url for fsrv to serve files") flag.StringVar(&s.addr, "addr", "0.0.0.0:9393", "address to listen on")

@@ -104,12 +109,16 @@ }

func main() { rand.Seed(time.Now().UnixNano()) + + mux := flow.New() st := settings{} st.readSettings() - http.HandleFunc("/", st.uploadFile) + mux.HandleFunc("/", st.upload, "POST") + mux.HandleFunc("/", st.indexpage, "GET") + mux.HandleFunc("/:file", st.servefile, "GET") log.Println("listening on " + st.addr) - http.ListenAndServe(st.addr, nil) + http.ListenAndServe(st.addr, mux) }