getMeta.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Get Meta Data
  3. Get the target file meta
  4. supplied data: file=(path)
  5. */
  6. //Define helper functions
  7. function bytesToSize(bytes) {
  8. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  9. if (bytes == 0) return '0 Byte';
  10. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  11. return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];
  12. }
  13. if (requirelib("filelib") == true){
  14. //Get the filename from paramters
  15. var openingFilePath = decodeURIComponent(file);
  16. var dirname = openingFilePath.split("/")
  17. dirname.pop()
  18. dirname = dirname.join("/");
  19. //Scan nearby files
  20. var nearbyFiles = filelib.aglob(dirname + "/*", "user") //aglob must be used here to prevent errors for non-unicode filename
  21. var audioFiles = [];
  22. var supportedFormats = [".mp3",".flac",".wav",".ogg",".aac",".webm",".mp4"];
  23. //For each nearby files
  24. for (var i =0; i < nearbyFiles.length; i++){
  25. var thisFile = nearbyFiles[i];
  26. var ext = thisFile.split(".").pop();
  27. ext = "." + ext;
  28. //Check if the file extension is in the supported extension list
  29. for (var k = 0; k < supportedFormats.length; k++){
  30. if (filelib.isDir(nearbyFiles[i]) == false && supportedFormats[k] == ext){
  31. var fileExt = ext.substr(1);
  32. var fileName = thisFile.split("/").pop();
  33. var fileSize = filelib.filesize(thisFile);
  34. var humanReadableFileSize = bytesToSize(fileSize);
  35. var thisFileInfo = [];
  36. thisFileInfo.push(fileName);
  37. thisFileInfo.push(thisFile);
  38. thisFileInfo.push(fileExt);
  39. thisFileInfo.push(humanReadableFileSize);
  40. audioFiles.push(thisFileInfo);
  41. break;
  42. }
  43. }
  44. }
  45. sendJSONResp(JSON.stringify(audioFiles));
  46. }