getMeta.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /*
  21. var nearbyFiles = filelib.aglob(dirname + "/*", "user") //aglob must be used here to prevent errors for non-unicode filename
  22. var audioFiles = [];
  23. var supportedFormats = [".mp3",".flac",".wav",".ogg",".aac",".webm",".mp4"];
  24. //For each nearby files
  25. for (var i =0; i < nearbyFiles.length; i++){
  26. var thisFile = nearbyFiles[i];
  27. var ext = thisFile.split(".").pop();
  28. ext = "." + ext;
  29. //Check if the file extension is in the supported extension list
  30. for (var k = 0; k < supportedFormats.length; k++){
  31. if (nearbyFiles[i] != "" && filelib.isDir(nearbyFiles[i]) == false && supportedFormats[k] == ext){
  32. var fileExt = ext.substr(1);
  33. var fileName = thisFile.split("/").pop();
  34. var fileSize = filelib.filesize(thisFile);
  35. var humanReadableFileSize = bytesToSize(fileSize);
  36. var thisFileInfo = [];
  37. thisFileInfo.push(fileName);
  38. thisFileInfo.push(thisFile);
  39. thisFileInfo.push(fileExt);
  40. thisFileInfo.push(humanReadableFileSize);
  41. audioFiles.push(thisFileInfo);
  42. break;
  43. }
  44. }
  45. }
  46. */
  47. var nearbyFiles = filelib.readdir(dirname, "user");
  48. var audioFiles = [];
  49. var supportedFormats = [".mp3",".flac",".wav",".ogg",".aac",".webm",".mp4"];
  50. //For each nearby files
  51. for (var i =0; i < nearbyFiles.length; i++){
  52. var thisFile = nearbyFiles[i];
  53. var ext = thisFile.Ext;
  54. //Check if the file extension is in the supported extension list
  55. for (var k = 0; k < supportedFormats.length; k++){
  56. if (!thisFile.IsDir && supportedFormats[k] == ext){
  57. var fileExt = ext.substr(1);
  58. var fileName = thisFile.Filename;
  59. var fileSize = thisFile.Filesize;
  60. var humanReadableFileSize = bytesToSize(fileSize);
  61. var thisFileInfo = [];
  62. thisFileInfo.push(fileName);
  63. thisFileInfo.push(thisFile.Filepath);
  64. thisFileInfo.push(fileExt);
  65. thisFileInfo.push(humanReadableFileSize);
  66. audioFiles.push(thisFileInfo);
  67. break;
  68. }
  69. }
  70. }
  71. if (nearbyFiles == false || nearbyFiles.length == 0){
  72. //There are some error that unable to scan nearby files. Return this file info only.
  73. audioFiles = [];
  74. var thisFile = openingFilePath;
  75. var ext = thisFile.split(".").pop();
  76. var fileExt = ext.substr(1);
  77. var fileName = thisFile.split("/").pop();
  78. var fileSize = filelib.filesize(thisFile);
  79. var humanReadableFileSize = bytesToSize(fileSize);
  80. var thisFileInfo = [];
  81. thisFileInfo.push(fileName);
  82. thisFileInfo.push(thisFile);
  83. thisFileInfo.push(fileExt);
  84. thisFileInfo.push(humanReadableFileSize);
  85. audioFiles.push(thisFileInfo);
  86. }
  87. sendJSONResp(JSON.stringify(audioFiles));
  88. }