44 "encoding/json"
55 "errors"
66 "fmt"
7+ "io"
78 "os"
89 "time"
910
@@ -227,11 +228,20 @@ func (s *Service) RestoreBackup(r *request.RestoreBackupRequest) error {
227228// CreateStorageImport begins the process of importing an image onto a storage device. A `upcloud.StorageImportSourceHTTPImport` source
228229// will import from an HTTP source. `upcloud.StorageImportSourceDirectUpload` will directly upload the file specified in `SourceLocation`.
229230func (s * Service ) CreateStorageImport (r * request.CreateStorageImportRequest ) (* upcloud.StorageImportDetails , error ) {
230-
231231 if r .Source == request .StorageImportSourceDirectUpload {
232- return s .directStorageImport (r )
232+ switch r .SourceLocation .(type ) {
233+ case string , io.Reader :
234+ return s .directStorageImport (r )
235+ case nil :
236+ return nil , errors .New ("SourceLocation must be specified" )
237+ default :
238+ return nil , fmt .Errorf ("unsupported storage source location type %T" , r .SourceLocation )
239+ }
233240 }
234241
242+ if _ , isString := r .SourceLocation .(string ); ! isString {
243+ return nil , fmt .Errorf ("unsupported storage source location type %T" , r .Source )
244+ }
235245 return s .doCreateStorageImport (r )
236246}
237247
@@ -253,15 +263,24 @@ func (s *Service) doCreateStorageImport(r *request.CreateStorageImportRequest) (
253263// directStorageImport handles the direct upload logic including getting the upload URL and PUT the file data
254264// to that endpoint.
255265func (s * Service ) directStorageImport (r * request.CreateStorageImportRequest ) (* upcloud.StorageImportDetails , error ) {
256- if r .SourceLocation == "" {
257- return nil , errors .New ("SourceLocation must be specified" )
258- }
266+ var bodyReader io.Reader
259267
260- f , err := os .Open (r .SourceLocation )
261- if err != nil {
262- return nil , fmt .Errorf ("unable to open SourceLocation: %w" , err )
268+ switch v := r .SourceLocation .(type ) {
269+ case string :
270+ if v == "" {
271+ return nil , errors .New ("SourceLocation must be specified" )
272+ }
273+ f , err := os .Open (v )
274+ if err != nil {
275+ return nil , fmt .Errorf ("unable to open SourceLocation: %w" , err )
276+ }
277+ bodyReader = f
278+ defer f .Close ()
279+ case io.Reader :
280+ bodyReader = v
281+ default :
282+ return nil , fmt .Errorf ("unsupported source location type %T" , r .SourceLocation )
263283 }
264- defer f .Close ()
265284
266285 r .SourceLocation = ""
267286 storageImport , err := s .doCreateStorageImport (r )
@@ -273,10 +292,15 @@ func (s *Service) directStorageImport(r *request.CreateStorageImportRequest) (*u
273292 return nil , errors .New ("no DirectUploadURL found in response" )
274293 }
275294
276- _ , err = s .client .PerformJSONPutUploadRequest (storageImport .DirectUploadURL , f )
295+ curContentType := s .client .GetContentType ()
296+ if r .ContentType != "" {
297+ s .client .SetContentType (r .ContentType )
298+ }
299+ _ , err = s .client .PerformJSONPutUploadRequest (storageImport .DirectUploadURL , bodyReader )
277300 if err != nil {
278301 return nil , err
279302 }
303+ s .client .SetContentType (curContentType )
280304
281305 storageImport , err = s .GetStorageImportDetails (& request.GetStorageImportDetailsRequest {
282306 UUID : r .StorageUUID ,
0 commit comments