@ -18,6 +18,7 @@
package main
package main
import (
import (
"errors"
"fmt"
"fmt"
"io"
"io"
"io/ioutil"
"io/ioutil"
@ -87,24 +88,32 @@ func upload(ctx *cli.Context) {
if err != nil {
if err != nil {
utils . Fatalf ( "Error opening file: %s" , err )
utils . Fatalf ( "Error opening file: %s" , err )
}
}
var hash string
// define a function which either uploads a directory or single file
// based on the type of the file being uploaded
var doUpload func ( ) ( hash string , err error )
if stat . IsDir ( ) {
if stat . IsDir ( ) {
if ! recursive {
doUpload = func ( ) ( string , error ) {
utils . Fatalf ( "Argument is a directory and recursive upload is disabled" )
if ! recursive {
return "" , errors . New ( "Argument is a directory and recursive upload is disabled" )
}
return client . UploadDirectory ( file , defaultPath , "" )
}
}
hash , err = client . UploadDirectory ( file , defaultPath , "" )
} else {
} else {
if mimeType == "" {
doUpload = func ( ) ( string , error ) {
mimeType = detectMimeType ( file )
f , err := swarm . Open ( file )
}
if err != nil {
f , err := swarm . Open ( file )
return "" , fmt . Errorf ( "error opening file: %s" , err )
if err != nil {
}
utils . Fatalf ( "Error opening file: %s" , err )
defer f . Close ( )
if mimeType == "" {
mimeType = detectMimeType ( file )
}
f . ContentType = mimeType
return client . Upload ( f , "" )
}
}
defer f . Close ( )
f . ContentType = mimeType
hash , err = client . Upload ( f , "" )
}
}
hash , err := doUpload ( )
if err != nil {
if err != nil {
utils . Fatalf ( "Upload failed: %s" , err )
utils . Fatalf ( "Upload failed: %s" , err )
}
}