Chris Lu
12 years ago
3 changed files with 88 additions and 57 deletions
-
67weed-fs/src/cmd/weed/volume.go
-
14weed-fs/src/pkg/operation/delete_content.go
-
64weed-fs/src/pkg/operation/upload_content.go
@ -0,0 +1,14 @@ |
|||||
|
package operation |
||||
|
|
||||
|
import ( |
||||
|
"net/http" |
||||
|
) |
||||
|
|
||||
|
func Delete(url string) error { |
||||
|
req, err := http.NewRequest("DELETE", url, nil) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
_, err = http.DefaultClient.Do(req) |
||||
|
return err |
||||
|
} |
@ -1,42 +1,42 @@ |
|||||
package operation |
package operation |
||||
|
|
||||
import ( |
import ( |
||||
"bytes" |
|
||||
"encoding/json" |
|
||||
"mime/multipart" |
|
||||
"net/http" |
|
||||
_ "fmt" |
|
||||
"io" |
|
||||
"io/ioutil" |
|
||||
|
"bytes" |
||||
|
"encoding/json" |
||||
|
_ "fmt" |
||||
|
"io" |
||||
|
"io/ioutil" |
||||
|
"mime/multipart" |
||||
|
"net/http" |
||||
) |
) |
||||
|
|
||||
type UploadResult struct { |
type UploadResult struct { |
||||
Size int |
|
||||
|
Size int |
||||
} |
} |
||||
|
|
||||
func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) { |
func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) { |
||||
println("uploading to", uploadUrl) |
|
||||
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 { |
|
||||
return nil, err |
|
||||
} |
|
||||
defer resp.Body.Close() |
|
||||
resp_body, err := ioutil.ReadAll(resp.Body) |
|
||||
if err != nil { |
|
||||
return nil, err |
|
||||
} |
|
||||
var ret UploadResult |
|
||||
println("upload response to", uploadUrl, resp_body) |
|
||||
err = json.Unmarshal(resp_body, &ret) |
|
||||
if err != nil { |
|
||||
panic(err.Error()) |
|
||||
} |
|
||||
//fmt.Println("Uploaded " + strconv.Itoa(ret.Size) + " Bytes to " + uploadUrl)
|
|
||||
return &ret, nil |
|
||||
|
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 { |
||||
|
println("uploading 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 { |
||||
|
println("upload response to", uploadUrl, resp_body) |
||||
|
panic(err.Error()) |
||||
|
} |
||||
|
//fmt.Println("Uploaded " + strconv.Itoa(ret.Size) + " Bytes to " + uploadUrl)
|
||||
|
return &ret, nil |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue