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:
parent
687f26cc77
commit
673f7c6dfd
|
@ -23,7 +23,7 @@ func (f *FilesController) PostUpload() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
upload, err := files.Upload(file)
|
upload, err := files.Upload(file, nil)
|
||||||
|
|
||||||
if internalError(ctx, err) {
|
if internalError(ctx, err) {
|
||||||
return
|
return
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
"io"
|
"io"
|
||||||
"lukechampine.com/blake3"
|
"lukechampine.com/blake3"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *resty.Client
|
var client *resty.Client
|
||||||
|
@ -23,12 +24,23 @@ func Init() {
|
||||||
client.SetDisableWarn(true)
|
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
|
var upload model.Upload
|
||||||
|
|
||||||
|
if r == nil && file == nil {
|
||||||
|
return upload, errors.New("invalid upload mode")
|
||||||
|
}
|
||||||
|
|
||||||
hasher := blake3.New(32, nil)
|
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 {
|
if err != nil {
|
||||||
return upload, err
|
return upload, err
|
||||||
}
|
}
|
||||||
|
@ -41,7 +53,12 @@ func Upload(r io.ReadSeeker) (model.Upload, error) {
|
||||||
return upload, err
|
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 {
|
if err != nil {
|
||||||
return upload, err
|
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")
|
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 {
|
if err != nil {
|
||||||
return upload, err
|
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 {
|
if err != nil {
|
||||||
return upload, err
|
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 {
|
if ret.StatusCode() != 200 {
|
||||||
err = errors.New(string(ret.Body()))
|
err = errors.New(string(ret.Body()))
|
||||||
return upload, err
|
return upload, err
|
||||||
|
|
4
tus.go
4
tus.go
|
@ -13,8 +13,8 @@ import (
|
||||||
"github.com/tus/tusd/pkg/filestore"
|
"github.com/tus/tusd/pkg/filestore"
|
||||||
tusd "github.com/tus/tusd/pkg/handler"
|
tusd "github.com/tus/tusd/pkg/handler"
|
||||||
"github.com/tus/tusd/pkg/memorylocker"
|
"github.com/tus/tusd/pkg/memorylocker"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TUS_API_PATH = "/files/tus"
|
const TUS_API_PATH = "/files/tus"
|
||||||
|
@ -133,7 +133,7 @@ func tusWorker(upload *tusd.Upload) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = files.Upload(file.(io.ReadSeeker))
|
_, err = files.Upload(nil, file.(*os.File))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue