123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- /*
- List Song
- This function will list all song with the given directories.
- Type of operations
- listSong=(type)
- listSong=search:(keyword)
- listdir=root
- listdir= (Target path)
- listfolder=(target folder)
- This module is a direct lanuage translation from the original module.Musi.go
- */
- //Global variables
- var MINCACHESIZE = 300; //Min song list length for start using cache method
- //Include the common library
- var succ = includes("common.js")
- if (succ == false){
- console.log("Failed to load common.js");
- }
- //Handle user request
- function handleUserRequest(){
- if (requirelib("filelib") == false){
- sendJSONResp(JSON.stringify({
- error: "Unable to load filelib"
- }));
- }else{
- var musicDis = [];
-
- //Make the user's root music folder if not exists
- if (filelib.fileExists("user:/Music/") == false){
- filelib.mkdir("user:/Music/");
- }
- //Scan all music directories
- var rootDirs = filelib.glob("/");
- for (var i = 0; i < rootDirs.length; i++){
- var thisRoot = rootDirs[i];
-
- //Always use all roots
- musicDis.push(thisRoot);
-
- }
- //Handle user request on listing
- if (typeof(listSong) != "undefined"){
- //List song given type
- if (listSong == "all"){
- //Load from cache first. If cache list > 1000 then deliver the cache then update the cache file
- newDBTableIfNotExists("AirMusic");
- var cacheListRaw = readDBItem("AirMusic", "cache");
- //var isRanged = false; //Check if this only need to return from a range
- //if (typeof(start) != "undefined" && typeof(end) != "undefined"){
- // isRanged = true;
- //}
- if (cacheListRaw == ""){
- //Cache list not generated yet. Continue
- }else if (cacheListRaw != ""){
- //There is something in the cache list. Try parse it
- try{
- //Try parse it.
- var cacheList = JSON.parse(cacheListRaw);
- if (cacheList.length > MINCACHESIZE){
- //Too many songs. Just use the cache list instead.
- sendJSONResp(JSON.stringify({
- cached: true,
- list: cacheList
- }));
- return
- }
- }catch(ex){
- }
- }
- //Do the scanning
- var songData = [];
- var musicFiles = [];
- for (var i = 0; i < musicDis.length; i++){
- var thisFileLib = musicDis[i];
- var allfilelist = filelib.walk(thisFileLib + "Music/", "file");
- for (var k = 0; k < allfilelist.length; k++){
- var ext = allfilelist[k].split('.').pop();
- if (IsSupportExt(ext) == true && !IsMetaFile(allfilelist[k])){
- musicFiles.push(allfilelist[k]);
- }
- }
- }
- for (var i = 0; i < musicFiles.length; i++){
- var thisMusicFile = musicFiles[i];
- var thisSongData = [];
-
- /*
- Catch formats looks like this
- entry = [access_url, filename, ext, filesize]
- */
- //Access Path
- thisSongData.push("/media?file=" + thisMusicFile);
- //File Name only
- var filename = thisMusicFile.split("/").pop()
- filename = filename.split(".");
- var ext = filename.pop();
- filename = filename.join(".");
- thisSongData.push(filename);
- //File Extension
- thisSongData.push(ext);
- //File size
- var fileSize = bytesToSize(filelib.filesize(thisMusicFile))
- thisSongData.push(fileSize)
- songData.push(thisSongData);
- }
- //Return the parsed info
- //sendJSONResp(JSON.stringify(songData));
- //Write to cache and send
- writeDBItem("AirMusic", "cache", JSON.stringify(songData));
- sendJSONResp(JSON.stringify({
- cached: false,
- list: songData
- }));
-
- }else if (listSong.substr(0,7) == "search:"){
- //Search mode
- var keyword = listSong.substr(7)
- keyword = keyword.toLowerCase();
- var songData = [];
- var cachedList = readDBItem("AirMusic", "cache");
- var allfilelist = [];
- function getRealtimeAllFleList(){
- var allfilelist = [];
- for (var i = 0; i < musicDis.length; i++){
- var thisFileLib = musicDis[i];
- thisDirList = filelib.walk(thisFileLib, "file");
- for (var j = 0; j < thisDirList.length; j++){
- allfilelist.push(thisDirList[j]);
- }
- }
- return allfilelist;
- }
- if (cachedList == ""){
- //No cache. Do real time scanning
- allfilelist = getRealtimeAllFleList();
- }else{
- //Try parse it. If parse failed fallback to realtime scanning
- try{
- cachedList = JSON.parse(cachedList);
- for (var j = 0; j < cachedList.length; j++){
- var thisCachedSong = cachedList[j];
- var thisFilepath = thisCachedSong[0].replace("/media?file=", "");
- //Check if this file still exists. If not, get realtime list instead.
- if (filelib.fileExists(thisFilepath)){
- allfilelist.push(thisFilepath);
- }else{
- //Cache outdated. Rescanning now
- allfilelist = getRealtimeAllFleList();
- execd("buildCache.js")
- break;
- }
- }
- }catch(ex){
- //Fallback
- allfilelist = getRealtimeAllFleList();
- }
- }
-
- for (var k = 0; k < allfilelist.length; k++){
- var thisFile = allfilelist[k];
- var ext = allfilelist[k].split('.').pop();
- var filename = allfilelist[k].split('/').pop();
- filename = filename.toLowerCase();
- if (IsSupportExt(ext) == true && filename.indexOf(keyword) !== -1 && !IsMetaFile(allfilelist[k])){
- //This file match our ext req and keyword exists
- var thisSongData = [];
- //Access Path
- thisSongData.push("/media?file=" + thisFile);
- //File Name only
- var filename = thisFile.split("/").pop()
- filename = filename.split(".");
- var ext = filename.pop();
- filename = filename.join(".");
- thisSongData.push(filename);
- //File Extension
- thisSongData.push(ext);
- //File size
- var fileSize = bytesToSize(filelib.filesize(thisFile))
- thisSongData.push(fileSize)
-
- songData.push(thisSongData);
- }
- }
-
- //Send resp
- sendJSONResp(JSON.stringify(songData));
- //Run build cache in background so to update any cache if exists
- execd("buildCache.js")
- }else{
- //WIP
- console.log("listSong type " + listSong + " work in progress")
- }
- }else if (typeof(listdir) != "undefined"){
- //List dir given path
- if (listdir == "root"){
- //List the information of the user roots that contain Music folder
- var rootInfo = [];
- for (var i =0; i < musicDis.length; i++){
- var thisRootInfo = [];
- var thisMusicDir = musicDis[i];
- var thisRoot = thisMusicDir.split("/").shift() + "/";
- var objcetsInRoot = [];
- if (thisRoot == "user:/"){
- objcetsInRoot = filelib.readdir(thisRoot + "Music");
- }else{
- objcetsInRoot = filelib.readdir(thisRoot);
- thisMusicDir = thisRoot;
- }
-
- var rootName = filelib.rootName(thisRoot);
- if (rootName == false){
- rootName = thisRoot
- }
- thisRootInfo.push(rootName);
- thisRootInfo.push(thisMusicDir);
- var files = [];
- var folders = [];
- for (var j = 0; j < objcetsInRoot.length; j++){
- var thisObject = objcetsInRoot[j];
- if (thisObject.IsDir){
- folders.push(thisObject.Filepath);
- }else{
- files.push(thisObject.Filepath);
- }
- }
- thisRootInfo.push(files.length);
- thisRootInfo.push(folders.length);
- rootInfo.push(thisRootInfo);
- }
- sendJSONResp(JSON.stringify(rootInfo));
- }else{
- //List information about other folders
- var targetpath = decodeURIComponent(listdir);
- var filelist = filelib.readdir(targetpath);
- var files = [];
- var fileSizes = [];
- var folders = [];
- for (var j = 0; j < filelist.length; j++){
- var thisFile = filelist[j];
- if (thisFile.IsDir){
- folders.push(thisFile.Filepath);
- }else{
- var ext = thisFile.Ext.substr(1);
- if (IsSupportExt(ext) && !IsMetaFile(thisFile.Filepath)){
- files.push(thisFile.Filepath);
- fileSizes.push(thisFile.Filesize);
- }
- }
- }
- //For each folder, get its information
- var folderInfo = [];
-
- for (var i = 0; i < folders.length; i++){
- var thisFolderInfo = [];
- var folderName = folders[i].split("/").pop();
- /*
- var thisFolderSubfolder = [];
- var thisFolderSubfiles = [];
- var subFolderFileList = filelib.readdir(folders[i])
- for (var j = 0; j < subFolderFileList.length; j++){
- var thisFileinfo = subFolderFileList[j];
- if (thisFileinfo.IsDir){
- var thisFolderName = thisFileinfo.Filename;
- if (thisFolderName.substring(0,1) != "."){
- thisFolderSubfolder.push(thisFileinfo.Filepath);
- }
- }else{
- var ext = thisFileinfo.Ext.substr(1);
- if (IsSupportExt(ext) && !IsMetaFile(thisFileinfo.Filepath)){
- thisFolderSubfiles.push(thisFileinfo.Filepath);
- }
-
- }
- }
- */
- thisFolderInfo.push(folderName);
- thisFolderInfo.push(folders[i] + "/");
- //thisFolderInfo.push((thisFolderSubfiles).length + "");
- //thisFolderInfo.push((thisFolderSubfolder).length + "");
- thisFolderInfo.push(-1);
- thisFolderInfo.push(-1);
- folderInfo.push(thisFolderInfo)
- }
-
-
- var fileInfo = [];
- for (var i = 0; i < files.length; i++){
- var thisFileInfo = [];
- var filename = files[i].split("/").pop();
- filename = filename.split('.')
- filename.pop();
- filename = filename.join(".");
- var ext = files[i].split(".").pop()
- var filesize = fileSizes[i];
- filesize = bytesToSize(filesize);
- thisFileInfo.push("/media?file=" + files[i]);
- thisFileInfo.push(filename);
- thisFileInfo.push(ext);
- thisFileInfo.push(filesize);
- fileInfo.push(thisFileInfo);
- }
- var results = [];
- results.push(folderInfo);
- results.push(fileInfo);
- sendJSONResp(JSON.stringify(results));
- }
-
- }else if (typeof(listFolder) != "undefined"){
- //List folder giben filepath
- }
- }
- }
- //Execute Handler
- handleUserRequest();
|