aboutsummaryrefslogtreecommitdiff
path: root/src/weed/operation/upload_content.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2013-02-10 03:09:26 -0800
committerChris Lu <chris.lu@gmail.com>2013-02-10 03:09:26 -0800
commitcb4e8ec16b5c204718f1efdc1731f1bdd5698ff3 (patch)
tree47f27040c3d80e6849932bf2acf54b68c930f72b /src/weed/operation/upload_content.go
parentd3b267bac27018b7f70dfec7c258d0556fff4c14 (diff)
downloadseaweedfs-cb4e8ec16b5c204718f1efdc1731f1bdd5698ff3.tar.xz
seaweedfs-cb4e8ec16b5c204718f1efdc1731f1bdd5698ff3.zip
re-organize code directory structure
Diffstat (limited to 'src/weed/operation/upload_content.go')
-rw-r--r--src/weed/operation/upload_content.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/weed/operation/upload_content.go b/src/weed/operation/upload_content.go
new file mode 100644
index 000000000..0bdb697da
--- /dev/null
+++ b/src/weed/operation/upload_content.go
@@ -0,0 +1,47 @@
+package operation
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ _ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "mime/multipart"
+ "net/http"
+)
+
+type UploadResult struct {
+ Size int
+ Error string
+}
+
+func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) {
+ body_buf := bytes.NewBufferString("")
+ body_writer := multipart.NewWriter(body_buf)
+ file_writer, err := body_writer.CreateFormFile("file", filename)
+ io.Copy(file_writer, reader)
+ content_type := body_writer.FormDataContentType()
+ body_writer.Close()
+ resp, err := http.Post(uploadUrl, content_type, body_buf)
+ if err != nil {
+ log.Println("failing to upload to", uploadUrl)
+ return nil, err
+ }
+ defer resp.Body.Close()
+ resp_body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ var ret UploadResult
+ err = json.Unmarshal(resp_body, &ret)
+ if err != nil {
+ log.Println("failing to read upload resonse", uploadUrl, resp_body)
+ return nil, err
+ }
+ if ret.Error != "" {
+ return nil, errors.New(ret.Error)
+ }
+ return &ret, nil
+}