aboutsummaryrefslogtreecommitdiff
path: root/go/weed/upload.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2013-08-13 23:26:51 -0700
committerChris Lu <chris.lu@gmail.com>2013-08-13 23:26:51 -0700
commitd5e7c1de0ae4667d7362b869081eeeb6085a426b (patch)
tree843cdfc3be3245ec071c8fd8b556c13f89d21a69 /go/weed/upload.go
parentf7c1a15ad10dd48f4cd7042bded13cbe67bfb83d (diff)
downloadseaweedfs-d5e7c1de0ae4667d7362b869081eeeb6085a426b.tar.xz
seaweedfs-d5e7c1de0ae4667d7362b869081eeeb6085a426b.zip
refactoring code
reusable code by go clients
Diffstat (limited to 'go/weed/upload.go')
-rw-r--r--go/weed/upload.go102
1 files changed, 2 insertions, 100 deletions
diff --git a/go/weed/upload.go b/go/weed/upload.go
index 54c566ca7..8c0469595 100644
--- a/go/weed/upload.go
+++ b/go/weed/upload.go
@@ -2,17 +2,10 @@ package main
import (
"code.google.com/p/weed-fs/go/operation"
- "code.google.com/p/weed-fs/go/util"
"encoding/json"
- "errors"
"fmt"
- "mime"
- "net/url"
"os"
- "path"
"path/filepath"
- "strconv"
- "strings"
)
var (
@@ -50,97 +43,6 @@ var cmdUpload = &Command{
`,
}
-type AssignResult struct {
- Fid string `json:"fid"`
- Url string `json:"url"`
- PublicUrl string `json:"publicUrl"`
- Count int
- Error string `json:"error"`
-}
-
-func Assign(server string, count int) (*AssignResult, error) {
- values := make(url.Values)
- values.Add("count", strconv.Itoa(count))
- if *uploadReplication != "" {
- values.Add("replication", *uploadReplication)
- }
- jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
- 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
-}
-
-func upload(filename string, server string, fid string) (int, error) {
- debug("Start uploading file:", filename)
- fh, err := os.Open(filename)
- if err != nil {
- debug("Failed to open file:", filename)
- return 0, err
- }
- fi, fiErr := fh.Stat()
- if fiErr != nil {
- debug("Failed to stat file:", filename)
- return 0, fiErr
- }
- filename = path.Base(filename)
- isGzipped := path.Ext(filename) == ".gz"
- if isGzipped {
- filename = filename[0 : len(filename)-3]
- }
- mtype := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
- ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), filename, fh, isGzipped, mtype)
- if e != nil {
- return 0, e
- }
- return ret.Size, e
-}
-
-type SubmitResult struct {
- 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(*server, 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)
- }
- results[index].Size, err = upload(file, ret.PublicUrl, fid)
- if err != nil {
- fid = ""
- results[index].Error = err.Error()
- }
- results[index].Fid = fid
- results[index].FileUrl = ret.PublicUrl + "/" + fid
- }
- return results, nil
-}
-
func runUpload(cmd *Command, args []string) bool {
if len(cmdUpload.Flag.Args()) == 0 {
if *uploadDir == "" {
@@ -154,7 +56,7 @@ func runUpload(cmd *Command, args []string) bool {
return nil
}
}
- results, e := submit([]string{path})
+ results, e := operation.SubmitFiles(*server, []string{path}, *uploadReplication)
bytes, _ := json.Marshal(results)
fmt.Println(string(bytes))
if e != nil {
@@ -167,7 +69,7 @@ func runUpload(cmd *Command, args []string) bool {
return err
})
} else {
- results, _ := submit(args)
+ results, _ := operation.SubmitFiles(*server, args, *uploadReplication)
bytes, _ := json.Marshal(results)
fmt.Println(string(bytes))
}