Newer
Older
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017-2018 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package v1
import (
"net/url"
"gitlab.servus.at/autoradio/tank/importer"
"gitlab.servus.at/autoradio/tank/store"
)
func (api *API) ListFilesOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// TODO: implement pagination
files, err := api.store.ListFiles(vars["group-id"])
if err != nil {
sendError(w, err)
return
}
sendWebResponse(w, http.StatusOK, FilesListing{files})
func (api *API) CreateFileForGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
request := &FileCreateRequest{}
err := json.NewDecoder(r.Body).Decode(request)
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "error decoding request: " + err.Error()})
if request.SourceURI == "" {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "source-uri is mandatory"})
return
srcURI, err := url.Parse(request.SourceURI)
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "source-uri is invalid: " + err.Error()})
return
}
q := r.URL.Query()
waitFor := q.Get("wait-for")
switch waitFor {
case "running":
case "done":
case "":
default:
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "unable to wait for unknown state: " + waitFor})
return
}
file := &store.File{}
file.Source.URI = request.SourceURI
if file, err = api.store.CreateFile(group, *file); err != nil {
sendError(w, err)
user := "anonymous" // TODO: get this from auth middleware
refID := "" // TODO: get this from query paremeter
job, err := api.importer.CreateJob(group, file.ID, *srcURI, user, refID)
// shall we remove the file here... thinking...
sendError(w, err)
if err = job.Start(context.Background(), 3*time.Hour); err != nil { // TODO: hardcoded value
// shall we remove the file here... thinking...
sendError(w, err)
return
}
if waitFor == "" {
sendWebResponse(w, http.StatusCreated, file)
return
}
switch waitFor {
case "running":
<-job.Running()
case "done":
<-job.Done()
}
if file, err = api.store.GetFile(group, file.ID); err != nil {
sendError(w, err)
return
}
sendWebResponse(w, http.StatusCreated, file)
func (api *API) ReadFileOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["file-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
return
}
file, err := api.store.GetFile(vars["group-id"], id)
if err != nil {
sendError(w, err)
return
}
sendWebResponse(w, http.StatusOK, file)
func (api *API) PatchFileOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["file-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
return
}
data := make(map[string]string)
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "error decoding request: " + err.Error()})
return
}
file, err := api.store.UpdateFileMetadata(vars["group-id"], id, data)
if err != nil {
sendError(w, err)
return
}
sendWebResponse(w, http.StatusOK, file)
})
}
func (api *API) DeleteFileOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["file-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
if job, err := api.importer.GetJob(vars["group-id"], id); err != importer.ErrNotFound {
job.Cancel()
}
if err = api.store.DeleteFile(vars["group-id"], id); err != nil {
sendError(w, err)
func (api *API) ReadUsageOfFile() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := idFromString(vars["file-id"])
if err != nil {
sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
return
}
result := FileUsageListing{}
if result.Usage.Playlists, err = api.store.GetFileUsage(vars["group-id"], id); err != nil {
sendError(w, err)
return
}
sendWebResponse(w, http.StatusOK, result)
})
}