diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ninjacloud.go | 82 |
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 | ||
155 | func createDir(path string) (err error) { | 155 | func 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) |