aboutsummaryrefslogtreecommitdiff
path: root/weed-fs/src/cmd
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2012-09-20 17:58:29 -0700
committerChris Lu <chris.lu@gmail.com>2012-09-20 17:58:29 -0700
commit5e97179d061fd885ab5df0d91c1713a5139ca112 (patch)
tree125f4d7900509d09eed08a57dc969a64cef77767 /weed-fs/src/cmd
parent7d8e9f829c11dd8151144e20506e673d2f3b9d27 (diff)
downloadseaweedfs-5e97179d061fd885ab5df0d91c1713a5139ca112.tar.xz
seaweedfs-5e97179d061fd885ab5df0d91c1713a5139ca112.zip
refactoring content uploading
Diffstat (limited to 'weed-fs/src/cmd')
-rw-r--r--weed-fs/src/cmd/weed/upload.go51
-rw-r--r--weed-fs/src/cmd/weed/volume.go24
2 files changed, 29 insertions, 46 deletions
diff --git a/weed-fs/src/cmd/weed/upload.go b/weed-fs/src/cmd/weed/upload.go
index 515816921..c025c5029 100644
--- a/weed-fs/src/cmd/weed/upload.go
+++ b/weed-fs/src/cmd/weed/upload.go
@@ -1,16 +1,12 @@
package main
import (
- "bytes"
"encoding/json"
"errors"
"fmt"
- "io"
- "io/ioutil"
- "mime/multipart"
- "net/http"
"net/url"
"os"
+ "pkg/operation"
"pkg/util"
"strconv"
)
@@ -64,23 +60,10 @@ func assign(count int) (*AssignResult, error) {
return &ret, nil
}
-type UploadResult struct {
- Size int
-}
-
-func upload(filename string, uploadUrl string) (int, string) {
+func upload(filename string, server string, fid string) (int) {
if *IsDebug {
fmt.Println("Start uploading file:", filename)
}
- body_buf := bytes.NewBufferString("")
- body_writer := multipart.NewWriter(body_buf)
- file_writer, err := body_writer.CreateFormFile("file", filename)
- if err != nil {
- if *IsDebug {
- fmt.Println("Failed to create form file:", filename)
- }
- panic(err.Error())
- }
fh, err := os.Open(filename)
if err != nil {
if *IsDebug {
@@ -88,31 +71,8 @@ func upload(filename string, uploadUrl string) (int, string) {
}
panic(err.Error())
}
- io.Copy(file_writer, fh)
- content_type := body_writer.FormDataContentType()
- body_writer.Close()
- resp, err := http.Post(uploadUrl, content_type, body_buf)
- if err != nil {
- if *IsDebug {
- fmt.Println("Failed to upload file to", uploadUrl)
- }
- panic(err.Error())
- }
- defer resp.Body.Close()
- resp_body, err := ioutil.ReadAll(resp.Body)
- if *IsDebug {
- fmt.Println("Upload response:", string(resp_body))
- }
- if err != nil {
- panic(err.Error())
- }
- var ret UploadResult
- err = json.Unmarshal(resp_body, &ret)
- if err != nil {
- panic(err.Error())
- }
- //fmt.Println("Uploaded " + strconv.Itoa(ret.Size) + " Bytes to " + uploadUrl)
- return ret.Size, uploadUrl
+ ret, _ := operation.Upload(server, fid, filename, fh)
+ return ret.Size
}
type SubmitResult struct {
@@ -131,8 +91,7 @@ func submit(files []string) []SubmitResult {
if index > 0 {
fid = fid + "_" + strconv.Itoa(index)
}
- uploadUrl := "http://" + ret.PublicUrl + "/" + fid
- results[index].Size, _ = upload(file, uploadUrl)
+ results[index].Size = upload(file, ret.PublicUrl, fid)
results[index].Fid = fid
}
return results
diff --git a/weed-fs/src/cmd/weed/volume.go b/weed-fs/src/cmd/weed/volume.go
index 3b41a60d1..af4ec8d5e 100644
--- a/weed-fs/src/cmd/weed/volume.go
+++ b/weed-fs/src/cmd/weed/volume.go
@@ -137,6 +137,30 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
writeJson(w, r, ne)
} else {
ret := store.Write(volumeId, needle)
+ if ret > 0 { //send to other replica locations
+ if r.FormValue("type") != "standard" {
+ waitTime, err := strconv.Atoi(r.FormValue("wait"))
+ lookupResult, lookupErr := operation.Lookup(*server, volumeId)
+ if lookupErr == nil {
+ sendFunc := func(background bool) {
+ postContentFunc := func(location operation.Location) bool{
+
+ return true
+ }
+ for _, location := range lookupResult.Locations {
+ if background {
+ go postContentFunc(location)
+ }else{
+ postContentFunc(location)
+ }
+ }
+ }
+ sendFunc(err == nil && waitTime > 0)
+ } else {
+ log.Println("Failed to lookup for", volumeId, lookupErr.Error())
+ }
+ }
+ }
m := make(map[string]uint32)
m["size"] = ret
writeJson(w, r, m)