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"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -123,7 +124,7 @@ func (bp *BackblazeProvider) GetDirectory(path string) Directory {
|
|||
return finalDir
|
||||
}
|
||||
|
||||
func (bp *BackblazeProvider) ViewFile(path string) []byte {
|
||||
func (bp *BackblazeProvider) ViewFile(path string, w io.Writer) {
|
||||
client := &http.Client{}
|
||||
// Get bucket name >:(
|
||||
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("")))
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return nil
|
||||
return
|
||||
}
|
||||
req.Header.Add("Authorization", bp.Authentication)
|
||||
file, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
fileBytes, err := ioutil.ReadAll(file.Body)
|
||||
_, err = io.Copy(w, file.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return nil
|
||||
return
|
||||
}
|
||||
return fileBytes
|
||||
}
|
||||
|
||||
func (bp *BackblazeProvider) SaveFile(contents []byte, path string) bool {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package files
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"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}, "/")
|
||||
fileContents, err := ioutil.ReadFile(file)
|
||||
fileReader, err := os.Open(file)
|
||||
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 {
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package files
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type FileProvider struct {
|
||||
Name string `yaml:"name"`
|
||||
|
@ -30,7 +33,7 @@ type FileContents struct {
|
|||
|
||||
type FileProviderInterface interface {
|
||||
GetDirectory(path string) Directory
|
||||
ViewFile(path string) []byte
|
||||
ViewFile(path string, w io.Writer)
|
||||
SaveFile(contents []byte, path string) bool
|
||||
DetermineType(path string) string
|
||||
}
|
||||
|
@ -41,6 +44,10 @@ func TranslateProvider(codename string, i *FileProviderInterface) {
|
|||
*i = &DiskProvider{provider,}
|
||||
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" {
|
||||
bbProv := &BackblazeProvider{provider, provider.Config["bucket"], ""}
|
||||
|
||||
|
@ -59,8 +66,8 @@ func (f FileProvider) GetDirectory(path string) Directory {
|
|||
return Directory{}
|
||||
}
|
||||
|
||||
func (f FileProvider) ViewFile(path string) []byte {
|
||||
return nil
|
||||
func (f FileProvider) ViewFile(path string, w io.Writer) {
|
||||
return
|
||||
}
|
||||
|
||||
func (f FileProvider) SaveFile(contents []byte, path string) bool {
|
||||
|
|
|
@ -25,8 +25,7 @@ func HandleProvider() common.Handler {
|
|||
return nil
|
||||
}
|
||||
if fileType == "file" {
|
||||
file := provider.ViewFile(vars["file"])
|
||||
w.Write(file)
|
||||
provider.ViewFile(vars["file"], w)
|
||||
return nil
|
||||
}
|
||||
fileList = provider.GetDirectory(vars["file"])
|
||||
|
|
|
@ -29,6 +29,7 @@ func main() {
|
|||
createLockFile()
|
||||
}
|
||||
|
||||
// Initialize file providers.
|
||||
file, err := ioutil.ReadFile("providers.yml")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -37,7 +38,6 @@ func main() {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(files.Providers)
|
||||
|
||||
r := router.Init()
|
||||
fmt.Println("Your NAS instance is live on port :3000")
|
||||
|
|
Loading…
Reference in a new issue