2020-02-24 18:07:47 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/gmemstr/nas/common"
|
|
|
|
"github.com/gmemstr/nas/files"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
2020-03-22 00:18:19 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-02-24 18:07:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func HandleProvider() common.Handler {
|
|
|
|
|
|
|
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if r.Method == "GET" {
|
|
|
|
providerCodename := vars["provider"]
|
2020-03-22 00:18:19 +00:00
|
|
|
providerCodename = strings.Replace(providerCodename, "/", "", -1)
|
2020-03-21 00:31:24 +00:00
|
|
|
provider := *files.Providers[providerCodename]
|
2020-02-24 18:07:47 +00:00
|
|
|
|
|
|
|
fileList := provider.GetDirectory("")
|
|
|
|
if vars["file"] != "" {
|
2020-03-15 23:48:37 +00:00
|
|
|
fileType := provider.DetermineType(vars["file"])
|
|
|
|
if fileType == "" {
|
|
|
|
w.Write([]byte("file not found"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fileType == "file" {
|
2020-03-19 10:13:38 +00:00
|
|
|
provider.ViewFile(vars["file"], w)
|
2020-03-15 23:48:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-24 18:07:47 +00:00
|
|
|
fileList = provider.GetDirectory(vars["file"])
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(fileList)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte("An error occurred"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListProviders() common.Handler {
|
|
|
|
|
|
|
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
2020-03-22 00:18:19 +00:00
|
|
|
var providers []string
|
|
|
|
for v, _ := range files.ProviderConfig {
|
|
|
|
providers = append(providers, v)
|
|
|
|
}
|
|
|
|
sort.Strings(providers)
|
|
|
|
data, err := json.Marshal(providers)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
w.Write(data)
|
2020-02-24 18:07:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|