summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2012-09-16 11:37:33 +0100
committerPacien2015-12-07 22:48:24 +0100
commitcd0597f9cb7f4652b7ef586c74a04422c0c15d94 (patch)
tree5397d19a26810610f146540de543fa39a9383eba /src
parentdf573701d2430ceb4154b31b4d900b150154a3ee (diff)
downloadninja-go-local-cloud-cd0597f9cb7f4652b7ef586c74a04422c0c15d94.tar.gz
Multiple bug fixes:
- files and dirs creating permissions (600 -> 777) - remove empty element when listing dirs content - add informations for root dir - add error logging - check if file or dir exists before deleting - add indentation to JSON output
Diffstat (limited to 'src')
-rw-r--r--src/ninjacloud.go82
1 files changed, 71 insertions, 11 deletions
diff --git a/src/ninjacloud.go b/src/ninjacloud.go
index 5016234..4a10b2d 100644
--- a/src/ninjacloud.go
+++ b/src/ninjacloud.go
@@ -108,7 +108,7 @@ func writeFile(path string, content []byte, overwrite bool) (err error) {
108 return 108 return
109 } 109 }
110 } 110 }
111 err = ioutil.WriteFile(path, content, 0600) 111 err = ioutil.WriteFile(path, content, 0777)
112 return 112 return
113} 113}
114 114
@@ -153,7 +153,7 @@ func copyFile(source string, dest string) (err error) {
153//// Dirs 153//// Dirs
154 154
155func createDir(path string) (err error) { 155func createDir(path string) (err error) {
156 err = os.MkdirAll(path, 0600) 156 err = os.MkdirAll(path, 0777)
157 return 157 return
158} 158}
159 159
@@ -246,9 +246,17 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
246 } else { 246 } else {
247 e.Children = nil 247 e.Children = nil
248 } 248 }
249 list = append(list, e) 249 if cap(list) > 1 {
250 list = append(list, e)
251 } else {
252 list[0] = e
253 }
250 } else if !d.IsDir() && returnFiles { 254 } else if !d.IsDir() && returnFiles {
251 if sliceContains(filter, filepath.Ext(d.Name())) { 255 ext := filepath.Ext(d.Name())
256 if ext != "" {
257 ext = ext[1:]
258 }
259 if sliceContains(filter, ext) {
252 var e element 260 var e element
253 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10) 261 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10)
254 modTime = modTime[:len(modTime)-6] 262 modTime = modTime[:len(modTime)-6]
@@ -293,13 +301,17 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
293 // Create a new file 301 // Create a new file
294 content, err := ioutil.ReadAll(r.Body) 302 content, err := ioutil.ReadAll(r.Body)
295 if err != nil { 303 if err != nil {
304 log.Println(err)
296 w.WriteHeader(http.StatusInternalServerError) 305 w.WriteHeader(http.StatusInternalServerError)
297 return 306 return
298 } 307 }
299 err = writeFile(p, *&content, false) 308 err = writeFile(p, *&content, false)
300 if err == os.ErrExist { 309 if err == os.ErrExist {
310 log.Println(err)
301 w.WriteHeader(http.StatusBadRequest) 311 w.WriteHeader(http.StatusBadRequest)
312 return
302 } else if err != nil { 313 } else if err != nil {
314 log.Println(err)
303 w.WriteHeader(http.StatusInternalServerError) 315 w.WriteHeader(http.StatusInternalServerError)
304 return 316 return
305 } 317 }
@@ -311,13 +323,17 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
311 // Update an existing file (save over existing file) 323 // Update an existing file (save over existing file)
312 content, err := ioutil.ReadAll(r.Body) 324 content, err := ioutil.ReadAll(r.Body)
313 if err != nil { 325 if err != nil {
326 log.Println(err)
314 w.WriteHeader(http.StatusInternalServerError) 327 w.WriteHeader(http.StatusInternalServerError)
315 return 328 return
316 } 329 }
317 err = writeFile(p, *&content, true) 330 err = writeFile(p, *&content, true)
318 if err == os.ErrNotExist { 331 if err == os.ErrNotExist {
332 log.Println(err)
319 w.WriteHeader(http.StatusNotFound) 333 w.WriteHeader(http.StatusNotFound)
334 return
320 } else if err != nil { 335 } else if err != nil {
336 log.Println(err)
321 w.WriteHeader(http.StatusInternalServerError) 337 w.WriteHeader(http.StatusInternalServerError)
322 return 338 return
323 } 339 }
@@ -325,31 +341,41 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
325 return 341 return
326 } else { 342 } else {
327 // Copy, Move of an existing file 343 // Copy, Move of an existing file
328 if r.Header.Get("overwrite-destination") == "true" { 344 if r.Header.Get("overwrite-destination") != "true" {
329 err := removeFile(p) 345 if exist(p) {
346 w.WriteHeader(http.StatusInternalServerError)
347 return
348 }
349 /*err := removeFile(p)
330 if err == os.ErrNotExist { 350 if err == os.ErrNotExist {
351 log.Println(err)
331 w.WriteHeader(http.StatusNotFound) 352 w.WriteHeader(http.StatusNotFound)
332 return 353 return
333 } else if err != nil { 354 } else if err != nil {
355 log.Println(err)
334 w.WriteHeader(http.StatusInternalServerError) 356 w.WriteHeader(http.StatusInternalServerError)
335 return 357 return
336 } 358 }*/
337 } 359 }
338 if r.Header.Get("delete-source") == "true" { 360 if r.Header.Get("delete-source") == "true" {
339 err := moveFile(source, p) 361 err := moveFile(source, p)
340 if err == os.ErrNotExist { 362 if err == os.ErrNotExist {
363 log.Println(err)
341 w.WriteHeader(http.StatusNotFound) 364 w.WriteHeader(http.StatusNotFound)
342 return 365 return
343 } else if err != nil { 366 } else if err != nil {
367 log.Println(err)
344 w.WriteHeader(http.StatusInternalServerError) 368 w.WriteHeader(http.StatusInternalServerError)
345 return 369 return
346 } 370 }
347 } else { 371 } else {
348 err := copyFile(source, p) 372 err := copyFile(source, p)
349 if err == os.ErrNotExist { 373 if err == os.ErrNotExist {
374 log.Println(err)
350 w.WriteHeader(http.StatusNotFound) 375 w.WriteHeader(http.StatusNotFound)
351 return 376 return
352 } else if err != nil { 377 } else if err != nil {
378 log.Println(err)
353 w.WriteHeader(http.StatusInternalServerError) 379 w.WriteHeader(http.StatusInternalServerError)
354 return 380 return
355 } 381 }
@@ -359,11 +385,17 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
359 } 385 }
360 case "DELETE": 386 case "DELETE":
361 // Delete an existing file 387 // Delete an existing file
388 if !exist(p) {
389 w.WriteHeader(http.StatusNotFound)
390 return
391 }
362 err := removeFile(p) 392 err := removeFile(p)
363 if err == os.ErrNotExist { 393 if err == os.ErrNotExist {
394 log.Println(err)
364 w.WriteHeader(http.StatusNotFound) 395 w.WriteHeader(http.StatusNotFound)
365 return 396 return
366 } else if err != nil { 397 } else if err != nil {
398 log.Println(err)
367 w.WriteHeader(http.StatusInternalServerError) 399 w.WriteHeader(http.StatusInternalServerError)
368 return 400 return
369 } 401 }
@@ -392,6 +424,7 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
392 } else if getInfo != "" && getInfo != "false" { 424 } else if getInfo != "" && getInfo != "false" {
393 infos, err := properties(p) 425 infos, err := properties(p)
394 if err != nil { 426 if err != nil {
427 log.Println(err)
395 w.WriteHeader(http.StatusInternalServerError) 428 w.WriteHeader(http.StatusInternalServerError)
396 return 429 return
397 } 430 }
@@ -404,8 +437,9 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
404 "size": size, 437 "size": size,
405 "readOnly": "false", // TODO 438 "readOnly": "false", // TODO
406 } 439 }
407 j, err := json.Marshal(fileInfo) 440 j, err := json.MarshalIndent(fileInfo, "", " ")
408 if err != nil { 441 if err != nil {
442 log.Println(err)
409 w.WriteHeader(http.StatusInternalServerError) 443 w.WriteHeader(http.StatusInternalServerError)
410 return 444 return
411 } 445 }
@@ -431,6 +465,7 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
431 // Create a new directory 465 // Create a new directory
432 err := createDir(p) 466 err := createDir(p)
433 if err != nil { 467 if err != nil {
468 log.Println(err)
434 w.WriteHeader(http.StatusBadRequest) 469 w.WriteHeader(http.StatusBadRequest)
435 return 470 return
436 } 471 }
@@ -438,11 +473,17 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
438 return 473 return
439 case "DELETE": 474 case "DELETE":
440 // Delete an existing directory 475 // Delete an existing directory
476 if !exist(p) {
477 w.WriteHeader(http.StatusNotFound)
478 return
479 }
441 err := removeDir(p) 480 err := removeDir(p)
442 if err == os.ErrNotExist { 481 if err == os.ErrNotExist {
482 log.Println(err)
443 w.WriteHeader(http.StatusNotFound) 483 w.WriteHeader(http.StatusNotFound)
444 return 484 return
445 } else if err != nil { 485 } else if err != nil {
486 log.Println(err)
446 w.WriteHeader(http.StatusInternalServerError) 487 w.WriteHeader(http.StatusInternalServerError)
447 return 488 return
448 } 489 }
@@ -483,19 +524,34 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
483 } 524 }
484 fileInfo, err := listDir(p, recursive, filter, returnType) 525 fileInfo, err := listDir(p, recursive, filter, returnType)
485 if err == os.ErrNotExist { 526 if err == os.ErrNotExist {
527 log.Println(err)
486 w.WriteHeader(http.StatusNotFound) 528 w.WriteHeader(http.StatusNotFound)
487 return 529 return
488 } else if err != nil { 530 } else if err != nil {
531 log.Println(err)
489 w.WriteHeader(http.StatusInternalServerError) 532 w.WriteHeader(http.StatusInternalServerError)
490 return 533 return
491 } 534 }
492 var e element 535 var e element
536 rootDir, err := properties(p)
537 if err != nil {
538 log.Println(err)
539 w.WriteHeader(http.StatusInternalServerError)
540 return
541 }
542 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10)
543 modTime = modTime[:len(modTime)-6]
493 e.Type = "directory" 544 e.Type = "directory"
494 e.Name = "root" 545 e.Name = "root"
495 e.Uri = p + "/" 546 e.Uri = p
547 e.CreationDate = modTime // TODO
548 e.ModifiedDate = modTime
549 e.Size = strconv.FormatInt(rootDir.Size(), 10)
550 e.Writable = "true" // TODO
496 e.Children = fileInfo 551 e.Children = fileInfo
497 j, err := json.Marshal(e) 552 j, err := json.MarshalIndent(e, "", " ")
498 if err != nil { 553 if err != nil {
554 log.Println(err)
499 w.WriteHeader(http.StatusInternalServerError) 555 w.WriteHeader(http.StatusInternalServerError)
500 return 556 return
501 } 557 }
@@ -513,18 +569,22 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
513 if operation == "move" { 569 if operation == "move" {
514 err := moveDir(source, p) 570 err := moveDir(source, p)
515 if err == os.ErrNotExist { 571 if err == os.ErrNotExist {
572 log.Println(err)
516 w.WriteHeader(http.StatusNotFound) 573 w.WriteHeader(http.StatusNotFound)
517 return 574 return
518 } else if err != nil { 575 } else if err != nil {
576 log.Println(err)
519 w.WriteHeader(http.StatusInternalServerError) 577 w.WriteHeader(http.StatusInternalServerError)
520 return 578 return
521 } 579 }
522 } else if operation == "copy" { 580 } else if operation == "copy" {
523 err := copyDir(source, p) 581 err := copyDir(source, p)
524 if err == os.ErrNotExist { 582 if err == os.ErrNotExist {
583 log.Println(err)
525 w.WriteHeader(http.StatusNotFound) 584 w.WriteHeader(http.StatusNotFound)
526 return 585 return
527 } else if err != nil { 586 } else if err != nil {
587 log.Println(err)
528 w.WriteHeader(http.StatusInternalServerError) 588 w.WriteHeader(http.StatusInternalServerError)
529 return 589 return
530 } 590 }
@@ -553,7 +613,7 @@ func getStatusHandler(w http.ResponseWriter, r *http.Request) {
553 "server-root": rootFlag, 613 "server-root": rootFlag,
554 "status": "running", 614 "status": "running",
555 } 615 }
556 j, err := json.Marshal(cloudStatus) 616 j, err := json.MarshalIndent(cloudStatus, "", " ")
557 if err != nil { 617 if err != nil {
558 log.Println(err) 618 log.Println(err)
559 } 619 }