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 } ret, e := operation.Upload("http://"+server+"/"+fid, filename, fh) if e != nil { return 0, e } return ret.Size, e }
func PostHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() vid, _, _ := parseURLPath(r.URL.Path) volumeId, e := storage.NewVolumeId(vid) if e != nil { writeJson(w, r, e) } else { needle, filename, ne := storage.NewNeedle(r) if ne != nil { writeJson(w, r, ne) } else { ret := store.Write(volumeId, needle) errorStatus := "" needToReplicate := !store.HasVolume(volumeId) if ret > 0 { needToReplicate = needToReplicate || store.GetVolume(volumeId).NeedToReplicate() } else { errorStatus = "Failed to write to local disk" } if !needToReplicate && ret > 0 { needToReplicate = store.GetVolume(volumeId).NeedToReplicate() } if needToReplicate { //send to other replica locations if r.FormValue("type") != "standard" { if !distributedOperation(volumeId, func(location operation.Location) bool { _, err := operation.Upload("http://"+location.Url+r.URL.Path+"?type=standard", filename, bytes.NewReader(needle.Data)) return err == nil }) { ret = 0 errorStatus = "Failed to write to replicas for volume " + volumeId.String() } } } m := make(map[string]interface{}) if errorStatus == "" { w.WriteHeader(http.StatusCreated) } else { store.Delete(volumeId, needle) distributedOperation(volumeId, func(location operation.Location) bool { return nil == operation.Delete("http://"+location.Url+r.URL.Path+"?type=standard") }) w.WriteHeader(http.StatusInternalServerError) m["error"] = errorStatus } m["size"] = ret writeJson(w, r, m) } } }