|
@ -4,7 +4,6 @@ import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
"flag" |
|
|
|
|
|
"fmt" |
|
|
"fmt" |
|
|
"io" |
|
|
"io" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
@ -16,16 +15,20 @@ import ( |
|
|
"strconv" |
|
|
"strconv" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var uploadReplication *string |
|
|
|
|
|
|
|
|
func init() { |
|
|
func init() { |
|
|
cmdUpload.Run = runUpload // break init cycle
|
|
|
cmdUpload.Run = runUpload // break init cycle
|
|
|
IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information") |
|
|
IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information") |
|
|
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location") |
|
|
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location") |
|
|
|
|
|
uploadReplication = cmdUpload.Flag.String("replication", "00", "replication type(00,01,10,11)") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var cmdUpload = &Command{ |
|
|
var cmdUpload = &Command{ |
|
|
UsageLine: "upload -server=localhost:9333 file1 file2 file2", |
|
|
|
|
|
Short: "upload a set of files, using consecutive file keys", |
|
|
|
|
|
Long: `upload a set of files, using consecutive file keys. |
|
|
|
|
|
|
|
|
UsageLine: "upload -server=localhost:9333 file1 [file2 file3]", |
|
|
|
|
|
Short: "upload one or a list of files", |
|
|
|
|
|
Long: `upload one or a list of files. |
|
|
|
|
|
It uses consecutive file keys for the list of files. |
|
|
e.g. If the file1 uses key k, file2 can be read via k_1 |
|
|
e.g. If the file1 uses key k, file2 can be read via k_1 |
|
|
|
|
|
|
|
|
`, |
|
|
`, |
|
@ -35,14 +38,18 @@ type AssignResult struct { |
|
|
Fid string "fid" |
|
|
Fid string "fid" |
|
|
Url string "url" |
|
|
Url string "url" |
|
|
PublicUrl string "publicUrl" |
|
|
PublicUrl string "publicUrl" |
|
|
Count int `json:",string"` |
|
|
|
|
|
|
|
|
Count int |
|
|
Error string "error" |
|
|
Error string "error" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func assign(count int) (*AssignResult, error) { |
|
|
func assign(count int) (*AssignResult, error) { |
|
|
values := make(url.Values) |
|
|
values := make(url.Values) |
|
|
values.Add("count", strconv.Itoa(count)) |
|
|
values.Add("count", strconv.Itoa(count)) |
|
|
jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values) |
|
|
|
|
|
|
|
|
values.Add("replication", *uploadReplication) |
|
|
|
|
|
jsonBlob, err := util.Post("http://"+*server+"/dir/assign2", values) |
|
|
|
|
|
if *IsDebug { |
|
|
|
|
|
fmt.Println("debug", *IsDebug, "assign result :", string(jsonBlob)) |
|
|
|
|
|
} |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
@ -62,14 +69,23 @@ type UploadResult struct { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func upload(filename string, uploadUrl string) (int, string) { |
|
|
func upload(filename string, uploadUrl string) (int, string) { |
|
|
|
|
|
if *IsDebug { |
|
|
|
|
|
fmt.Println("Start uploading file:", filename) |
|
|
|
|
|
} |
|
|
body_buf := bytes.NewBufferString("") |
|
|
body_buf := bytes.NewBufferString("") |
|
|
body_writer := multipart.NewWriter(body_buf) |
|
|
body_writer := multipart.NewWriter(body_buf) |
|
|
file_writer, err := body_writer.CreateFormFile("file", filename) |
|
|
file_writer, err := body_writer.CreateFormFile("file", filename) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
|
|
|
if *IsDebug { |
|
|
|
|
|
fmt.Println("Failed to create form file:", filename) |
|
|
|
|
|
} |
|
|
panic(err.Error()) |
|
|
panic(err.Error()) |
|
|
} |
|
|
} |
|
|
fh, err := os.Open(filename) |
|
|
fh, err := os.Open(filename) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
|
|
|
if *IsDebug { |
|
|
|
|
|
fmt.Println("Failed to open file:", filename) |
|
|
|
|
|
} |
|
|
panic(err.Error()) |
|
|
panic(err.Error()) |
|
|
} |
|
|
} |
|
|
io.Copy(file_writer, fh) |
|
|
io.Copy(file_writer, fh) |
|
@ -77,10 +93,16 @@ func upload(filename string, uploadUrl string) (int, string) { |
|
|
body_writer.Close() |
|
|
body_writer.Close() |
|
|
resp, err := http.Post(uploadUrl, content_type, body_buf) |
|
|
resp, err := http.Post(uploadUrl, content_type, body_buf) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
|
|
|
if *IsDebug { |
|
|
|
|
|
fmt.Println("Failed to upload file to", uploadUrl) |
|
|
|
|
|
} |
|
|
panic(err.Error()) |
|
|
panic(err.Error()) |
|
|
} |
|
|
} |
|
|
defer resp.Body.Close() |
|
|
defer resp.Body.Close() |
|
|
resp_body, err := ioutil.ReadAll(resp.Body) |
|
|
resp_body, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
if *IsDebug { |
|
|
|
|
|
fmt.Println("Upload response:", string(resp_body)) |
|
|
|
|
|
} |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
panic(err.Error()) |
|
|
panic(err.Error()) |
|
|
} |
|
|
} |
|
@ -98,7 +120,7 @@ type SubmitResult struct { |
|
|
Size int "size" |
|
|
Size int "size" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func submit(files []string)([]SubmitResult) { |
|
|
|
|
|
|
|
|
func submit(files []string) []SubmitResult { |
|
|
ret, err := assign(len(files)) |
|
|
ret, err := assign(len(files)) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
panic(err) |
|
|
panic(err) |
|
@ -117,10 +139,11 @@ func submit(files []string)([]SubmitResult) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func runUpload(cmd *Command, args []string) bool { |
|
|
func runUpload(cmd *Command, args []string) bool { |
|
|
|
|
|
*IsDebug = true |
|
|
if len(cmdUpload.Flag.Args()) == 0 { |
|
|
if len(cmdUpload.Flag.Args()) == 0 { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
results := submit(flag.Args()) |
|
|
|
|
|
|
|
|
results := submit(args) |
|
|
bytes, _ := json.Marshal(results) |
|
|
bytes, _ := json.Marshal(results) |
|
|
fmt.Print(string(bytes)) |
|
|
fmt.Print(string(bytes)) |
|
|
return true |
|
|
return true |
|
|