listNearbyImage.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var loadedfile = requirelib("filelib");
  2. if (!loadedfile) {
  3. console.log("Failed to load lib filelib, terminated.");
  4. }
  5. function listNearby(){
  6. var result = [];
  7. //Extract the path from the filepath
  8. var dirpath = path.split("\\").join("/");
  9. dirpath = dirpath.split("/");
  10. dirpath.pop();
  11. dirpath = dirpath.join("/");
  12. //Get nearby files and filter out the one that is web supported photo format
  13. var nearbyFiles = filelib.readdir(dirpath, "user")
  14. for (var i = 0; i < nearbyFiles.length; i++){
  15. var thisFile = nearbyFiles[i];
  16. //console.log(JSON.stringify(nearbyFiles[i]));
  17. var ext = thisFile.Ext.substr(1);
  18. ext = ext.toLowerCase();
  19. if (ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "webp"){
  20. result.push(thisFile.Filepath);
  21. }
  22. }
  23. sendJSONResp(JSON.stringify(result))
  24. }
  25. if (typeof(path) == "undefined"){
  26. sendJSONResp(JSON.stringify({
  27. "error": "Invalid path given"
  28. }));
  29. }else{
  30. listNearby();
  31. }