@@ -28,13 +28,39 @@ type (
2828 URL string
2929 }
3030
31- TUSClient struct {
31+ tusClient struct {
3232 tusClient * tus.Client
3333 maxRetries int
3434 }
3535)
3636
37- func NewTUSClient (sessionState * session.State , connection * connection.ConnectionSettings , chunkSize int64 , maxRetries int ) (* TUSClient , error ) {
37+ // UploadTempContentFromFile uploads a tempfile with smart selection of
38+ // chunksize. If chunksize is not set (<=0) and file size is smaller than
39+ // default chunksize, the chunksize it will be set to the file size roofed to
40+ // closest 1024 bytes.
41+ func UploadTempContentFromFile (ctx context.Context , sessionState * session.State , connection * connection.ConnectionSettings ,
42+ file * os.File , chunkSize int64 , maxRetries int ) (* RemoteFile , error ) {
43+
44+ fileStat , err := file .Stat ()
45+ if err != nil {
46+ return nil , errors .WithStack (err )
47+ }
48+ fileSize := fileStat .Size ()
49+ if chunkSize <= 0 && fileSize < defaultChunkSize {
50+ chunkSize = (chunkSize / 1024 + 1 ) * 1024
51+ }
52+ tempFileClient , err := newTUSClient (sessionState , connection , chunkSize , maxRetries )
53+ if err != nil {
54+ return nil , errors .WithStack (err )
55+ }
56+ tempFile , err := tempFileClient .uploadFromFile (ctx , file )
57+ if err != nil {
58+ return nil , errors .Wrap (err , "failed to upload temp content from file" )
59+ }
60+ return tempFile , nil
61+ }
62+
63+ func newTUSClient (sessionState * session.State , connection * connection.ConnectionSettings , chunkSize int64 , maxRetries int ) (* tusClient , error ) {
3864 if maxRetries < 0 {
3965 maxRetries = 0
4066 }
@@ -61,13 +87,13 @@ func NewTUSClient(sessionState *session.State, connection *connection.Connection
6187 if err != nil {
6288 return nil , errors .Wrap (err , "failed to create tus client" )
6389 }
64- return & TUSClient {
90+ return & tusClient {
6591 tusClient : client ,
6692 maxRetries : maxRetries ,
6793 }, nil
6894}
6995
70- func (client TUSClient ) UploadFromFile (ctx context.Context , file * os.File ) (* RemoteFile , error ) {
96+ func (client tusClient ) uploadFromFile (ctx context.Context , file * os.File ) (* RemoteFile , error ) {
7197 upload , err := tus .NewUploadFromFile (file )
7298 if err != nil {
7399 return nil , errors .Wrap (err , "failed to create tus upload from file" )
0 commit comments