From 673f7c6dfd4cee1386462bb3841ce2742fe96b15 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 15 May 2023 15:47:34 -0400 Subject: [PATCH] refactor: have Upload take both a io.ReaderSeeker and os.File and update usages based of if we are streaming a small file or handling a big one via filename --- controller/files.go | 2 +- service/files/files.go | 48 ++++++++++++++++++++++++++++++++++++------ tus.go | 4 ++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/controller/files.go b/controller/files.go index 2c4251a..2d0b053 100644 --- a/controller/files.go +++ b/controller/files.go @@ -23,7 +23,7 @@ func (f *FilesController) PostUpload() { return } - upload, err := files.Upload(file) + upload, err := files.Upload(file, nil) if internalError(ctx, err) { return diff --git a/service/files/files.go b/service/files/files.go index d099954..263dcfe 100644 --- a/service/files/files.go +++ b/service/files/files.go @@ -12,6 +12,7 @@ import ( "github.com/go-resty/resty/v2" "io" "lukechampine.com/blake3" + "os" ) var client *resty.Client @@ -23,12 +24,23 @@ func Init() { client.SetDisableWarn(true) } -func Upload(r io.ReadSeeker) (model.Upload, error) { +func Upload(r io.ReadSeeker, file *os.File) (model.Upload, error) { var upload model.Upload + if r == nil && file == nil { + return upload, errors.New("invalid upload mode") + } + hasher := blake3.New(32, nil) - _, err := io.Copy(hasher, r) + var err error + + if r != nil { + _, err = io.Copy(hasher, r) + } else { + _, err = io.Copy(hasher, file) + } + if err != nil { return upload, err } @@ -41,7 +53,12 @@ func Upload(r io.ReadSeeker) (model.Upload, error) { return upload, err } - _, err = r.Seek(0, io.SeekStart) + if r != nil { + _, err = r.Seek(0, io.SeekStart) + } else { + _, err = file.Seek(0, io.SeekStart) + } + if err != nil { return upload, err } @@ -64,18 +81,37 @@ func Upload(r io.ReadSeeker) (model.Upload, error) { return upload, errors.New("file already exists in network, but missing in database") } - tree, err := bao.ComputeBaoTree(bufio.NewReader(r)) + var tree []byte + + if r != nil { + tree, err = bao.ComputeTreeStreaming(bufio.NewReader(r)) + } else { + tree, err = bao.ComputeTreeFile(file) + } if err != nil { return upload, err } - _, err = r.Seek(0, io.SeekStart) + if r != nil { + _, err = r.Seek(0, io.SeekStart) + } else { + _, err = file.Seek(0, io.SeekStart) + } + if err != nil { return upload, err } - ret, err := client.R().SetBody(r).Put(fmt.Sprintf("/worker/objects/%s", hashHex)) + var body interface{} + + if r != nil { + body = r + } else { + body = file + } + + ret, err := client.R().SetBody(body).Put(fmt.Sprintf("/worker/objects/%s", hashHex)) if ret.StatusCode() != 200 { err = errors.New(string(ret.Body())) return upload, err diff --git a/tus.go b/tus.go index 24ed3b1..ad83442 100644 --- a/tus.go +++ b/tus.go @@ -13,8 +13,8 @@ import ( "github.com/tus/tusd/pkg/filestore" tusd "github.com/tus/tusd/pkg/handler" "github.com/tus/tusd/pkg/memorylocker" - "io" "log" + "os" ) const TUS_API_PATH = "/files/tus" @@ -133,7 +133,7 @@ func tusWorker(upload *tusd.Upload) error { return err } - _, err = files.Upload(file.(io.ReadSeeker)) + _, err = files.Upload(nil, file.(*os.File)) if err != nil { log.Print(err) return err