// Route request based on method. func Route(ctx *web.Context) { switch ctx.R.Method { case "GET": get(ctx) case "PUT": put(ctx) case "DELETE": del(ctx) default: log.For("/API/FILE(ROUTE)", ctx.User).Warning().Printf("Unimplemented method: %s", ctx.R.Method) ctx.Reply().Status(http.StatusNotImplemented).Do() } }
// get file from storage. func get(ctx *web.Context) { lg := log.For("/API/FILE(GET)", ctx.User) path := strings.Replace(ctx.R.URL.Path, "/api/file/", "", 1) lg.Debug().Print("Attempting to get ", path) if 0 == len(path) { msg := "Format should be /api/file/path/to/my.file" lg.Warning().Print(msg) ctx.Reply().Status(http.StatusBadRequest).With(msg).Do() return } if err := file.Download(path, ctx.W); nil != err { lg.Warning().Print(err) ctx.Reply().Status(http.StatusNotFound).With(err.Error()).Do() return } // Explicit response not required. }
// put file into storage. func put(ctx *web.Context) { lg := log.For("/API/FILE(PUT)", ctx.User) path := strings.Replace(ctx.R.URL.Path, "/api/file/", "", 1) lg.Debug().Print("Attempting to put ", path) if 0 == len(path) { msg := "Format should be /api/file/path/to/my.file" lg.Warning().Print(msg) ctx.Reply().Status(http.StatusBadRequest).With(msg).Do() return } if err := file.Upload(path, ctx.R.Body); nil != err { lg.Warning().Print(err) ctx.Reply().Status(http.StatusInternalServerError).With(err.Error()).Do() return } ctx.Reply().Status(http.StatusOK).Do() }
// del (ete) file from storage. func del(ctx *web.Context) { lg := log.For("/API/FILE(DELETE)", ctx.User) path := strings.Replace(ctx.R.URL.Path, "/api/file/", "", 1) lg.Debug().Print("Attempting to delete ", path) if 0 == len(path) { msg := "Format should be /api/file/path/to/my.file" lg.Warning().Print(msg) ctx.Reply().Status(http.StatusBadRequest).With(msg).Do() return } if err := file.Delete(path); nil != err { lg.Warning().Print(err) ctx.Reply().Status(http.StatusNotFound).With(err.Error()).Do() return } ctx.Reply().Status(http.StatusOK).Do() }