summaryrefslogtreecommitdiff
path: root/src/ninjacloud.go
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2012-09-21 21:06:24 +0200
committerPacien2015-12-07 22:48:24 +0100
commit20655b872242d41baeec8301e978fd52c90cc500 (patch)
treeb2fada0f88dfc3afd157ee38778ce819b76cfb51 /src/ninjacloud.go
parentf7d690f312e64372166f97472214219283999cea (diff)
downloadninja-go-local-cloud-20655b872242d41baeec8301e978fd52c90cc500.tar.gz
Use relative paths using a virtual Windows drive instead of absolute
paths.
Diffstat (limited to 'src/ninjacloud.go')
-rw-r--r--src/ninjacloud.go43
1 files changed, 20 insertions, 23 deletions
diff --git a/src/ninjacloud.go b/src/ninjacloud.go
index 326834c..9f78cfb 100644
--- a/src/ninjacloud.go
+++ b/src/ninjacloud.go
@@ -28,7 +28,6 @@ import (
28 "net/http" 28 "net/http"
29 "os" 29 "os"
30 "path/filepath" 30 "path/filepath"
31 "runtime"
32 "strconv" 31 "strconv"
33 "strings" 32 "strings"
34) 33)
@@ -41,11 +40,15 @@ var interfaceFlag string
41var portFlag string 40var portFlag string
42var rootFlag string 41var rootFlag string
43 42
43const driveName = "Z"
44const drivePrefix = driveName + ":/"
45
44const filePath = "/file/" 46const filePath = "/file/"
45const dirPath = "/directory/" 47const dirPath = "/directory/"
46const webPath = "/web?url=" 48const webPath = "/web?url="
47const statusPath = "/cloudstatus/" 49const statusPath = "/cloudstatus/"
48 50
51const drivePrefixLen = len(drivePrefix) - 1
49const filePathLen = len(filePath) 52const filePathLen = len(filePath)
50const dirPathLen = len(dirPath) 53const dirPathLen = len(dirPath)
51const webPathLen = len(webPath) 54const webPathLen = len(webPath)
@@ -233,7 +236,7 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
233 list = append(list, element{}) 236 list = append(list, element{})
234 e.Type = "directory" 237 e.Type = "directory"
235 e.Name = d.Name() 238 e.Name = d.Name()
236 e.Uri = uri 239 e.Uri = filepath.Clean(drivePrefix + uri)
237 e.CreationDate = modTime // TODO 240 e.CreationDate = modTime // TODO
238 e.ModifiedDate = modTime 241 e.ModifiedDate = modTime
239 e.Size = strconv.FormatInt(d.Size(), 10) 242 e.Size = strconv.FormatInt(d.Size(), 10)
@@ -256,13 +259,13 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
256 if ext != "" { 259 if ext != "" {
257 ext = ext[1:] 260 ext = ext[1:]
258 } 261 }
259 if sliceContains(filter, ext) { 262 if cap(filter) == 1 || sliceContains(filter, ext) {
260 var e element 263 var e element
261 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10) 264 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10)
262 modTime = modTime[:len(modTime)-6] 265 modTime = modTime[:len(modTime)-6]
263 e.Type = "file" 266 e.Type = "file"
264 e.Name = d.Name() 267 e.Name = d.Name()
265 e.Uri = filepath.Clean(path + "/" + d.Name()) 268 e.Uri = filepath.Clean(drivePrefix + path + "/" + d.Name())
266 e.CreationDate = modTime // TODO 269 e.CreationDate = modTime // TODO
267 e.ModifiedDate = modTime 270 e.ModifiedDate = modTime
268 e.Size = strconv.FormatInt(d.Size(), 10) 271 e.Size = strconv.FormatInt(d.Size(), 10)
@@ -277,22 +280,13 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
277 280
278//////// REQUEST HANDLERS 281//////// REQUEST HANDLERS
279 282
280func osPath(p string) string {
281 filepath.Clean(p)
282 if runtime.GOOS == "windows" {
283 p = p[:1] + ":" + p[1:]
284 } else {
285 p = "/" + p
286 }
287 return p
288}
289
290//// File APIs 283//// File APIs
291 284
292func fileHandler(w http.ResponseWriter, r *http.Request) { 285func fileHandler(w http.ResponseWriter, r *http.Request) {
293 w.Header().Add("Cache-Control", "no-cache") 286 w.Header().Add("Cache-Control", "no-cache")
294 p := osPath(r.URL.Path[filePathLen:]) 287 p := filepath.Clean(r.URL.Path[filePathLen:])
295 if !isInRoot(p) { 288 p = strings.TrimLeft(p, driveName+"/")
289 if filepath.IsAbs(p) {
296 w.WriteHeader(http.StatusForbidden) 290 w.WriteHeader(http.StatusForbidden)
297 return 291 return
298 } 292 }
@@ -456,8 +450,9 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
456 450
457func dirHandler(w http.ResponseWriter, r *http.Request) { 451func dirHandler(w http.ResponseWriter, r *http.Request) {
458 w.Header().Add("Cache-Control", "no-cache") 452 w.Header().Add("Cache-Control", "no-cache")
459 p := osPath(r.URL.Path[dirPathLen:]) 453 p := filepath.Clean(r.URL.Path[dirPathLen:])
460 if !isInRoot(p) { 454 p = strings.TrimLeft(p, driveName+"/")
455 if filepath.IsAbs(p) {
461 w.WriteHeader(http.StatusForbidden) 456 w.WriteHeader(http.StatusForbidden)
462 return 457 return
463 } 458 }
@@ -534,6 +529,7 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
534 w.WriteHeader(http.StatusInternalServerError) 529 w.WriteHeader(http.StatusInternalServerError)
535 return 530 return
536 } 531 }
532 var j []byte
537 var e element 533 var e element
538 rootDir, err := properties(p) 534 rootDir, err := properties(p)
539 if err != nil { 535 if err != nil {
@@ -544,14 +540,14 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
544 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10) 540 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10)
545 modTime = modTime[:len(modTime)-6] 541 modTime = modTime[:len(modTime)-6]
546 e.Type = "directory" 542 e.Type = "directory"
547 e.Name = "root" 543 e.Name = rootDir.Name()
548 e.Uri = p 544 e.Uri = filepath.Clean(drivePrefix + p)
549 e.CreationDate = modTime // TODO 545 e.CreationDate = modTime // TODO
550 e.ModifiedDate = modTime 546 e.ModifiedDate = modTime
551 e.Size = strconv.FormatInt(rootDir.Size(), 10) 547 e.Size = strconv.FormatInt(rootDir.Size(), 10)
552 e.Writable = "true" // TODO 548 e.Writable = "true" // TODO
553 e.Children = fileInfo 549 e.Children = fileInfo
554 j, err := json.MarshalIndent(e, "", " ") 550 j, err = json.MarshalIndent(e, "", " ")
555 if err != nil { 551 if err != nil {
556 log.Println(err) 552 log.Println(err)
557 w.WriteHeader(http.StatusInternalServerError) 553 w.WriteHeader(http.StatusInternalServerError)
@@ -613,7 +609,7 @@ func getStatusHandler(w http.ResponseWriter, r *http.Request) {
613 cloudStatus := map[string]string{ 609 cloudStatus := map[string]string{
614 "name": APP_NAME, 610 "name": APP_NAME,
615 "version": APP_VERSION, 611 "version": APP_VERSION,
616 "server-root": rootFlag, 612 "server-root": "",
617 "status": "running", 613 "status": "running",
618 } 614 }
619 j, err := json.MarshalIndent(cloudStatus, "", " ") 615 j, err := json.MarshalIndent(cloudStatus, "", " ")
@@ -648,7 +644,8 @@ func main() {
648 http.HandleFunc(webPath, getDataHandler) 644 http.HandleFunc(webPath, getDataHandler)
649 http.HandleFunc(statusPath, getStatusHandler) 645 http.HandleFunc(statusPath, getStatusHandler)
650 646
651 err := http.ListenAndServe(interfaceFlag+":"+portFlag, nil) 647 err := os.Chdir(rootFlag)
648 err = http.ListenAndServe(interfaceFlag+":"+portFlag, nil)
652 if err != nil { 649 if err != nil {
653 log.Println(err) 650 log.Println(err)
654 return 651 return