@@ -2,17 +2,12 @@ package tusgo
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
- "fmt"
7
5
"io"
8
6
"math/rand"
9
- "net"
10
7
"net/http"
11
8
"net/url"
12
- "os"
13
9
"reflect"
14
10
"strconv"
15
- "sync"
16
11
"time"
17
12
18
13
. "github.com/onsi/ginkgo/v2"
@@ -900,270 +895,3 @@ var _ = Describe("Client", func() {
900
895
})
901
896
})
902
897
})
903
-
904
- // doUpload does the transfer of the data until we will finish the upload, or until too many errors will occur
905
- func doUpload (dst * UploadStream , src * os.File ) error {
906
- attempts := 10
907
-
908
- // Reset offsets -- they will be further adjusted to the remote upload offset
909
- if _ , err := dst .Seek (0 , io .SeekStart ); err != nil {
910
- return err
911
- }
912
- if _ , err := src .Seek (0 , io .SeekStart ); err != nil {
913
- return err
914
- }
915
-
916
- for dst .Upload .RemoteOffset < dst .Upload .RemoteSize && attempts > 0 {
917
- _ , err := io .Copy (dst , src )
918
- if err == nil {
919
- break // Transfer has finished
920
- }
921
-
922
- attempts --
923
- if errors .Is (err , ErrOffsetsNotSynced ) {
924
- // Sync local and remote offsets
925
- if _ , err = dst .Sync (); err != nil {
926
- return err
927
- }
928
- // Also adjust the file pointer
929
- if _ , err = src .Seek (dst .Tell (), io .SeekStart ); err != nil {
930
- return err
931
- }
932
- } else if errors .Is (err , ErrChecksumMismatch ) {
933
- continue // Checksum mismatch, try to make a transfer again
934
- } else if _ , ok := err .(net.Error ); ! ok {
935
- return err // Permanent error
936
- }
937
- }
938
- if attempts == 0 {
939
- return errors .New ("too many attempts to upload the data" )
940
- }
941
- return nil
942
- }
943
-
944
- func createUploadFromFile (file * os.File , cl * Client , partial bool ) * Upload {
945
- // Open a file to be transferred
946
- finfo , err := file .Stat ()
947
- if err != nil {
948
- panic (err )
949
- }
950
-
951
- u := Upload {}
952
- if _ , err := cl .CreateUpload (& u , finfo .Size (), partial , nil ); err != nil {
953
- panic (err )
954
- }
955
- fmt .Printf ("Location: %s\n " , u .Location )
956
- return & u
957
- }
958
-
959
- func ExampleClient_CreateUpload () {
960
- baseURL , err := url .Parse ("http://example.com/files" )
961
- if err != nil {
962
- panic (err )
963
- }
964
- cl := NewClient (http .DefaultClient , baseURL )
965
- if _ , err = cl .UpdateCapabilities (); err != nil {
966
- panic (err )
967
- }
968
-
969
- u := Upload {}
970
- // Create an upload with 2 MiB size
971
- if _ , err = cl .CreateUpload (& u , 1024 * 1024 * 2 , false , nil ); err != nil {
972
- panic (err )
973
- }
974
- fmt .Printf ("Location: %s\n " , u .Location )
975
- }
976
-
977
- func ExampleClient_ConcatenateUploads_with_creation () {
978
- baseURL , err := url .Parse ("http://example.com/files" )
979
- if err != nil {
980
- panic (err )
981
- }
982
- cl := NewClient (http .DefaultClient , baseURL )
983
- if _ , err = cl .UpdateCapabilities (); err != nil {
984
- panic (err )
985
- }
986
-
987
- wg := & sync.WaitGroup {}
988
- fileNames := []string {"/tmp/file1.txt" , "/tmp/file2.txt" }
989
- // Assume that uploads were already been created
990
- uploads := make ([]* Upload , 2 )
991
- wg .Add (len (fileNames ))
992
-
993
- // Transfer partial uploads in parallel
994
- for ind , fn := range fileNames {
995
- fn := fn
996
- ind := ind
997
- go func () {
998
- defer wg .Done ()
999
-
1000
- f , err := os .Open (fn )
1001
- if err != nil {
1002
- panic (err )
1003
- }
1004
- defer f .Close ()
1005
- uploads [ind ] = createUploadFromFile (f , cl , true )
1006
- fmt .Printf ("Upload #%d: Location: %s" , ind , uploads [ind ].Location )
1007
-
1008
- fmt .Printf ("Upload #%d: transferring file %s to %s\n " , ind , fn , uploads [ind ].Location )
1009
- stream := NewUploadStream (cl , uploads [ind ])
1010
- if err = doUpload (stream , f ); err != nil {
1011
- panic (err )
1012
- }
1013
- }()
1014
- }
1015
-
1016
- wg .Wait ()
1017
- fmt .Println ("Uploading complete, starting concatenation..." )
1018
-
1019
- // Concatenate partial uploads into a final upload
1020
- final := Upload {}
1021
- if _ , err = cl .ConcatenateUploads (& final , []Upload {* uploads [0 ], * uploads [1 ]}, nil ); err != nil {
1022
- panic (err )
1023
- }
1024
-
1025
- fmt .Printf ("Final upload location: %s\n " , final .Location )
1026
-
1027
- // Get file info
1028
- u := Upload {RemoteOffset : OffsetUnknown }
1029
- for {
1030
- if _ , err = cl .GetUpload (& u , final .Location ); err != nil {
1031
- panic (err )
1032
- }
1033
- // When concatenation still in progress the offset can be either OffsetUnknown or a value less than size
1034
- // depending on server implementation
1035
- if u .RemoteOffset != OffsetUnknown && u .RemoteOffset == u .RemoteSize {
1036
- break
1037
- }
1038
- fmt .Println ("Waiting concatenation to be finished" )
1039
- time .Sleep (2 * time .Second )
1040
- }
1041
-
1042
- fmt .Printf ("Concatenation finished. Offset: %d, Size: %d\n " , u .RemoteOffset , u .RemoteSize )
1043
- }
1044
-
1045
- func Example_creation_and_transfer () {
1046
- baseURL , err := url .Parse ("http://example.com/files" )
1047
- if err != nil {
1048
- panic (err )
1049
- }
1050
- cl := NewClient (http .DefaultClient , baseURL )
1051
- if _ , err = cl .UpdateCapabilities (); err != nil {
1052
- panic (err )
1053
- }
1054
-
1055
- f , err := os .Open ("/tmp/file.txt" )
1056
- if err != nil {
1057
- panic (err )
1058
- }
1059
- defer f .Close ()
1060
- u := createUploadFromFile (f , cl , false )
1061
-
1062
- stream := NewUploadStream (cl , u )
1063
- if err = doUpload (stream , f ); err != nil {
1064
- panic (err )
1065
- }
1066
- fmt .Printf ("Uploading complete. Offset: %d, Size: %d\n " , u .RemoteOffset , u .RemoteSize )
1067
- }
1068
-
1069
- func Example_creation_and_transfer_with_deferred_size () {
1070
- baseURL , err := url .Parse ("http://example.com/files" )
1071
- if err != nil {
1072
- panic (err )
1073
- }
1074
- cl := NewClient (http .DefaultClient , baseURL )
1075
- if _ , err = cl .UpdateCapabilities (); err != nil {
1076
- panic (err )
1077
- }
1078
-
1079
- u := Upload {}
1080
- if _ , err = cl .CreateUpload (& u , SizeUnknown , false , nil ); err != nil {
1081
- panic (err )
1082
- }
1083
- fmt .Printf ("Location: %s\n " , u .Location )
1084
-
1085
- // Open a file to be transferred
1086
- f , err := os .Open ("/tmp/file.txt" )
1087
- if err != nil {
1088
- panic (err )
1089
- }
1090
- defer f .Close ()
1091
- finfo , err := f .Stat ()
1092
- if err != nil {
1093
- panic (err )
1094
- }
1095
- u .RemoteSize = finfo .Size () // Set size after the upload has been created on server
1096
-
1097
- stream := NewUploadStream (cl , & u )
1098
- stream .SetUploadSize = true
1099
- if err = doUpload (stream , f ); err != nil {
1100
- panic (err )
1101
- }
1102
- fmt .Printf ("Uploading complete. Offset: %d, Size: %d\n " , u .RemoteOffset , u .RemoteSize )
1103
- }
1104
-
1105
- func Example_checksum () {
1106
- baseURL , err := url .Parse ("http://example.com/files" )
1107
- if err != nil {
1108
- panic (err )
1109
- }
1110
- cl := NewClient (http .DefaultClient , baseURL )
1111
- if _ , err = cl .UpdateCapabilities (); err != nil {
1112
- panic (err )
1113
- }
1114
-
1115
- // Open a file to be transferred
1116
- f , err := os .Open ("/tmp/file.txt" )
1117
- if err != nil {
1118
- panic (err )
1119
- }
1120
- defer f .Close ()
1121
- finfo , err := f .Stat ()
1122
- if err != nil {
1123
- panic (err )
1124
- }
1125
- u := Upload {Location : "http://example.com/files/foo/bar" , RemoteSize : finfo .Size ()}
1126
-
1127
- // We want to use sha1
1128
- stream := NewUploadStream (cl , & u ).WithChecksumAlgorithm ("sha1" )
1129
- if err = doUpload (stream , f ); err != nil {
1130
- panic (err )
1131
- }
1132
- fmt .Println ("Uploading complete" )
1133
- }
1134
-
1135
- func Example_transfer_with_progress_watch () {
1136
- baseURL , err := url .Parse ("http://example.com/files" )
1137
- if err != nil {
1138
- panic (err )
1139
- }
1140
- cl := NewClient (http .DefaultClient , baseURL )
1141
- if _ , err = cl .UpdateCapabilities (); err != nil {
1142
- panic (err )
1143
- }
1144
-
1145
- // Open a file to be transferred
1146
- f , err := os .Open ("/tmp/file1.txt" )
1147
- if err != nil {
1148
- panic (err )
1149
- }
1150
- defer f .Close ()
1151
- u := createUploadFromFile (f , cl , false )
1152
-
1153
- go func () {
1154
- ticker := time .NewTicker (1 * time .Second )
1155
- defer ticker .Stop ()
1156
- for range ticker .C {
1157
- fmt .Printf ("Progress: %d/%d (%.1f%%)\n " , u .RemoteOffset , u .RemoteSize , float64 (u .RemoteOffset )/ float64 (u .RemoteSize )* 100 )
1158
- if u .RemoteOffset == u .RemoteSize {
1159
- return
1160
- }
1161
- }
1162
- }()
1163
-
1164
- stream := NewUploadStream (cl , u )
1165
- if err = doUpload (stream , f ); err != nil {
1166
- panic (err )
1167
- }
1168
- fmt .Printf ("Uploading complete. Offset: %d, Size: %d\n " , u .RemoteOffset , u .RemoteSize )
1169
- }
0 commit comments