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

This commit is contained in:
Derrick Hammer 2023-05-15 15:47:34 -04:00
parent 687f26cc77
commit 673f7c6dfd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 45 additions and 9 deletions

View File

@ -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

View File

@ -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
}
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
}
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

4
tus.go
View File

@ -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