mirror of
https://github.com/gmemstr/sliproad.git
synced 2024-09-20 00:21:15 +01:00
d86ed1233f
Implemented file uploading for both the disk and Backblaze providers. Also implements a UI element and frontend logic for doing so from the frontend. Disk will write directly to disk, while the Backblaze provider will attempt to stream the file from memory directly to the POST body. This could introduce some problems down the line, so caching to disk then uploading in the background may need to be implemented. It also performs the final upload using a goroutine so the end client can continue on it's merry way. Backblaze proved a bit tricky to do, and considering switching to using a library for managing Backblaze going forward.
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gmemstr/nas/common"
|
|
"github.com/gmemstr/nas/files"
|
|
"github.com/gorilla/mux"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
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"]
|
|
providerCodename = strings.Replace(providerCodename, "/", "", -1)
|
|
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)
|
|
}
|
|
if r.Method == "POST" {
|
|
providerCodename := vars["provider"]
|
|
providerCodename = strings.Replace(providerCodename, "/", "", -1)
|
|
provider := *files.Providers[providerCodename]
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
if err != nil {
|
|
w.Write([]byte("unable to parse form"))
|
|
fmt.Println(err.Error())
|
|
return nil
|
|
}
|
|
file, handler, err := r.FormFile("file")
|
|
defer file.Close()
|
|
|
|
success := provider.SaveFile(file, handler, vars["file"])
|
|
if !success {
|
|
w.Write([]byte("unable to save file"))
|
|
return nil
|
|
}
|
|
w.Write([]byte("saved file"))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func ListProviders() common.Handler {
|
|
|
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
|
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)
|
|
return nil
|
|
}
|
|
}
|