diff options
-rw-r--r-- | src/ninjacloud.go | 65 |
1 files changed, 55 insertions, 10 deletions
diff --git a/src/ninjacloud.go b/src/ninjacloud.go index cb99446..2786202 100644 --- a/src/ninjacloud.go +++ b/src/ninjacloud.go | |||
@@ -10,6 +10,7 @@ import ( | |||
10 | "os" | 10 | "os" |
11 | "path/filepath" | 11 | "path/filepath" |
12 | "runtime" | 12 | "runtime" |
13 | "strconv" | ||
13 | ) | 14 | ) |
14 | 15 | ||
15 | const APP_NAME = "Ninja Go Local Cloud" | 16 | const APP_NAME = "Ninja Go Local Cloud" |
@@ -33,6 +34,25 @@ const webPathLen = len(webPath) | |||
33 | 34 | ||
34 | //////// FILESYSTEM | 35 | //////// FILESYSTEM |
35 | 36 | ||
37 | func properties(path string) (infos os.FileInfo, err error) { | ||
38 | infos, err = os.Stat(path) | ||
39 | return | ||
40 | } | ||
41 | |||
42 | func modifiedSince(path string, since string) bool { | ||
43 | s, err := strconv.ParseInt(since, 10, 64) | ||
44 | infos, err := properties(path) | ||
45 | if err != nil { | ||
46 | return false | ||
47 | } | ||
48 | l := infos.ModTime().UnixNano() | ||
49 | s = s * 1000000 | ||
50 | if s > l { | ||
51 | return true | ||
52 | } | ||
53 | return false | ||
54 | } | ||
55 | |||
36 | func exist(path string) bool { | 56 | func exist(path string) bool { |
37 | _, err := os.Stat(path) | 57 | _, err := os.Stat(path) |
38 | if !os.IsNotExist(err) { | 58 | if !os.IsNotExist(err) { |
@@ -63,10 +83,10 @@ func writeFile(path string, content []byte, overwrite bool) (err error) { | |||
63 | return | 83 | return |
64 | } | 84 | } |
65 | 85 | ||
66 | func readFile(path string) (content []byte, err error) { | 86 | /*func readFile(path string) (content []byte, err error) { |
67 | content, err = ioutil.ReadFile(path) | 87 | content, err = ioutil.ReadFile(path) |
68 | return | 88 | return |
69 | } | 89 | }*/ |
70 | 90 | ||
71 | func removeFile(path string) (err error) { | 91 | func removeFile(path string) (err error) { |
72 | err = os.Remove(path) | 92 | err = os.Remove(path) |
@@ -263,11 +283,16 @@ func fileHandler(w http.ResponseWriter, r *http.Request) { | |||
263 | return | 283 | return |
264 | case "GET": | 284 | case "GET": |
265 | // Read an existing file | 285 | // Read an existing file |
266 | modifiedSince := r.Header.Get("If-modified-since") | 286 | modSince := r.Header.Get("If-modified-since") |
267 | if modifiedSince != "" { | 287 | getInfo := r.Header.Get("get-file-info") |
268 | // TODO | 288 | if modSince != "" && modSince != "false" && modSince != "none" { |
269 | var test os.FileInfo | 289 | if modifiedSince(p, modSince) { |
270 | test.ModTime() | 290 | w.WriteHeader(http.StatusOK) |
291 | return | ||
292 | } else { | ||
293 | w.WriteHeader(http.StatusNotModified) | ||
294 | return | ||
295 | } | ||
271 | } else if r.Header.Get("check-existence-only") == "true" { | 296 | } else if r.Header.Get("check-existence-only") == "true" { |
272 | if exist(p) { | 297 | if exist(p) { |
273 | w.WriteHeader(http.StatusNoContent) | 298 | w.WriteHeader(http.StatusNoContent) |
@@ -276,10 +301,30 @@ func fileHandler(w http.ResponseWriter, r *http.Request) { | |||
276 | w.WriteHeader(http.StatusNotFound) | 301 | w.WriteHeader(http.StatusNotFound) |
277 | return | 302 | return |
278 | } | 303 | } |
279 | } else if r.Header.Get("get-file-info") != "" { | 304 | } else if getInfo != "" && getInfo != "false" { |
280 | // TODO | 305 | infos, err := properties(p) |
306 | if err != nil { | ||
307 | w.WriteHeader(http.StatusInternalServerError) | ||
308 | return | ||
309 | } | ||
310 | modDate := strconv.FormatInt(infos.ModTime().UnixNano(), 10) | ||
311 | modDate = modDate[:len(modDate)-6] | ||
312 | size := strconv.FormatInt(infos.Size(), 10) | ||
313 | fileInfo := map[string]string{ | ||
314 | "creationDate": modDate, // TODO | ||
315 | "modifiedDate": modDate, | ||
316 | "size": size, | ||
317 | "readOnly": "false", // TODO | ||
318 | } | ||
319 | j, err := json.Marshal(fileInfo) | ||
320 | if err != nil { | ||
321 | w.WriteHeader(http.StatusInternalServerError) | ||
322 | return | ||
323 | } | ||
324 | w.Write(j) | ||
325 | return | ||
281 | } else { | 326 | } else { |
282 | // TODO | 327 | http.ServeFile(w, r, p) |
283 | } | 328 | } |
284 | } | 329 | } |
285 | } | 330 | } |