2012-08-07 08:29:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-09-20 09:11:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2012-09-21 00:58:29 +00:00
|
|
|
"pkg/operation"
|
2012-09-20 09:11:08 +00:00
|
|
|
"pkg/util"
|
|
|
|
"strconv"
|
2012-08-07 08:29:22 +00:00
|
|
|
)
|
|
|
|
|
2012-09-20 09:11:08 +00:00
|
|
|
var uploadReplication *string
|
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
func init() {
|
2012-09-20 09:11:08 +00:00
|
|
|
cmdUpload.Run = runUpload // break init cycle
|
|
|
|
IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
|
|
|
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
|
|
|
|
uploadReplication = cmdUpload.Flag.String("replication", "00", "replication type(00,01,10,11)")
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdUpload = &Command{
|
2012-09-20 09:11:08 +00:00
|
|
|
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.
|
2012-08-07 08:29:22 +00:00
|
|
|
e.g. If the file1 uses key k, file2 can be read via k_1
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
type AssignResult struct {
|
2012-09-20 09:11:08 +00:00
|
|
|
Fid string "fid"
|
|
|
|
Url string "url"
|
|
|
|
PublicUrl string "publicUrl"
|
|
|
|
Count int
|
|
|
|
Error string "error"
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
func assign(count int) (*AssignResult, error) {
|
2012-09-20 09:11:08 +00:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Add("count", strconv.Itoa(count))
|
|
|
|
values.Add("replication", *uploadReplication)
|
2012-09-25 22:37:13 +00:00
|
|
|
jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values)
|
2012-09-20 09:11:08 +00:00
|
|
|
if *IsDebug {
|
2012-09-26 20:38:45 +00:00
|
|
|
fmt.Println("assign result :", string(jsonBlob))
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ret AssignResult
|
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ret.Count <= 0 {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 00:58:29 +00:00
|
|
|
func upload(filename string, server string, fid string) (int) {
|
2012-09-20 09:11:08 +00:00
|
|
|
if *IsDebug {
|
|
|
|
fmt.Println("Start uploading file:", filename)
|
|
|
|
}
|
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
if *IsDebug {
|
|
|
|
fmt.Println("Failed to open file:", filename)
|
|
|
|
}
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2012-09-21 04:03:40 +00:00
|
|
|
ret, _ := operation.Upload("http://"+server+"/"+fid, filename, fh)
|
2012-09-21 00:58:29 +00:00
|
|
|
return ret.Size
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SubmitResult struct {
|
2012-09-20 09:11:08 +00:00
|
|
|
Fid string "fid"
|
|
|
|
Size int "size"
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 09:11:08 +00:00
|
|
|
func submit(files []string) []SubmitResult {
|
|
|
|
ret, err := assign(len(files))
|
|
|
|
if err != nil {
|
2012-09-26 20:38:45 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return nil
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
|
|
|
results := make([]SubmitResult, len(files))
|
|
|
|
for index, file := range files {
|
|
|
|
fid := ret.Fid
|
|
|
|
if index > 0 {
|
|
|
|
fid = fid + "_" + strconv.Itoa(index)
|
|
|
|
}
|
2012-09-21 00:58:29 +00:00
|
|
|
results[index].Size = upload(file, ret.PublicUrl, fid)
|
2012-09-20 09:11:08 +00:00
|
|
|
results[index].Fid = fid
|
|
|
|
}
|
|
|
|
return results
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runUpload(cmd *Command, args []string) bool {
|
2012-09-20 09:11:08 +00:00
|
|
|
*IsDebug = true
|
|
|
|
if len(cmdUpload.Flag.Args()) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
results := submit(args)
|
|
|
|
bytes, _ := json.Marshal(results)
|
|
|
|
fmt.Print(string(bytes))
|
|
|
|
return true
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|