Example #1
0
AlbumArt.prototype.getAlbumArt = function (data, path,icon) {

    var artist, album, size;

    if (data != undefined && data.path != undefined) {
        path = this.sanitizeUri(data.path);
    }

    var web;
	try {
    if (data != undefined && data.artist != undefined && enableweb) {
        //performing decode since we cannot assume client will pass decoded strings

        artist = nodetools.urlDecode(data.artist);

        if(data.album)
        {
            album = nodetools.urlDecode(data.album);
        }
        else
        {
            album ='';
        }

        if (data.size) {
			size=data.size;
		} else {
			size=defaultwebsize;
		}

        web = '?web=' + nodetools.urlEncode(artist) + '/' + nodetools.urlEncode(album) + '/'+size;
    }
	} catch (e) {
	console.log('Cannot compose Albumart path')
	}
    var url = '/albumart';

    if (web != undefined)
        url = url + web;

    if (web != undefined && path != undefined)
        url = url + '&';
    else if (path != undefined)
        url = url + '?';

    if (path != undefined)
        url = url + 'path=' + nodetools.urlEncode(path);

    if(icon!==undefined)
    {
        if(url==='/albumart')
            url=url+'?icon='+icon;
        else url=url+'&icon='+icon;
    }

    url=url+'&cacheid='+cacheid;

    return url;
};
Example #2
0
var searchOnline = function (defer, web) {
	/**
	 * If we arrive to this point the file albumart has not been passed or doesn't exists
	 */

	var artist, album, resolution;

	if (web != undefined) {
		var splitted = nodetools.urlDecode(web).split('/');

		if (splitted.length < 3) {
			defer.reject(new Error('The web link ' + web + ' is malformed'));
			return defer.promise;
		}

		if (splitted.length == 3) {
			artist = splitted[0];
			album = splitted[1];
			resolution = splitted[2];
		}
		else if (splitted.length == 4) {
			artist = splitted[1];
			album = splitted[2];
			resolution = splitted[3];
		}
	}
	else {
		defer.reject(new Error('No parameters defined'));
		return defer.promise;
	}

	/**
	 * Loading album art from network
	 */
	var folder = albumArtRootFolder + artist + '/' + album + '/';
	var fileName = resolution;

	fs.ensureDirSync(folder);
	var infoPath = folder + 'info.json';

	var infoJson = {};

	if (fs.existsSync(infoPath) == false) {
		fs.ensureFileSync(infoPath);
		fs.writeJsonSync(infoPath, infoJson);
	}

	var stats = fs.statSync(infoPath)
	var fileSizeInBytes = stats["size"]

	if (fileSizeInBytes > 0)
		infoJson = fs.readJsonSync(infoPath, {throws: false})

	if (infoJson[resolution] == undefined) {
		albumart(artist, album, resolution, function (err, url) {
			if (err) {
				albumart(artist, function (err, url) {
					if (err) {
						console.log("ERRORE: " + err);
						defer.reject(new Error(err));
						return defer.promise;
					}
					else {
						if (url != undefined && url != '') {
							var splitted = url.split('.');
							var fileExtension = splitted[splitted.length - 1];
							var diskFileName = uuid.v4() + '.' + fileExtension;

							var options = {
								directory: folder,
								filename: diskFileName
							}

							//console.log("URL: " + url);
							download(url, options, function (err) {
								if (err) defer.reject(new Error(err));
								else defer.resolve(folder + diskFileName);
							});

							infoJson[resolution] = diskFileName;
						}
						else {
							defer.reject(new Error('No albumart URL'));
							return defer.promise;
						}
					}

					fs.writeJsonSync(infoPath, infoJson);
				});
			}
			else {
				if (url != undefined && url != '') {
					var splitted = url.split('.');
					var fileExtension = splitted[splitted.length - 1];
					var diskFileName = uuid.v4() + '.' + fileExtension;

					var options = {
						directory: folder,
						filename: diskFileName
					}
					download(url, options, function (err) {
						if (err) defer.reject(new Error(err));
						else defer.resolve(folder + diskFileName);
					});
				}
				else {
					defer.reject(new Error('No albumart URL'));
					return defer.promise;
				}

				infoJson[resolution] = diskFileName;

			}

			fs.writeJsonSync(infoPath, infoJson);
		});
	}
	else {
		defer.resolve(folder + infoJson[resolution]);
	}
};
Example #3
0
var processRequest = function (web, path) {
	var defer = Q.defer();

	if (web == undefined && path == undefined) {
	    logger.info('No input data');
		defer.reject(new Error(''));
		return defer.promise;
	}

	if (path != undefined) {
        path=nodetools.urlDecode(path);

        path=sanitizeUri(path);

        if(path.startsWith('/')){
        	if (path.startsWith('/tmp/')){

			} else {
				path = '/mnt' + path;
			}
		} else {
			path = '/mnt/' + path;
		}

        if (fs.existsSync(path)) {
            var stats = fs.statSync(path);
            var isFolder=false;
            var imageSize='extralarge';

            /**
             * Trying to hit the disk cache
             *
             */
            var coverFolder = '';

            if (stats.isDirectory()) {
                coverFolder=path;
                isFolder=true;
            }
            else {
                var splitted = path.split('/');

                for (var k = 0; k < splitted.length - 1; k++) {
                    coverFolder = coverFolder + '/' + splitted[k];
                }
            }

            fs.ensureDirSync(coverFolder);
            var cacheFilePath=mountAlbumartFolder+coverFolder+'/'+imageSize+'.jpeg';
            logger.info(cacheFilePath);


            if(fs.existsSync(cacheFilePath))
            {
                defer.resolve(cacheFilePath);
            }
            else {
                if (isFolder) {
                    searchInFolder(defer, path, web);
                } else {
                    var starttime=Date.now();
                    searchInFolder(defer, path, web);
                    /*var parser = mm(fs.createReadStream(path), function (err, metadata) {
                     if (err) {
                     logger.info(err);
                     searchInFolder(defer, path, web);
                     }
                     else {
                     try {
                     var stoptime=Date.now();
                     logger.info("Parsing took "+(stoptime-starttime)+" milliseconds");
                     if (metadata.picture != undefined && metadata.picture.length > 0) {
                     logger.info("Found art in file " + path);

                     fs.writeFile('/tmp/albumart', metadata.picture[0].data, function (err) {
                     //console.log('file has been written');
                     defer.resolve('/tmp/albumart');
                     });
                     }
                     else searchInFolder(defer, path, web);
                     }
                     catch (ecc) {
                     logger.info(ecc);
                     }
                     }
                     });*/
                }
            }


		} else {
			logger.info('File' + path + ' doesnt exist');
			searchInFolder(defer, path, web);
		}

	}
    else
    {
        searchOnline(defer,web);
    }
	return defer.promise;
};