func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) { //只返回目录 if strings.HasSuffix(r.URL.Path, "/") { fs.listDirectoryHandler(w, r) return } fileId, err := fs.filer.FindFile(r.URL.Path) if err == leveldb.ErrNotFound { glog.V(3).Infoln("Not found in db", r.URL.Path) w.WriteHeader(http.StatusNotFound) return } parts := strings.Split(fileId, ",") //3,12345 if len(parts) != 2 { glog.V(1).Infoln("Invalid fileId", fileId) w.WriteHeader(http.StatusNotFound) return } lookup, lookupError := operation.Lookup(fs.master, parts[0]) if lookupError != nil { glog.V(1).Infoln("Invalid lookup", lookupError.Error()) w.WriteHeader(http.StatusNotFound) return } if len(lookup.Locations) == 0 { glog.V(1).Infoln("Can not find location for volume", parts[0]) w.WriteHeader(http.StatusNotFound) return } //从合适的DataNode中随机选择一个 从中读取fileId指定的文件 urlLocation := lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl u, _ := url.Parse("http://" + urlLocation + "/" + fileId) 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, } glog.V(3).Infoln("retrieving from", u) 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() for k, v := range resp.Header { w.Header()[k] = v } w.WriteHeader(resp.StatusCode) io.Copy(w, resp.Body) }
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() //获得一个fileId assignResult, ae := operation.Assign(fs.master, 1, query.Get("replication"), fs.collection, query.Get("ttl")) 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", r.RequestURI, 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", r.RequestURI, 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", r.RequestURI, string(resp_body)) writeJsonError(w, r, unmarshal_err) return } if ret.Error != "" { glog.V(0).Infoln("failing to post to volume server", r.RequestURI, 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", r.RequestURI, db_err.Error()) writeJsonError(w, r, db_err) return } w.WriteHeader(http.StatusCreated) }