diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -11,6 +11,8 @@ import ( "path" "path/filepath" "flag" + + "github.com/google/uuid" ) //go:embed templates @@ -35,7 +37,19 @@ func serve(w http.ResponseWriter, token string, views ...string) { func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - serve(w, handler.token, "templates/index.html") + if r.URL.Path == "/" { + serve(w, handler.token, "templates/index.html") + } else { + resourceId := path.Base(r.URL.Path) + f, err := os.Open(filepath.Join(handler.dataPath, resourceId)) + if err != nil { + log.Println(err) + fmt.Fprint(w, err.Error()) + w.WriteHeader(http.StatusBadRequest) + return + } + io.Copy(w, f) + } return case http.MethodPost: token := r.Header.Get("X-Upload-Token") @@ -44,7 +58,14 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) return } - filename := filepath.Join(handler.dataPath, path.Base(r.URL.Path)) + u, err := uuid.NewRandom() + if err != nil { + log.Println(err) + fmt.Fprint(w, err.Error()) + w.WriteHeader(http.StatusInternalServerError) + return + } + filename := filepath.Join(handler.dataPath, u.String()) log.Printf("boxing %s\n", filename) f, err := os.Create(filename) if err != nil { @@ -55,6 +76,7 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } defer r.Body.Close() io.Copy(f, r.Body) + w.Header().Add("X-Resource-ID", filepath.Base(filename)) log.Printf("boxed %s\n", filename) default: w.WriteHeader(http.StatusMethodNotAllowed) |
