Add remove torrent feature

This commit is contained in:
Lucas BEE 2017-05-30 14:38:40 +02:00
parent e53306686f
commit 7a7a5ea93c
3 changed files with 34 additions and 2 deletions

View File

@ -4,6 +4,9 @@ import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/gorilla/mux"
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth"
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/users"
@ -43,7 +46,7 @@ func DownloadHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error
return env.RenderOK(w, "Torrent added")
}
// ListHandler downloads a movie via polochon
// ListHandler downloads a torrent via polochon
func ListHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
v := auth.GetCurrentUser(r, env.Log)
user, ok := v.(*users.User)
@ -63,3 +66,31 @@ func ListHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
return env.RenderJSON(w, torrents)
}
// RemoveHandler removes a torrents via polochon
func RemoveHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
v := auth.GetCurrentUser(r, env.Log)
user, ok := v.(*users.User)
if !ok {
return env.RenderError(w, errors.New("invalid user type"))
}
// Get the torrent ID from the URL
vars := mux.Vars(r)
torrentID, err := strconv.Atoi(vars["id"])
if err != nil {
return env.RenderError(w, errors.New("invalid argument"))
}
client, err := user.NewPapiClient()
if err != nil {
return env.RenderError(w, err)
}
err = client.RemoveTorrent(torrentID)
if err != nil {
return env.RenderError(w, err)
}
return env.RenderOK(w, "Torrent removed")
}

View File

@ -43,7 +43,7 @@ import getRoutes from './routes'
function mapStateToProps(state) {
let torrentCount = 0;
if (state.torrentStore.has('torrents')) {
torrentCount = state.torrentStore.has('torrents').size;
torrentCount = state.torrentStore.get('torrents').size;
}
return {
username: state.userStore.username,

View File

@ -52,6 +52,7 @@ func setupRoutes(env *web.Env) {
// Torrents routes
env.Handle("/torrents", torrents.DownloadHandler).WithRole(users.UserRole).Methods("POST")
env.Handle("/torrents", torrents.ListHandler).WithRole(users.UserRole).Methods("GET")
env.Handle("/torrents/{id:[0-9]+}", torrents.RemoveHandler).WithRole(users.UserRole).Methods("DELETE")
// Route to refresh all movies and shows
env.Handle("/refresh", extmedias.RefreshHandler).WithRole(users.AdminRole).Methods("POST")