seaweedfs/go/weed/upload.go

149 lines
3.6 KiB
Go
Raw Normal View History

package main
import (
2013-02-27 06:54:22 +00:00
"code.google.com/p/weed-fs/go/operation"
"code.google.com/p/weed-fs/go/util"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
)
var (
uploadReplication *string
uploadDir *string
)
func init() {
cmdUpload.Run = runUpload // break init cycle
2013-01-20 03:49:57 +00:00
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
2012-09-30 09:20:33 +00:00
uploadReplication = cmdUpload.Flag.String("replication", "000", "replication type(000,001,010,100,110,200)")
}
var cmdUpload = &Command{
UsageLine: "upload -server=localhost:9333 file1 [file2 file3]\n upload -server=localhost:9333 -dir=one_directory",
Short: "upload one or a list of files",
Long: `upload one or a list of files, or batch upload one whole folder recursively.
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
`,
}
type AssignResult struct {
2013-02-10 17:44:44 +00:00
Fid string `json:"fid"`
Url string `json:"url"`
PublicUrl string `json:"publicUrl"`
Count int
2013-02-10 17:44:44 +00:00
Error string `json:"error"`
}
func assign(count int) (*AssignResult, error) {
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-27 19:17:27 +00:00
debug("assign result :", string(jsonBlob))
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-09-26 21:28:46 +00:00
func upload(filename string, server string, fid string) (int, error) {
2012-09-27 19:17:27 +00:00
debug("Start uploading file:", filename)
fh, err := os.Open(filename)
if err != nil {
2012-09-27 19:17:27 +00:00
debug("Failed to open file:", filename)
2012-09-26 21:28:46 +00:00
return 0, err
}
fi, fiErr := fh.Stat()
2013-07-12 05:45:29 +00:00
if fiErr != nil {
debug("Failed to stat file:", filename)
return 0, fiErr
}
2013-07-12 05:45:29 +00:00
ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), path.Base(filename), fh)
2012-09-26 21:28:46 +00:00
if e != nil {
2013-01-17 08:56:56 +00:00
return 0, e
2012-09-26 21:28:46 +00:00
}
return ret.Size, e
}
type SubmitResult struct {
2013-07-12 05:45:29 +00:00
FileName string `json:"fileName"`
FileUrl string `json:"fileUrl"`
Fid string `json:"fid"`
Size int `json:"size"`
Error string `json:"error"`
}
func submit(files []string) ([]SubmitResult, error) {
results := make([]SubmitResult, len(files))
for index, file := range files {
results[index].FileName = file
}
ret, err := assign(len(files))
if err != nil {
for index, _ := range files {
results[index].Error = err.Error()
}
return results, err
}
for index, file := range files {
fid := ret.Fid
if index > 0 {
fid = fid + "_" + strconv.Itoa(index)
}
2012-09-26 21:28:46 +00:00
results[index].Size, err = upload(file, ret.PublicUrl, fid)
if err != nil {
fid = ""
results[index].Error = err.Error()
}
results[index].Fid = fid
2013-07-12 05:45:29 +00:00
results[index].FileUrl = ret.PublicUrl + "/" + fid
}
return results, nil
}
func runUpload(cmd *Command, args []string) bool {
if len(cmdUpload.Flag.Args()) == 0 {
if *uploadDir == "" {
return false
}
filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
if err == nil {
if !info.IsDir() {
results, e := submit([]string{path})
bytes, _ := json.Marshal(results)
fmt.Println(string(bytes))
if e != nil {
return e
}
}
} else {
fmt.Println(err)
}
return err
})
} else {
results, _ := submit(args)
bytes, _ := json.Marshal(results)
fmt.Println(string(bytes))
}
return true
}