diff options
| -rw-r--r-- | build/server.js | 24 | ||||
| -rw-r--r-- | src/server/Cache.hx | 19 |
2 files changed, 31 insertions, 12 deletions
diff --git a/build/server.js b/build/server.js index 6c00510..aa3d8e1 100644 --- a/build/server.js +++ b/build/server.js @@ -3635,7 +3635,7 @@ server_Cache.prototype = { return; } var outName = videoId + ".mp4"; - if(this.cachedFiles.indexOf(outName) != -1) { + if(this.cachedFiles.indexOf(outName) != -1 && sys_FileSystem.exists("" + this.cacheDir + "/" + outName)) { callback(outName); return; } @@ -3694,7 +3694,7 @@ server_Cache.prototype = { if(count < 2) { return; } - var args = ("-y -i input-video -i input-audio -c copy -map 0:v -map 1:a " + outName).split(" "); + var args = ("-y -i input-video -i input-audio -c copy -map 0:v -map 1:a ./" + outName).split(" "); var $process = js_node_ChildProcess.spawn("ffmpeg",args,{ cwd : _gthis.cacheDir, stdio : "ignore"}); $process.on("close",function(code) { if(code != 0) { @@ -3709,7 +3709,9 @@ server_Cache.prototype = { if(sys_FileSystem.exists(inAudio)) { js_node_Fs.unlinkSync(inAudio); } - _gthis.cachedFiles.push(outName); + if(_gthis.cachedFiles.indexOf(outName) == -1) { + _gthis.cachedFiles.unshift(outName); + } _gthis.removeOlderCache(); callback(outName); }); @@ -3726,7 +3728,7 @@ server_Cache.prototype = { } ,removeOlderCache: function() { while(this.getUsedSpace() > this.storageLimit) { - var name = this.cachedFiles.shift(); + var name = this.cachedFiles.pop(); var path = "" + this.cacheDir + "/" + name; if(sys_FileSystem.exists(path)) { js_node_Fs.unlinkSync(path); @@ -3735,9 +3737,17 @@ server_Cache.prototype = { } ,getUsedSpace: function() { var total = 0; - var _g = 0; - var _g1 = this.cachedFiles; - while(_g < _g1.length) total += js_node_Fs.statSync("" + this.cacheDir + "/" + _g1[_g++]).size; + var arr = this.cachedFiles; + var _g_i = arr.length - 1; + while(_g_i > -1) { + var name = arr[_g_i--]; + var path = "" + this.cacheDir + "/" + name; + if(!sys_FileSystem.exists(path)) { + HxOverrides.remove(this.cachedFiles,name); + continue; + } + total += js_node_Fs.statSync(path).size; + } return total; } ,getBestYoutubeVideoFormat: function(formats) { diff --git a/src/server/Cache.hx b/src/server/Cache.hx index 8348476..8681ea2 100644 --- a/src/server/Cache.hx +++ b/src/server/Cache.hx @@ -55,7 +55,7 @@ class Cache { return; } final outName = videoId + ".mp4"; - if (cachedFiles.contains(outName)) { + if (cachedFiles.contains(outName) && FileSystem.exists('$cacheDir/$outName')) { callback(outName); return; } @@ -102,11 +102,14 @@ class Cache { count++; log(client, '$type track downloaded ($count/2)'); if (count < 2) return; - final args = '-y -i input-video -i input-audio -c copy -map 0:v -map 1:a $outName'.split(" "); + final args = '-y -i input-video -i input-audio -c copy -map 0:v -map 1:a ./$outName'.split(" "); final process = ChildProcess.spawn("ffmpeg", args, { cwd: cacheDir, stdio: "ignore" }); + // process.stderr.on('data', (data) -> { + // trace('FFmpeg stderr: ${data}'); + // }); process.on("close", (code:Int) -> { if (code != 0) { log(client, 'Error: ffmpeg closed with code $code'); @@ -117,7 +120,9 @@ class Cache { if (FileSystem.exists(inVideo)) FileSystem.deleteFile(inVideo); if (FileSystem.exists(inAudio)) FileSystem.deleteFile(inAudio); - cachedFiles.push(outName); + if (!cachedFiles.contains(outName)) { + cachedFiles.unshift(outName); + } removeOlderCache(); callback(outName); @@ -136,7 +141,7 @@ class Cache { function removeOlderCache():Void { while (getUsedSpace() > storageLimit) { - final name = cachedFiles.shift(); + final name = cachedFiles.pop(); final path = '$cacheDir/$name'; if (FileSystem.exists(path)) FileSystem.deleteFile(path); } @@ -144,8 +149,12 @@ class Cache { function getUsedSpace():Int { var total = 0; - for (name in cachedFiles) { + for (name in cachedFiles.reversed()) { final path = '$cacheDir/$name'; + if (!FileSystem.exists(path)) { + cachedFiles.remove(name); + continue; + } total += FileSystem.stat(path).size; } return total; |
