コード例 #1
0
func (ss *StoreServer) uploadHandler(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	vars := mux.Vars(r)
	fileIDStr := vars["fileID"]
	volID, needleID, cookie, err := newFileID(fileIDStr)
	if err != nil {
		helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
		return
	}
	if ss.volumeMap[volID] == nil {
		helper.WriteJson(w, result{Error: fmt.Sprintf("no volume %d", volID)}, http.StatusInternalServerError)
		return
	}
	data, name, err := parseUpload(r)
	if err != nil {
		helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
		return
	}
	n := storage.NewNeedle(cookie, needleID, data, name)
	if err = ss.volumeMap[volID].AppendNeedle(n); err != nil {
		helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
		return
	}

	fi, _ := ss.volumeMap[volID].StoreFile.Stat()
	vi := volumeInfo{
		ID:   volID,
		Size: fi.Size(),
	}
	viBytes, _ := json.Marshal(vi)
	for i := range ss.conf.Directories { // send volume information to directory server
		var b bytes.Buffer
		b.Write(viBytes)
		_, err := postAndError("http://"+ss.conf.Directories[i]+"/vol/info", "application/json", &b)
		if err == nil {
			break
		} else {
			log4go.Warn("send volumeInfo to directory get err: %s", err.Error())
		}
	}
	for _, localVolIDIP := range ss.localVolIDIPs {
		if localVolIDIP.ID == volID {
			for _, ip := range localVolIDIP.IP {
				if ip != ss.Addr {
					if err = replicateUpload(fmt.Sprintf("http://%s/replicate/%s", ip, fileIDStr), string(name), data); err != nil {
						helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
						return
					}
				}
			}
			break
		}
	}
	res := result{
		Name: string(name),
		Size: len(data),
	}
	helper.WriteJson(w, res, http.StatusOK)

}
コード例 #2
0
func (ss *StoreServer) replicateUploadHandler(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	vars := mux.Vars(r)
	fileIDStr := vars["fileID"]
	volID, needleID, cookie, err := newFileID(fileIDStr)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if ss.volumeMap[volID] == nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data, name, err := parseUpload(r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	n := storage.NewNeedle(cookie, needleID, data, name)
	if err = ss.volumeMap[volID].AppendNeedle(n); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}