This is still quite ugly and only meant to get things going for now, as a proof of concept. It is usable though, and quite fast. Signed-off-by: Yonah <contrib@gensokyo.uk>
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
flagFetchDirPath string
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&flagFetchDirPath, "d", "data", "Path to content-addressed store")
|
|
}
|
|
|
|
func handleDataNew(storePath string) http.HandlerFunc {
|
|
// hash string to url
|
|
urlMap := mustReadJSON[map[string]string](path.Join(storePath, "map"))
|
|
|
|
urlMapInverse := make(map[string]string, len(urlMap))
|
|
for hs, u := range urlMap {
|
|
urlMapInverse[u] = hs
|
|
}
|
|
log.Printf("inverted %d media entries", len(urlMap))
|
|
|
|
return func(writer http.ResponseWriter, request *http.Request) {
|
|
if request.URL == nil {
|
|
log.Printf("got invalid request %p", request)
|
|
return
|
|
}
|
|
|
|
fixedPath := strings.Replace(request.URL.Path, "/data/https:", "https:/", 1)
|
|
if hs, ok := urlMapInverse[fixedPath]; !ok {
|
|
verboseln("media path", fixedPath, "not found")
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
writeResp(writer, []byte("not found"))
|
|
return
|
|
} else {
|
|
http.ServeFile(writer, request, path.Join(storePath, hs))
|
|
}
|
|
}
|
|
}
|