refactoring code

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@24 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
chris.lu@gmail.com
2011-12-25 05:30:57 +00:00
parent 6813f118d4
commit ae3a53388f
7 changed files with 56 additions and 61 deletions

View File

@@ -38,19 +38,8 @@ func storeHandler(w http.ResponseWriter, r *http.Request) {
}
func GetHandler(w http.ResponseWriter, r *http.Request) {
n := new(storage.Needle)
path := r.URL.Path
sepIndex := strings.LastIndex(path, "/")
commaIndex := strings.LastIndex(path[sepIndex:], ",")
dotIndex := strings.LastIndex(path[sepIndex:], ".")
fid := path[commaIndex+1:]
if dotIndex > 0 {
fid = path[commaIndex+1 : dotIndex]
}
if commaIndex <= 0 {
log.Println("unknown file id", path[sepIndex+1:commaIndex])
return
}
volumeId, _ := strconv.Atoui64(path[sepIndex+1 : commaIndex])
vid, fid, ext := parseURLPath(r.URL.Path)
volumeId, _ := strconv.Atoui64(vid)
n.ParsePath(fid)
if *IsDebug {
@@ -65,17 +54,14 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
log.Println("request with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
return
}
if dotIndex > 0 {
ext := path[dotIndex:]
if ext!="" {
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
}
w.Write(n.Data)
}
func PostHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
commaIndex := strings.LastIndex(path, ",")
sepIndex := strings.LastIndex(path[:commaIndex], "/")
volumeId, e := strconv.Atoui64(path[sepIndex+1 : commaIndex])
vid, _, _ := parseURLPath(r.URL.Path)
volumeId, e := strconv.Atoui64(vid)
if e != nil {
writeJson(w, r, e)
} else {
@@ -87,19 +73,8 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
}
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
n := new(storage.Needle)
path := r.URL.Path
sepIndex := strings.LastIndex(path, "/")
commaIndex := strings.LastIndex(path[sepIndex:], ",")
dotIndex := strings.LastIndex(path[sepIndex:], ".")
fid := path[commaIndex+1:]
if dotIndex > 0 {
fid = path[commaIndex+1 : dotIndex]
}
if commaIndex <= 0 {
log.Println("unknown file id", path[sepIndex+1:commaIndex])
return
}
volumeId, _ := strconv.Atoui64(path[sepIndex+1 : commaIndex])
vid, fid, _ := parseURLPath(r.URL.Path)
volumeId, _ := strconv.Atoui64(vid)
n.ParsePath(fid)
cookie := n.Cookie
@@ -130,6 +105,23 @@ func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
}
//log.Println("JSON Response", string(bytes))
}
func parseURLPath(path string)(vid, fid, ext string){
sepIndex := strings.LastIndex(path, "/")
commaIndex := strings.LastIndex(path[sepIndex:], ",")
dotIndex := strings.LastIndex(path[sepIndex:], ".")
vid = path[sepIndex+1 : commaIndex]
fid = path[commaIndex+1:]
ext = ""
if dotIndex > 0 {
fid = path[commaIndex+1 : dotIndex]
ext = path[dotIndex+1:]
}
if commaIndex <= 0 {
log.Println("unknown file id", path[sepIndex+1:commaIndex])
return
}
return
}
func main() {
flag.Parse()