summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ninjacloud.go97
1 files changed, 54 insertions, 43 deletions
diff --git a/src/ninjacloud.go b/src/ninjacloud.go
index 5c6b417..f15d7f1 100644
--- a/src/ninjacloud.go
+++ b/src/ninjacloud.go
@@ -237,7 +237,7 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
237 uri := filepath.Clean(path + "/" + d.Name()) 237 uri := filepath.Clean(path + "/" + d.Name())
238 e.Type = "directory" 238 e.Type = "directory"
239 e.Name = d.Name() 239 e.Name = d.Name()
240 e.Uri = filepath.Clean(drivePrefix + uri) 240 e.Uri = filepath.Clean(drivePrefix + projectsDir + "/" + uri)
241 e.CreationDate = modTime // TODO 241 e.CreationDate = modTime // TODO
242 e.ModifiedDate = modTime 242 e.ModifiedDate = modTime
243 e.Size = strconv.FormatInt(d.Size(), 10) 243 e.Size = strconv.FormatInt(d.Size(), 10)
@@ -262,7 +262,7 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
262 modTime = modTime[:len(modTime)-6] 262 modTime = modTime[:len(modTime)-6]
263 e.Type = "file" 263 e.Type = "file"
264 e.Name = d.Name() 264 e.Name = d.Name()
265 e.Uri = filepath.Clean(drivePrefix + path + "/" + d.Name()) 265 e.Uri = filepath.Clean(drivePrefix + projectsDir + "/" + path + "/" + d.Name())
266 e.CreationDate = modTime // TODO 266 e.CreationDate = modTime // TODO
267 e.ModifiedDate = modTime 267 e.ModifiedDate = modTime
268 e.Size = strconv.FormatInt(d.Size(), 10) 268 e.Size = strconv.FormatInt(d.Size(), 10)
@@ -286,9 +286,8 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
286 w.Header().Add("Access-Control-Allow-Origin", "*/*") 286 w.Header().Add("Access-Control-Allow-Origin", "*/*")
287 w.Header().Add("Access-Control-Max-Age", "86400") 287 w.Header().Add("Access-Control-Max-Age", "86400")
288 p := filepath.Clean(r.URL.Path[filePathLen:]) 288 p := filepath.Clean(r.URL.Path[filePathLen:])
289 p = strings.TrimLeft(p, driveName) 289 p = filepath.ToSlash(p)
290 p = strings.TrimLeft(p, "/") 290 p = strings.TrimLeft(p, driveName+"/"+projectsDir+"/")
291 p = strings.TrimLeft(p, "\\")
292 if filepath.IsAbs(p) { 291 if filepath.IsAbs(p) {
293 w.WriteHeader(http.StatusForbidden) 292 w.WriteHeader(http.StatusForbidden)
294 return 293 return
@@ -475,9 +474,8 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
475 w.Header().Add("Access-Control-Allow-Origin", "*/*") 474 w.Header().Add("Access-Control-Allow-Origin", "*/*")
476 w.Header().Add("Access-Control-Max-Age", "86400") 475 w.Header().Add("Access-Control-Max-Age", "86400")
477 p := filepath.Clean(r.URL.Path[dirPathLen:]) 476 p := filepath.Clean(r.URL.Path[dirPathLen:])
478 p = strings.TrimLeft(p, driveName) 477 p = filepath.ToSlash(p)
479 p = strings.TrimLeft(p, "/") 478 p = strings.TrimLeft(p, driveName+"/"+projectsDir+"/")
480 p = strings.TrimLeft(p, "\\")
481 if filepath.IsAbs(p) { 479 if filepath.IsAbs(p) {
482 w.WriteHeader(http.StatusForbidden) 480 w.WriteHeader(http.StatusForbidden)
483 return 481 return
@@ -515,10 +513,7 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
515 case "GET": 513 case "GET":
516 // List the contents of an existing directory 514 // List the contents of an existing directory
517 modSince := r.Header.Get("If-modified-since") 515 modSince := r.Header.Get("If-modified-since")
518 if p == "" { 516 if modSince != "" && modSince != "false" && modSince != "none" {
519 w.Write([]byte(rootFlag))
520 return
521 } else if modSince != "" && modSince != "false" && modSince != "none" {
522 if !exist(p) { 517 if !exist(p) {
523 w.WriteHeader(http.StatusNotFound) 518 w.WriteHeader(http.StatusNotFound)
524 return 519 return
@@ -545,35 +540,49 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
545 if returnType == "" { 540 if returnType == "" {
546 returnType = "all" 541 returnType = "all"
547 } 542 }
548 fileInfo, err := listDir(p, recursive, filter, returnType)
549 if err == os.ErrNotExist {
550 log.Println(err)
551 w.WriteHeader(http.StatusNotFound)
552 return
553 } else if err != nil {
554 log.Println(err)
555 w.WriteHeader(http.StatusInternalServerError)
556 return
557 }
558 var j []byte
559 var e element 543 var e element
560 rootDir, err := properties(p) 544 if p == "." {
561 if err != nil { 545 var c []element
562 log.Println(err) 546 var n element
563 w.WriteHeader(http.StatusInternalServerError) 547 n.Type = "directory"
564 return 548 n.Name = projectsDir
549 n.Uri = drivePrefix + projectsDir
550 c = append(c, n)
551 e.Type = "directory"
552 e.Children = c
553 } else {
554 if p == "" {
555 p = "."
556 }
557 fileInfo, err := listDir(p, recursive, filter, returnType)
558 if err == os.ErrNotExist {
559 log.Println(err)
560 w.WriteHeader(http.StatusNotFound)
561 return
562 } else if err != nil {
563 log.Println(err)
564 w.WriteHeader(http.StatusInternalServerError)
565 return
566 }
567 rootDir, err := properties(p)
568 if err != nil {
569 log.Println(err)
570 w.WriteHeader(http.StatusInternalServerError)
571 return
572 }
573 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10)
574 modTime = modTime[:len(modTime)-6]
575 e.Type = "directory"
576 e.Name = rootDir.Name()
577 e.Uri = drivePrefix + p
578 e.CreationDate = modTime // TODO
579 e.ModifiedDate = modTime
580 e.Size = strconv.FormatInt(rootDir.Size(), 10)
581 e.Writable = "true" // TODO
582 e.Children = fileInfo
565 } 583 }
566 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10) 584
567 modTime = modTime[:len(modTime)-6] 585 j, err := json.MarshalIndent(e, "", " ")
568 e.Type = "directory"
569 e.Name = rootDir.Name()
570 e.Uri = filepath.Clean(drivePrefix + p)
571 e.CreationDate = modTime // TODO
572 e.ModifiedDate = modTime
573 e.Size = strconv.FormatInt(rootDir.Size(), 10)
574 e.Writable = "true" // TODO
575 e.Children = fileInfo
576 j, err = json.MarshalIndent(e, "", " ")
577 if err != nil { 586 if err != nil {
578 log.Println(err) 587 log.Println(err)
579 w.WriteHeader(http.StatusInternalServerError) 588 w.WriteHeader(http.StatusInternalServerError)
@@ -661,14 +670,16 @@ func main() {
661 return 670 return
662 } 671 }
663 672
664 err := os.Chdir(rootFlag) 673 root := filepath.Clean((rootFlag + "/" + projectsDir))
665 currentDir, err := os.Getwd() 674
675 err := createDir(root)
666 if err != nil { 676 if err != nil {
667 log.Println(err) 677 log.Println(err)
668 return 678 return
669 } 679 }
670 680
671 err = createDir(projectsDir) 681 err = os.Chdir(root)
682 currentDir, err := os.Getwd()
672 if err != nil { 683 if err != nil {
673 log.Println(err) 684 log.Println(err)
674 return 685 return
@@ -681,7 +692,7 @@ func main() {
681 http.HandleFunc(dirPath, dirHandler) 692 http.HandleFunc(dirPath, dirHandler)
682 http.HandleFunc(webPath, getDataHandler) 693 http.HandleFunc(webPath, getDataHandler)
683 http.HandleFunc(statusPath, getStatusHandler) 694 http.HandleFunc(statusPath, getStatusHandler)
684 http.Handle("/", http.FileServer(http.Dir(projectsDir))) 695 http.Handle("/", http.FileServer(http.Dir(root)))
685 696
686 err = http.ListenAndServe(interfaceFlag+":"+portFlag, nil) 697 err = http.ListenAndServe(interfaceFlag+":"+portFlag, nil)
687 if err != nil { 698 if err != nil {