delPhoto.js 986 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. Delete a given photo file by path
  3. Require paramter
  4. - savetarget
  5. - filename
  6. */
  7. requirelib("filelib");
  8. function deleteFile(){
  9. //Check if the save dir
  10. if (savetarget.substring(savetarget.length - 1,1) != "/"){
  11. savetarget = savetarget + "/";
  12. }
  13. //Check if the file exists
  14. var targetFilepath = savetarget + filename;
  15. if (!filelib.fileExists(targetFilepath)){
  16. sendJSONResp(JSON.stringify({
  17. error: "Target file not exists"
  18. }));
  19. return
  20. }
  21. //Check if it is supported image file extension
  22. var ext = targetFilepath.split(".").pop();
  23. if (ext != "png" && ext != "jpg"){
  24. //This is not an image file. Reject delete operation
  25. sendJSONResp(JSON.stringify({
  26. error: "Target file is not an image taken by Camera"
  27. }));
  28. return
  29. }
  30. //OK. Remove the file.
  31. filelib.deleteFile(targetFilepath);
  32. sendJSONResp(JSON.stringify("OK"));
  33. }
  34. deleteFile();