getFileInfo.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Music Module
  3. Get File Information
  4. paramter: file
  5. */
  6. function bytesToSize(bytes) {
  7. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  8. if (bytes == 0) return '0 Byte';
  9. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  10. return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];
  11. }
  12. if (requirelib("filelib") == false){
  13. sendJSONResp(JSON.stringify({
  14. error: "Unable to load filelib"
  15. }));
  16. }else{
  17. if (filepath.indexOf("/media?file=") !== -1){
  18. filepath = filepath.replace("/media?file=", "");
  19. }
  20. var vpath = decodeURIComponent(filepath);
  21. //console.log(vpath)
  22. var results = [];
  23. var filename = vpath.split("/").pop();
  24. results.push(filename);
  25. results.push(vpath);
  26. var filesize = filelib.filesize(vpath);
  27. var humanReadableSize = bytesToSize(filesize);
  28. results.push(humanReadableSize);
  29. results.push(filesize);
  30. var modTime = filelib.mtime(vpath, false);
  31. results.push(modTime);
  32. sendJSONResp(JSON.stringify(results));
  33. }