mirror of https://github.com/perkeep/perkeep.git
preupload request in upload client
This commit is contained in:
parent
b6751946ac
commit
785ee292f7
|
@ -5,11 +5,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"http"
|
||||||
"io"
|
"io"
|
||||||
|
"json"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var flagFile *string = flag.String("file", "", "file to upload")
|
var flagFile *string = flag.String("file", "", "file to upload")
|
||||||
|
@ -29,9 +33,58 @@ func NewAgent(server string) *Agent {
|
||||||
return &Agent{server}
|
return &Agent{server}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) Upload(handle *UploadHandle) {
|
func (a *Agent) Upload(h *UploadHandle) {
|
||||||
// TODO
|
url := fmt.Sprintf("%s/camli/preupload", a.server)
|
||||||
fmt.Println("Need to upload: ", handle)
|
fmt.Println("Need to upload: ", h, "to", url)
|
||||||
|
|
||||||
|
error := func(msg string, e os.Error) {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s on %v: %v\n", msg, h.blobref, e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post(
|
||||||
|
url,
|
||||||
|
"application/x-www-form-urlencoded",
|
||||||
|
strings.NewReader("camliversion=1&blob1="+h.blobref))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Upload error for %v: %v\n",
|
||||||
|
h.blobref, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Got response:", resp)
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
io.Copy(buf, resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
pur := make(map[string]interface{})
|
||||||
|
jerr := json.Unmarshal(buf.Bytes(), &pur)
|
||||||
|
if jerr != nil {
|
||||||
|
error("preupload parse error", jerr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadUrl, ok := pur["uploadUrl"].(string)
|
||||||
|
if uploadUrl == "" {
|
||||||
|
error("no uploadUrl in preupload response", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
alreadyHave, ok := pur["alreadyHave"].([]interface{})
|
||||||
|
if !ok {
|
||||||
|
error("no alreadyHave array in preupload response", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, haveObj := range alreadyHave {
|
||||||
|
haveObj := haveObj.(map[string]interface{})
|
||||||
|
if haveObj["blobRef"].(string) == h.blobref {
|
||||||
|
fmt.Println("already have it!")
|
||||||
|
// TODO: signal success
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("preupload done:", pur, alreadyHave)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) Wait() int {
|
func (a *Agent) Wait() int {
|
||||||
|
@ -60,11 +113,17 @@ func uploadFile(agent *Agent, filename string) os.Error {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Remove trailing slash if provided.
|
||||||
|
if strings.HasSuffix(*flagServer, "/") {
|
||||||
|
*flagServer = (*flagServer)[0 : len(*flagServer)-1]
|
||||||
|
}
|
||||||
|
|
||||||
agent := NewAgent(*flagServer)
|
agent := NewAgent(*flagServer)
|
||||||
if *flagFile != "" {
|
if *flagFile != "" {
|
||||||
uploadFile(agent, *flagFile)
|
uploadFile(agent, *flagFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := agent.Wait()
|
stats := agent.Wait()
|
||||||
fmt.Println("Done uploading; stats:", stats)
|
fmt.Println("Done uploading; stats:", stats)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue