示例#1
0
文件: volume.go 项目: pipul/weed-fs
func PostHandler(w http.ResponseWriter, r *http.Request) {
	if e := r.ParseForm(); e != nil {
		debug("form parse error:", e)
		writeJsonQuiet(w, r, e)
		return
	}
	vid, _, _, _ := parseURLPath(r.URL.Path)
	volumeId, e := storage.NewVolumeId(vid)
	if e != nil {
		debug("NewVolumeId error:", e)
		writeJsonQuiet(w, r, e)
		return
	}
	if e != nil {
		writeJsonQuiet(w, r, e)
	} else {
		needle, ne := storage.NewNeedle(r)
		if ne != nil {
			writeJsonQuiet(w, r, ne)
		} else {
			ret, errorStatus := replication.ReplicatedWrite(*masterNode, store, volumeId, needle, r)
			m := make(map[string]interface{})
			if errorStatus == "" {
				w.WriteHeader(http.StatusCreated)
			} else {
				w.WriteHeader(http.StatusInternalServerError)
				m["error"] = errorStatus
			}
			m["size"] = ret
			writeJsonQuiet(w, r, m)
		}
	}
}
func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
	if e := r.ParseForm(); e != nil {
		glog.V(0).Infoln("form parse error:", e)
		writeJsonError(w, r, e)
		return
	}
	vid, _, _, _, _ := parseURLPath(r.URL.Path)
	volumeId, ve := storage.NewVolumeId(vid)
	if ve != nil {
		glog.V(0).Infoln("NewVolumeId error:", ve)
		writeJsonError(w, r, ve)
		return
	}
	needle, ne := storage.NewNeedle(r)
	if ne != nil {
		writeJsonError(w, r, ne)
		return
	}

	ret := operation.UploadResult{}
	size, errorStatus := topology.ReplicatedWrite(vs.masterNode, vs.store, volumeId, needle, r)
	if errorStatus == "" {
		w.WriteHeader(http.StatusCreated)
	} else {
		w.WriteHeader(http.StatusInternalServerError)
		ret.Error = errorStatus
	}
	if needle.HasName() {
		ret.Name = string(needle.Name)
	}
	ret.Size = size
	writeJsonQuiet(w, r, ret)
}
示例#3
0
文件: volume.go 项目: abasse/weed-fs
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, err := store.Write(volumeId, needle)
			errorStatus := ""
			needToReplicate := !store.HasVolume(volumeId)
			if err != nil {
				errorStatus = "Failed to write to local disk (" + err.Error() + ")"
			} else 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)
		}
	}
}