listFolder.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. requirelib("filelib")
  2. function getExt(filename){
  3. return filename.split(".").pop();
  4. }
  5. function isImage(filename){
  6. var ext = getExt(filename);
  7. ext = ext.toLowerCase();
  8. if (ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "webp"){
  9. return true;
  10. }
  11. return false;
  12. }
  13. function isHiddenFile(filepath){
  14. var filename = filepath.split("/").pop();
  15. if (filename.substring(0, 1) == "."){
  16. return true;
  17. }else{
  18. return false;
  19. }
  20. }
  21. function folderContainSubFiles(filepath){
  22. var results = filelib.aglob(filepath + "/*", "user");
  23. if (results.length > 0){
  24. return true;
  25. }
  26. return false;
  27. }
  28. function dirname(filepath){
  29. var tmp = filepath.split("/");
  30. tmp.pop();
  31. return tmp.join("/");
  32. }
  33. function main(){
  34. //Scan the folder
  35. var results = filelib.aglob(folder, "user");
  36. //Sort the files
  37. var files = [];
  38. var folders = [];
  39. for (var i = 0; i < results.length; i++){
  40. var thisFile = results[i];
  41. if (filelib.isDir(thisFile)){
  42. if (!isHiddenFile(thisFile) && folderContainSubFiles(thisFile)){
  43. folders.push(thisFile);
  44. }
  45. }else{
  46. if (isImage(thisFile)){
  47. files.push(thisFile);
  48. }
  49. }
  50. }
  51. sendJSONResp(JSON.stringify([folders, files]));
  52. }
  53. main();