Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
Change-Id: I7f2b5831658ac869ef62488a790e2d687e56fb3b
Signed-off-by: Stuart <[email protected]>
  • Loading branch information
Stuart authored and Stuart committed Jul 11, 2017
1 parent 8940170 commit fbad8a3
Showing 1 changed file with 50 additions and 1 deletion.
1 change: 0 additions & 1 deletion chapter5/dataServer/temp/put.go

This file was deleted.

50 changes: 50 additions & 0 deletions chapter5/dataServer/temp/put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package temp

import (
"../locate"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)

func put(w http.ResponseWriter, r *http.Request) {
uuid := strings.Split(r.URL.EscapedPath(), "/")[2]
infoFile := os.Getenv("STORAGE_ROOT") + "/temp/" + uuid
b, e := ioutil.ReadFile(infoFile)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusNotFound)
return
}
datFile := infoFile + ".dat"
f, e := os.OpenFile(datFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer f.Close()
info, e := f.Stat()
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
i := strings.Split(string(b), ":")
size, _ := strconv.ParseInt(i[1], 0, 64)
actual := info.Size()
os.Remove(infoFile)
if actual != size {
os.Remove(datFile)
log.Println("actual size mismatch")
w.WriteHeader(http.StatusInternalServerError)
return
}
file := strings.Split(i[0], ".")
id, _ := strconv.Atoi(file[1])
locate.Add(file[0], id)
os.Rename(datFile, os.Getenv("STORAGE_ROOT")+"/objects/"+i[0])
}

0 comments on commit fbad8a3

Please sign in to comment.