From adc55ee420abc3751550bfb273e18441fd99b0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Tue, 29 May 2018 15:02:09 +0200 Subject: [PATCH] Updated thumbnailchecking - Borrowing youtube 404 image for now if someone tries to trick the server to use other thumbnail than soundlcoud thumbnail when adding soundcloud song - Setting thumbnail in db even for youtube-videos, for more consistency --- server/handlers/list_change.js | 15 +++++++++++- server/public/assets/js/list.js | 35 +++++++++++++++------------- server/public/assets/js/listeners.js | 2 +- server/public/assets/js/search.js | 4 ++-- server/routing/client/api.js | 9 ++++--- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/server/handlers/list_change.js b/server/handlers/list_change.js index 8f3fb0c8..9b2baa0f 100644 --- a/server/handlers/list_change.js +++ b/server/handlers/list_change.js @@ -209,6 +209,13 @@ function addPlaylist(arr, guid, offline, socket) { if(this_element.start > this_element.end) { this_element.start = 0; } + if(this_element.source == "soundcloud") { + if(this_element.thumbnail.indexOf("https://i1.sndcdn.com") > -1 || this_element.thumbnail.indexOf("https://w1.sndcdn.com") > -1) { + this_element.thumbnail = this_element.thumbnail; + } else { + this_element.thumbnail = "https://img.youtube.com/vi/404_notfound/mqdefault.jpg"; + } + } else if(this_element.source == "youtube") this_element.thumbnail = "https://img.youtube.com/vi/" + this_element.id + "/mqdefault.jpg"; if(now_playing) { now_playing = false; } @@ -392,7 +399,13 @@ function add_function(arr, coll, guid, offline, socket) { np = false; } var new_song = {"added": added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration, "start": parseInt(start), "end": parseInt(end), "type": "video", "source": source}; - if(source == "soundcloud") new_song.thumbnail = arr.thumbnail; + if(source == "soundcloud") { + if(arr.thumbnail.indexOf("https://i1.sndcdn.com") > -1 || arr.thumbnail.indexOf("https://w1.sndcdn.com") > -1) { + new_song.thumbnail = arr.thumbnail; + } else { + new_song.thumbnail = "https://img.youtube.com/vi/404_notfound/mqdefault.jpg"; + } + } else if(source == "youtube") new_song.thumbnail = "https://img.youtube.com/vi/" + new_song.id + "/mqdefault.jpg"; db.collection(coll).update({id: id}, new_song, {upsert: true}, function(err, docs){ new_song._id = "asd"; if(np) { diff --git a/server/public/assets/js/list.js b/server/public/assets/js/list.js index 94e094df..e305486d 100755 --- a/server/public/assets/js/list.js +++ b/server/public/assets/js/list.js @@ -245,23 +245,26 @@ var List = { check_error_videos: function(i) { //Helper.log("Empty-checker at " + i); - if(full_playlist.length == 0 || full_playlist[i].source == "soundcloud") return; - Helper.ajax({ - method: "get", - url: 'https://www.googleapis.com/youtube/v3/videos?id=' + full_playlist[i].id - + "&key=" + api_key + "&part=snippet", - success: function (data) { - data = JSON.parse(data); - //Helper.log("Empty-checker items " + data.items.length); - if (data.items.length == 0) { - Helper.log(["Emtpy-checker error at " + full_playlist[i].id + " " + full_playlist[i].title]); - socket.emit("error_video", {channel: chan.toLowerCase(), id: full_playlist[i].id, title: full_playlist[i].title, source: full_playlist[i].source}); + if(full_playlist.length == 0) return; + else if(full_playlist[i].source == "soundcloud" && full_playlist.length > i + 1 && window.location.pathname != "/") List.check_error_videos(i + 1); + else { + Helper.ajax({ + method: "get", + url: 'https://www.googleapis.com/youtube/v3/videos?id=' + full_playlist[i].id + + "&key=" + api_key + "&part=snippet", + success: function (data) { + data = JSON.parse(data); + //Helper.log("Empty-checker items " + data.items.length); + if (data.items.length == 0) { + Helper.log(["Emtpy-checker error at " + full_playlist[i].id + " " + full_playlist[i].title]); + socket.emit("error_video", {channel: chan.toLowerCase(), id: full_playlist[i].id, title: full_playlist[i].title, source: full_playlist[i].source}); + } + if(full_playlist.length > i + 1 && window.location.pathname != "/") { + List.check_error_videos(i + 1); + } } - if(full_playlist.length > i + 1 && window.location.pathname != "/") { - List.check_error_videos(i + 1); - } - } - }) + }); + } }, dynamicContentPageJumpTo: function(page) { diff --git a/server/public/assets/js/listeners.js b/server/public/assets/js/listeners.js index 4878eecb..6c564308 100755 --- a/server/public/assets/js/listeners.js +++ b/server/public/assets/js/listeners.js @@ -1306,7 +1306,7 @@ addListener("click", ".result-object", function(e){ var start = parseInt(e.querySelector(".result-start").value); var end = parseInt(e.querySelector(".result-end").value); var source = "youtube"; - var thumbnail; + var thumbnail = "https://img.youtube.com/vi/" + id + "/mqdefault.jpg"; if(e.getAttribute("data-type-source") != undefined) { source = "soundcloud"; thumbnail = e.getAttribute("data-type-thumbnail"); diff --git a/server/public/assets/js/search.js b/server/public/assets/js/search.js index 6843c3f3..4db33d72 100755 --- a/server/public/assets/js/search.js +++ b/server/public/assets/js/search.js @@ -372,7 +372,7 @@ var Search = { if((!not_matched)){ matched = true; - Search.readySubmit(true, { id: data.id, title: data.snippet.title, source: "youtube", duration: duration, totalLength: totalNumber - 1}); + Search.readySubmit(true, { id: data.id, title: data.snippet.title, source: "youtube", thumbnail: "https://img.youtube.com/vi/" + data.id + "/mqdefault.jpg", duration: duration, totalLength: totalNumber - 1}); break; } } @@ -615,7 +615,7 @@ addVideos: function(ids){ enc_title= song.snippet.title;//encodeURIComponent(song.snippet.title); //Search.submit(song.id, enc_title, duration, playlist, i); x += 1; - Search.submitYouTubeArray.push({id: song.id, title: enc_title, duration: duration}); + Search.submitYouTubeArray.push({id: song.id, title: enc_title, duration: duration, source: "youtube", thumbnail: "https://img.youtube.com/vi/" + song.id + "/mqdefault.jpg"}); } } if(more) Search.addVideos(next_ids); diff --git a/server/routing/client/api.js b/server/routing/client/api.js index 833de99f..7846f9e6 100644 --- a/server/routing/client/api.js +++ b/server/routing/client/api.js @@ -648,7 +648,6 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { var title = req.body.title; if(typeof(userpass) != "string" || typeof(adminpass) != "string" || typeof(title) != "string" || isNaN(duration) || isNaN(start_time) || isNaN(end_time)) { - console.log("this place crash"); throw "Wrong format"; } } @@ -726,11 +725,15 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { var new_song = {"added": Functions.get_time(),"guids":[guid],"id":video_id,"now_playing":set_np,"title":title,"votes":1, "duration":duration, "start": parseInt(start_time), "end": parseInt(end_time), "type": song_type, "source": source}; var runFunction = Search.get_correct_info; if(source == "soundcloud") { - new_song.thumbnail = req.body.thumbnail; + if(req.body.thumbnail.thumbnail.indexOf("https://i1.sndcdn.com") > -1 || req.body.thumbnail.thumbnail.indexOf("https://w1.sndcdn.com") > -1) { + new_song.thumbnail = req.body.thumbnail.thumbnail; + } else { + new_song.thumbnail = "https://img.youtube.com/vi/404_notfound/mqdefault.jpg"; + } runFunction = function(new_song, foo_2, foo_3, callback) { callback(new_song, true); } - } + } else if(source == "youtube") new_song.thumbnail = "https://img.youtube.com/vi/" + new_song.id + "/mqdefault.jpg"; runFunction(new_song, channel_name, false, function(element, found) { if(!found) { res.status(404).send(JSON.stringify(error.not_found.youtube));