summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2012-09-14 23:24:42 +0100
committerPacien2015-12-07 22:48:23 +0100
commit7d4128aad782150db9ae9d25d0572579ed81c50a (patch)
treeaf4d624d81dfb3264ecfbfe2295138883bcbfb2b /src
parentb77e312f5c9c192750c70ca95f9ad0aa3bdc668d (diff)
downloadninja-go-local-cloud-7d4128aad782150db9ae9d25d0572579ed81c50a.tar.gz
Implement mod time comparison, file info reading and file serving.
Diffstat (limited to 'src')
-rw-r--r--src/ninjacloud.go65
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
15const APP_NAME = "Ninja Go Local Cloud" 16const APP_NAME = "Ninja Go Local Cloud"
@@ -33,6 +34,25 @@ const webPathLen = len(webPath)
33 34
34//////// FILESYSTEM 35//////// FILESYSTEM
35 36
37func properties(path string) (infos os.FileInfo, err error) {
38 infos, err = os.Stat(path)
39 return
40}
41
42func 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
36func exist(path string) bool { 56func 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
66func 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
71func removeFile(path string) (err error) { 91func 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}