Ejemplo n.º 1
0
// curl -X DELETE http://localhost:8888/path/to
// curl -X DELETE http://localhost:8888/path/to?recursive=true
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
	var err error
	var fid string
	if strings.HasSuffix(r.URL.Path, "/") {
		isRecursive := r.FormValue("recursive") == "true"
		err = fs.filer.DeleteDirectory(r.URL.Path, isRecursive)
	} else {
		fid, err = fs.filer.DeleteFile(r.URL.Path)
		if err == nil {
			err = operation.DeleteFile(fs.master, fid)
		}
	}
	if err == nil {
		w.WriteHeader(http.StatusAccepted)
		writeJsonQuiet(w, r, map[string]string{"error": ""})
	} else {
		glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
		writeJsonError(w, r, err)
	}
}
Ejemplo n.º 2
0
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
	query := r.URL.Query()
	assignResult, ae := operation.Assign(fs.master, 1, query.Get("replication"), fs.collection)
	if ae != nil {
		glog.V(0).Infoln("failing to assign a file id", ae.Error())
		writeJsonError(w, r, ae)
		return
	}

	u, _ := url.Parse("http://" + assignResult.PublicUrl + "/" + assignResult.Fid)
	glog.V(4).Infoln("post to", u)
	request := &http.Request{
		Method:        r.Method,
		URL:           u,
		Proto:         r.Proto,
		ProtoMajor:    r.ProtoMajor,
		ProtoMinor:    r.ProtoMinor,
		Header:        r.Header,
		Body:          r.Body,
		Host:          r.Host,
		ContentLength: r.ContentLength,
	}
	resp, do_err := util.Do(request)
	if do_err != nil {
		glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
		writeJsonError(w, r, do_err)
		return
	}
	defer resp.Body.Close()
	resp_body, ra_err := ioutil.ReadAll(resp.Body)
	if ra_err != nil {
		glog.V(0).Infoln("failing to upload to volume server", ra_err.Error())
		writeJsonError(w, r, ra_err)
		return
	}
	glog.V(4).Infoln("post result", string(resp_body))
	var ret operation.UploadResult
	unmarshal_err := json.Unmarshal(resp_body, &ret)
	if unmarshal_err != nil {
		glog.V(0).Infoln("failing to read upload resonse", string(resp_body))
		writeJsonError(w, r, unmarshal_err)
		return
	}
	if ret.Error != "" {
		glog.V(0).Infoln("failing to post to volume server", ret.Error)
		writeJsonError(w, r, errors.New(ret.Error))
		return
	}
	path := r.URL.Path
	if strings.HasSuffix(path, "/") {
		if ret.Name != "" {
			path += ret.Name
		} else {
			operation.DeleteFile(fs.master, assignResult.Fid) //clean up
			glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
			writeJsonError(w, r, errors.New("Can not to write to folder "+path+" without a file name"))
			return
		}
	}
	glog.V(4).Infoln("saving", path, "=>", assignResult.Fid)
	if db_err := fs.filer.CreateFile(path, assignResult.Fid); db_err != nil {
		operation.DeleteFile(fs.master, assignResult.Fid) //clean up
		glog.V(0).Infoln("failing to write to filer server", db_err.Error())
		writeJsonError(w, r, db_err)
		return
	}
	w.WriteHeader(http.StatusCreated)
}