mirror of
https://github.com/gmemstr/sliproad.git
synced 2024-09-20 00:21:15 +01:00
io.Copy rather than ioutil.ReadAll
Should offer some speed boost, copying the file contents rather than reading it all to memory then writing it back out. Should theoretically be able to handle larger files as well, although some benchmarks are TBD around filesize limits.
This commit is contained in:
parent
ea1075cb5b
commit
c2171d00c9
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -123,7 +124,7 @@ func (bp *BackblazeProvider) GetDirectory(path string) Directory {
|
||||||
return finalDir
|
return finalDir
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *BackblazeProvider) ViewFile(path string) []byte {
|
func (bp *BackblazeProvider) ViewFile(path string, w io.Writer) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
// Get bucket name >:(
|
// Get bucket name >:(
|
||||||
bucketIdPayload := fmt.Sprintf(`{"accountId": "%s", "bucketId": "%s"}`, bp.Name, bp.Bucket)
|
bucketIdPayload := fmt.Sprintf(`{"accountId": "%s", "bucketId": "%s"}`, bp.Name, bp.Bucket)
|
||||||
|
@ -145,22 +146,21 @@ func (bp *BackblazeProvider) ViewFile(path string) []byte {
|
||||||
bytes.NewBuffer([]byte("")))
|
bytes.NewBuffer([]byte("")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
req.Header.Add("Authorization", bp.Authentication)
|
req.Header.Add("Authorization", bp.Authentication)
|
||||||
file, err := client.Do(req)
|
file, err := client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileBytes, err := ioutil.ReadAll(file.Body)
|
_, err = io.Copy(w, file.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
return fileBytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *BackblazeProvider) SaveFile(contents []byte, path string) bool {
|
func (bp *BackblazeProvider) SaveFile(contents []byte, path string) bool {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package files
|
package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -36,13 +37,16 @@ func (dp *DiskProvider) GetDirectory(path string) Directory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dp *DiskProvider) ViewFile(path string) []byte {
|
func (dp *DiskProvider) ViewFile(path string, w io.Writer) {
|
||||||
file := strings.Join([]string{dp.Location,path}, "/")
|
file := strings.Join([]string{dp.Location,path}, "/")
|
||||||
fileContents, err := ioutil.ReadFile(file)
|
fileReader, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return
|
||||||
|
}
|
||||||
|
_, err = io.Copy(w, fileReader)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return fileContents
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dp *DiskProvider) SaveFile(contents []byte, path string) bool {
|
func (dp *DiskProvider) SaveFile(contents []byte, path string) bool {
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package files
|
package files
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
type FileProvider struct {
|
type FileProvider struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
|
@ -30,7 +33,7 @@ type FileContents struct {
|
||||||
|
|
||||||
type FileProviderInterface interface {
|
type FileProviderInterface interface {
|
||||||
GetDirectory(path string) Directory
|
GetDirectory(path string) Directory
|
||||||
ViewFile(path string) []byte
|
ViewFile(path string, w io.Writer)
|
||||||
SaveFile(contents []byte, path string) bool
|
SaveFile(contents []byte, path string) bool
|
||||||
DetermineType(path string) string
|
DetermineType(path string) string
|
||||||
}
|
}
|
||||||
|
@ -41,6 +44,10 @@ func TranslateProvider(codename string, i *FileProviderInterface) {
|
||||||
*i = &DiskProvider{provider,}
|
*i = &DiskProvider{provider,}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* @TODO: It would be ideal if the authorization with Backblaze was done before
|
||||||
|
* actually needing to use it, ideally during the startup step.
|
||||||
|
*/
|
||||||
if codename == "backblaze" {
|
if codename == "backblaze" {
|
||||||
bbProv := &BackblazeProvider{provider, provider.Config["bucket"], ""}
|
bbProv := &BackblazeProvider{provider, provider.Config["bucket"], ""}
|
||||||
|
|
||||||
|
@ -59,8 +66,8 @@ func (f FileProvider) GetDirectory(path string) Directory {
|
||||||
return Directory{}
|
return Directory{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileProvider) ViewFile(path string) []byte {
|
func (f FileProvider) ViewFile(path string, w io.Writer) {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileProvider) SaveFile(contents []byte, path string) bool {
|
func (f FileProvider) SaveFile(contents []byte, path string) bool {
|
||||||
|
|
|
@ -25,8 +25,7 @@ func HandleProvider() common.Handler {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if fileType == "file" {
|
if fileType == "file" {
|
||||||
file := provider.ViewFile(vars["file"])
|
provider.ViewFile(vars["file"], w)
|
||||||
w.Write(file)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
fileList = provider.GetDirectory(vars["file"])
|
fileList = provider.GetDirectory(vars["file"])
|
||||||
|
|
|
@ -29,6 +29,7 @@ func main() {
|
||||||
createLockFile()
|
createLockFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize file providers.
|
||||||
file, err := ioutil.ReadFile("providers.yml")
|
file, err := ioutil.ReadFile("providers.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -37,7 +38,6 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println(files.Providers)
|
|
||||||
|
|
||||||
r := router.Init()
|
r := router.Init()
|
||||||
fmt.Println("Your NAS instance is live on port :3000")
|
fmt.Println("Your NAS instance is live on port :3000")
|
||||||
|
|
Loading…
Reference in a new issue