forked from stuarthu/go-implement-your-object-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |