@@ -5,50 +5,20 @@ import (
55 "github.com/GuoYuefei/DOStorage1/distributed/config"
66 "github.com/GuoYuefei/DOStorage1/distributed/data/locate"
77 "github.com/GuoYuefei/DOStorage1/distributed/utils"
8- "github.com/google/uuid"
9- "io"
108 "io/ioutil"
119 "net/http"
1210 "net/url"
1311 "os"
1412 "path"
15- "strconv"
16- "strings"
1713 "time"
1814)
1915
20- type tempInfo struct {
21- Uuid string
22- Name string // is hash
23- Size int64
24- }
25-
26- func (t * tempInfo ) hash () string {
27- s := strings .Split (t .Name , "." )
28- return s [0 ]
29- }
30-
31- func (t * tempInfo ) id () int {
32- s := strings .Split (t .Name , "." )
33- id , _ := strconv .Atoi (s [1 ])
34- return id
35- }
36-
37- func (t * tempInfo ) writeToFile () error {
38- f , e := os .Create (path .Join (config .TempRoot , t .Uuid ))
39- if e != nil {
40- return e
41- }
42- defer f .Close ()
43- b , _ := json .Marshal (t )
44- f .Write (b )
45- return nil
46- }
47-
4816func Handler (w http.ResponseWriter , r * http.Request ) {
4917 m := r .Method
5018 utils .Log .Printf (utils .Debug , "will call function temp.%s" , m )
5119 switch m {
20+ case http .MethodHead : head (w , r )
21+ case http .MethodGet : get (w , r )
5222 case http .MethodPut : put (w , r )
5323 case http .MethodPatch : patch (w , r )
5424 case http .MethodPost : post (w , r )
@@ -58,117 +28,6 @@ func Handler(w http.ResponseWriter, r *http.Request) {
5828 }
5929}
6030
61- func post (w http.ResponseWriter , r * http.Request ) {
62- uu := uuid .New ().String ()
63- name := strings .Split (r .URL .EscapedPath (), "/" )[2 ]
64- size , e := strconv .ParseInt (r .Header .Get ("size" ), 0 , 64 )
65- if e != nil {
66- utils .Log .Println (utils .Err , e )
67- w .WriteHeader (http .StatusInternalServerError )
68- return
69- }
70- t := tempInfo {
71- uu ,
72- name ,
73- size ,
74- }
75- e = t .writeToFile ()
76- if e != nil {
77- utils .Log .Println (utils .Err , e )
78- w .WriteHeader (http .StatusInternalServerError )
79- return
80- }
81- _ , e = os .Create (path .Join (config .TempRoot , t .Uuid + ".dat" ))
82- if e != nil {
83- utils .Log .Println (utils .Err , e )
84- w .WriteHeader (http .StatusInternalServerError )
85- return
86- }
87- w .Write ([]byte (uu ))
88- }
89-
90- func patch (w http.ResponseWriter , r * http.Request ) {
91- uu := strings .Split (r .URL .EscapedPath (), "/" )[2 ]
92- tempinfo , e := readFromFile (uu )
93- if e != nil {
94- utils .Log .Println (utils .Err , e )
95- w .WriteHeader (http .StatusNotFound )
96- return
97- }
98- infoFile := path .Join (config .TempRoot , uu )
99- datFile := infoFile + ".dat"
100- f , e := os .OpenFile (datFile , os .O_WRONLY | os .O_APPEND , 0 )
101- //f, e := os.Create(datFile)
102- if e != nil {
103- utils .Log .Println (utils .Err , e )
104- w .WriteHeader (http .StatusInternalServerError )
105- return
106- }
107- defer f .Close ()
108- _ , e = io .Copy (f , r .Body )
109- if e != nil {
110- utils .Log .Println (utils .Err , e )
111- w .WriteHeader (http .StatusInternalServerError )
112- return
113- }
114- info , e := f .Stat ()
115- if e != nil {
116- utils .Log .Println (utils .Err , e )
117- w .WriteHeader (http .StatusInternalServerError )
118- return
119- }
120- actual := info .Size ()
121- if actual > tempinfo .Size {
122- os .Remove (datFile )
123- os .Remove (infoFile )
124- utils .Log .Printf (utils .Debug , "actual size %d, exceeds %d" , actual , tempinfo .Size )
125- w .WriteHeader (http .StatusInternalServerError )
126- }
127- }
128-
129- func put (w http.ResponseWriter , r * http.Request ) {
130- uu := strings .Split (r .URL .EscapedPath (), "/" )[2 ]
131- tempinfo , e := readFromFile (uu )
132- if e != nil {
133- utils .Log .Println (utils .Err , e )
134- w .WriteHeader (http .StatusNotFound )
135- return
136- }
137- infoFile := path .Join (config .TempRoot , uu )
138- datFile := infoFile + ".dat"
139- f , e := os .OpenFile (datFile , os .O_WRONLY | os .O_APPEND , 0 )
140- if e != nil {
141- utils .Log .Println (utils .Err , e )
142- w .WriteHeader (http .StatusInternalServerError )
143- return
144- }
145- defer f .Close ()
146- info , e := f .Stat ()
147- if e != nil {
148- utils .Log .Println (utils .Err , e )
149- w .WriteHeader (http .StatusInternalServerError )
150- return
151- }
152- actual := info .Size ()
153- os .Remove (infoFile )
154- if actual != tempinfo .Size {
155- os .Remove (datFile )
156- utils .Log .Printf (utils .Info , "actual size %d, exceeds %d" , actual , tempinfo .Size )
157- w .WriteHeader (http .StatusInternalServerError )
158- return
159- }
160- commitTempObject (datFile , tempinfo )
161- w .WriteHeader (http .StatusAccepted )
162- }
163-
164- func delete (_ http.ResponseWriter , r * http.Request ) {
165- uu := strings .Split (r .URL .EscapedPath (), "/" )[2 ]
166- infoFile := path .Join (config .TempRoot , uu )
167- datFile := infoFile + ".dat"
168- os .Remove (infoFile )
169- os .Remove (datFile )
170- }
171-
17231func readFromFile (uu string ) (* tempInfo , error ) {
17332 f , e := os .Open (path .Join (config .TempRoot , uu ))
17433 if e != nil {
0 commit comments