|
|
|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
|
|
|
|
|
|
"dread.land/deepgram-demo/internal/audio" |
|
|
|
|
"dread.land/deepgram-demo/internal/metadata" |
|
|
|
|
"github.com/go-chi/chi/v5" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type ApiServer struct { |
|
|
|
|
@ -18,6 +19,49 @@ type ApiServer struct { |
|
|
|
|
Metadata *metadata.MetadataServer |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (apiServer *ApiServer) chunkHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
// chunkIndex starts at 0
|
|
|
|
|
chunkIndexString := r.URL.Query().Get("chunkindex") |
|
|
|
|
chunkSizeString := r.URL.Query().Get("chunksize") |
|
|
|
|
|
|
|
|
|
chunkIndex := 0 |
|
|
|
|
var err error |
|
|
|
|
if chunkIndexString != "" { |
|
|
|
|
chunkIndex, err = strconv.Atoi(chunkIndexString) |
|
|
|
|
} |
|
|
|
|
if err != nil { |
|
|
|
|
http.Error(w, fmt.Sprint(err), http.StatusBadRequest) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
chunkSize := 0 |
|
|
|
|
if chunkSizeString != "" { |
|
|
|
|
chunkSize, err = strconv.Atoi(chunkSizeString) |
|
|
|
|
} |
|
|
|
|
if err != nil { |
|
|
|
|
http.Error(w, fmt.Sprint(err), http.StatusBadRequest) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
filename := path.Base(path.Dir(r.URL.Path)) |
|
|
|
|
|
|
|
|
|
contents, err := apiServer.Audio.Download(filename) |
|
|
|
|
if err != nil { |
|
|
|
|
http.Error(w, fmt.Sprint(err), http.StatusNotFound) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
startIndex := chunkIndex * chunkSize |
|
|
|
|
if startIndex > len(contents) { |
|
|
|
|
http.Error(w, "index out of bounds", http.StatusBadRequest) |
|
|
|
|
} |
|
|
|
|
endIndex := startIndex + chunkSize |
|
|
|
|
if endIndex > len(contents) { |
|
|
|
|
endIndex = len(contents) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
w.Write(contents[startIndex:endIndex]) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
func (apiServer *ApiServer) filesHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
filename := path.Base(r.URL.Path) |
|
|
|
|
@ -49,7 +93,7 @@ func (apiServer *ApiServer) filesHandler(w http.ResponseWriter, r *http.Request) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (apiServer *ApiServer) metadataHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
filename := path.Base(r.URL.Path) |
|
|
|
|
filename := path.Base(path.Dir(r.URL.Path)) |
|
|
|
|
switch r.Method { |
|
|
|
|
case "GET": |
|
|
|
|
// get metadata
|
|
|
|
|
@ -93,14 +137,13 @@ func (apiServer *ApiServer) listHandler(w http.ResponseWriter, r *http.Request) |
|
|
|
|
|
|
|
|
|
func (apiServer *ApiServer) Serve() { |
|
|
|
|
|
|
|
|
|
// POST and GET media
|
|
|
|
|
http.HandleFunc("/files/", apiServer.filesHandler) |
|
|
|
|
// list media
|
|
|
|
|
http.HandleFunc("/files", apiServer.listHandler) |
|
|
|
|
// list metadata (with filter)
|
|
|
|
|
http.HandleFunc("/metadata", apiServer.listHandler) |
|
|
|
|
// GET metadata
|
|
|
|
|
http.HandleFunc("/metadata/", apiServer.metadataHandler) |
|
|
|
|
r := chi.NewRouter() |
|
|
|
|
r.Get("/files", apiServer.listHandler) |
|
|
|
|
r.Get("/files/{filename}", apiServer.filesHandler) |
|
|
|
|
r.Post("/files/{filename}", apiServer.filesHandler) |
|
|
|
|
r.Get("/files/{filename}/metadata", apiServer.metadataHandler) |
|
|
|
|
// chunkNumber
|
|
|
|
|
r.Get("/files/{filename}/chunk", apiServer.chunkHandler) |
|
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":3030", nil)) |
|
|
|
|
log.Fatal(http.ListenAndServe(":3030", r)) |
|
|
|
|
} |
|
|
|
|
|