diff --git a/files/backblaze.go b/files/backblaze.go index e66d7d2..52b97d5 100644 --- a/files/backblaze.go +++ b/files/backblaze.go @@ -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 { diff --git a/files/disk.go b/files/disk.go index 096a08a..a1902f4 100644 --- a/files/disk.go +++ b/files/disk.go @@ -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 { diff --git a/files/fileprovider.go b/files/fileprovider.go index 68b9aa9..43abf67 100644 --- a/files/fileprovider.go +++ b/files/fileprovider.go @@ -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 { diff --git a/router/filerouter.go b/router/filerouter.go index 2bf13da..0beda8d 100644 --- a/router/filerouter.go +++ b/router/filerouter.go @@ -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"]) diff --git a/webserver.go b/webserver.go index 332e19f..4cc8674 100644 --- a/webserver.go +++ b/webserver.go @@ -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")