@@ -79,17 +79,17 @@ func (i *TursoServerClient) UploadFileMultipart(filepath string, remoteEncryptio
7979 totalSize := stat .Size ()
8080 startTime := time .Now ()
8181
82- chunkSize , err := i .startMultipartUpload (totalSize )
82+ uploadStart , err := i .startMultipartUpload (totalSize )
8383 if err != nil {
8484 return err
8585 }
8686
87- uploadedBytes , err := i .uploadChunks (chunkSize , file , totalSize , startTime , remoteEncryptionCipher , remoteEncryptionKey , onUploadProgress )
87+ uploadedBytes , err := i .uploadChunks (uploadStart . UploadID , uploadStart . ChunkSize , file , totalSize , startTime , remoteEncryptionCipher , remoteEncryptionKey , onUploadProgress )
8888 if err != nil {
8989 return err
9090 }
9191
92- if err = i .finalizeUpload (); err != nil {
92+ if err = i .finalizeUpload (uploadStart . UploadID ); err != nil {
9393 return err
9494 }
9595
@@ -99,42 +99,51 @@ func (i *TursoServerClient) UploadFileMultipart(filepath string, remoteEncryptio
9999 return nil
100100}
101101
102- func (i * TursoServerClient ) startMultipartUpload (dbSize int64 ) (int64 , error ) {
102+ type multipartUploadStart struct {
103+ ChunkSize int64
104+ UploadID string
105+ }
106+
107+ func (i * TursoServerClient ) startMultipartUpload (dbSize int64 ) (multipartUploadStart , error ) {
103108 requestBody := map [string ]int64 {
104109 "db_size_bytes" : dbSize ,
105110 }
106111
107112 body , err := marshal (requestBody )
108113 if err != nil {
109- return 0 , fmt .Errorf ("failed to marshal multipart upload request: %w" , err )
114+ return multipartUploadStart {} , fmt .Errorf ("failed to marshal multipart upload request: %w" , err )
110115 }
111116
112117 r , err := i .client .Put ("/v2/upload/start" , body )
113118 if err != nil {
114- return 0 , fmt .Errorf ("failed to initiate multipart upload: %w" , err )
119+ return multipartUploadStart {} , fmt .Errorf ("failed to initiate multipart upload: %w" , err )
115120 }
116121 defer r .Body .Close ()
117122
118123 if r .StatusCode != http .StatusOK {
119124 body , err := io .ReadAll (r .Body )
120125 if err != nil {
121- return 0 , fmt .Errorf ("initiate multipart upload failed with status code %d and error reading response: %v" , r .StatusCode , err )
126+ return multipartUploadStart {} , fmt .Errorf ("initiate multipart upload failed with status code %d and error reading response: %v" , r .StatusCode , err )
122127 }
123- return 0 , fmt .Errorf ("initiate multipart upload failed with status code %d: %s" , r .StatusCode , string (body ))
128+ return multipartUploadStart {} , fmt .Errorf ("initiate multipart upload failed with status code %d: %s" , r .StatusCode , string (body ))
124129 }
125130
126131 type multipartUploadResponse struct {
127- ChunkSize int64 `json:"chunk_size"`
132+ ChunkSize int64 `json:"chunk_size"`
133+ UploadID string `json:"upload_id"`
128134 }
129135 var uploadResp multipartUploadResponse
130136 if err := json .NewDecoder (r .Body ).Decode (& uploadResp ); err != nil {
131- return 0 , fmt .Errorf ("failed to decode multipart upload response: %w" , err )
137+ return multipartUploadStart {} , fmt .Errorf ("failed to decode multipart upload response: %w" , err )
132138 }
133139
134- return uploadResp .ChunkSize , nil
140+ return multipartUploadStart {
141+ ChunkSize : uploadResp .ChunkSize ,
142+ UploadID : uploadResp .UploadID ,
143+ }, nil
135144}
136145
137- func (i * TursoServerClient ) uploadChunks (chunkSize int64 , file io.Reader , totalSize int64 , startTime time.Time , remoteEncryptionCipher , remoteEncryptionKey string , onUploadProgress func (progressPct int , uploadedBytes int64 , totalBytes int64 , elapsedTime time.Duration , done bool )) (int64 , error ) {
146+ func (i * TursoServerClient ) uploadChunks (uploadID string , chunkSize int64 , file io.Reader , totalSize int64 , startTime time.Time , remoteEncryptionCipher , remoteEncryptionKey string , onUploadProgress func (progressPct int , uploadedBytes int64 , totalBytes int64 , elapsedTime time.Duration , done bool )) (int64 , error ) {
138147 var uploadedBytes int64 = 0
139148 chunkID := 0
140149 lastProgressPct := - 1
@@ -157,7 +166,7 @@ func (i *TursoServerClient) uploadChunks(chunkSize int64, file io.Reader, totalS
157166 lastUpdate : lastProgressPct ,
158167 }
159168
160- chunkPath := fmt .Sprintf ("/v2/upload/chunk/%d" , chunkID )
169+ chunkPath := fmt .Sprintf ("/v2/upload/%s/ chunk/%d" , uploadID , chunkID )
161170
162171 var headers = map [string ]string {}
163172 if remoteEncryptionCipher != "" && remoteEncryptionKey != "" {
@@ -191,8 +200,8 @@ func (i *TursoServerClient) uploadChunks(chunkSize int64, file io.Reader, totalS
191200 return uploadedBytes , nil
192201}
193202
194- func (i * TursoServerClient ) finalizeUpload () error {
195- r , err := i .client .Put ("/v2/upload/finalize" , nil )
203+ func (i * TursoServerClient ) finalizeUpload (uploadID string ) error {
204+ r , err := i .client .Put (fmt . Sprintf ( "/v2/upload/%s/ finalize" , uploadID ) , nil )
196205 if err != nil {
197206 return fmt .Errorf ("failed to finalize multipart upload: %w" , err )
198207 }
0 commit comments