sliproad/router/filerouter.go
Gabriel Simmer 49dba732d3
Implement Setup() function for providers, cleanup
Implemented a much-needed Setup() function for providers to implement,
which (as demonstrated in the backblaze provider) allows for
authentication in advance of needing to make calls to remote locations.
This could also be used to create a directory or perform some other
sanity check required for the provider to work. So far, haven't noticed
any performance impacts from this approach, besides not needing to auth
each time we make a request.
2020-03-21 00:31:24 +00:00

50 lines
1.1 KiB
Go

package router
import (
"encoding/json"
"github.com/gmemstr/nas/common"
"github.com/gmemstr/nas/files"
"github.com/gorilla/mux"
"net/http"
)
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"]
provider := *files.Providers[providerCodename]
fileList := provider.GetDirectory("")
if vars["file"] != "" {
fileType := provider.DetermineType(vars["file"])
if fileType == "" {
w.Write([]byte("file not found"))
return nil
}
if fileType == "file" {
provider.ViewFile(vars["file"], w)
return nil
}
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 {
return nil
}
}