loadLatestPhoto.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. This script load the latest image from the save directory
  3. Require paramter: savetarget
  4. */
  5. requirelib('filelib');
  6. function getLatestPhotoFilename(){
  7. if (savetarget == ""){
  8. sendJSONResp(JSON.stringify({
  9. error: "savetarget not defined"
  10. }));
  11. return
  12. }
  13. if (!filelib.fileExists(savetarget)){
  14. sendJSONResp(JSON.stringify({
  15. error: "savetarget not exists"
  16. }));
  17. return
  18. }
  19. if (savetarget.substring(savetarget.length - 1,1) != "/"){
  20. savetarget = savetarget + "/";
  21. }
  22. //Save target exists. Glob it
  23. var jpgFiles = filelib.aglob(savetarget + "*.jpg");
  24. var pngFiles = filelib.aglob(savetarget + "*.png");
  25. var files = [];
  26. for (var i = 0; i < jpgFiles.length; i++){
  27. files.push(jpgFiles[i]);
  28. }
  29. for (var i = 0; i < pngFiles.length; i++){
  30. files.push(pngFiles[i]);
  31. }
  32. var latestFileMtime = 0;
  33. var latestFilename = "";
  34. for (var i = 0; i < files.length; i++){
  35. var thisFile = files[i];
  36. var thisModTime = filelib.mtime(thisFile, true);
  37. if (thisModTime > latestFileMtime){
  38. latestFileMtime = thisModTime;
  39. latestFilename = thisFile;
  40. }
  41. }
  42. sendJSONResp(JSON.stringify(latestFilename));
  43. }
  44. getLatestPhotoFilename();