forked from Onek8/LNXSDK
84 lines
2.0 KiB
Haxe
84 lines
2.0 KiB
Haxe
package leenkx.logicnode;
|
|
#if lnx_torrent
|
|
import leenkx.network.Leenkx;
|
|
import leenkx.network.torrent.Torrent;
|
|
import leenkx.network.torrent.TorrentFile;
|
|
#end
|
|
|
|
class GetTorrentFileNode extends LogicNode {
|
|
public var property0:String;
|
|
public var file:Dynamic;
|
|
var net_Url:String;
|
|
|
|
public function new(tree:LogicTree) {
|
|
super(tree);
|
|
}
|
|
|
|
override function run(from:Int) {
|
|
#if lnx_torrent
|
|
file = null;
|
|
var id = inputs[2].get();
|
|
var match = inputs[3].get();
|
|
if (id != null && match != null) {
|
|
var connection = inputs[1].get();
|
|
if (connection != null) net_Url = connection._url;
|
|
var socket = net_Url != null ? Leenkx.connections[net_Url] : null;
|
|
var client = socket != null ? socket.client : null;
|
|
var t = client != null ? client.get(id) : (Std.isOfType(id, Torrent) ? id : null);
|
|
if (t != null) file = findFile(t, match);
|
|
}
|
|
#end
|
|
runOutput(0);
|
|
}
|
|
|
|
#if lnx_torrent
|
|
function findFile(t:Torrent, match:Dynamic):TorrentFile {
|
|
var files = t.files;
|
|
switch (property0) {
|
|
case "index":
|
|
var i = Std.parseInt(Std.string(match));
|
|
if (i != null && i >= 0 && i < files.length) {
|
|
return files[i];
|
|
}
|
|
case "name":
|
|
var s = Std.string(match);
|
|
for (f in files) {
|
|
if (f.name == s) return f;
|
|
}
|
|
case "path":
|
|
var s = Std.string(match);
|
|
for (f in files) {
|
|
if (f.path == s) return f;
|
|
}
|
|
default:
|
|
var s = Std.string(match);
|
|
if (!StringTools.startsWith(s, ".")) s = "." + s;
|
|
for (f in files) {
|
|
if (StringTools.endsWith(f.name, s)) return f;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
#end
|
|
|
|
override function get(from:Int):Dynamic {
|
|
#if lnx_torrent
|
|
var f:TorrentFile = file;
|
|
return switch (from) {
|
|
case 1: file;
|
|
case 2: f != null ? f.name : null;
|
|
case 3: f != null ? f.path : null;
|
|
case 4: f != null ? f.length : 0;
|
|
case 5: f != null ? f.type : null;
|
|
case 6: f != null ? f.getBytes() : null;
|
|
case 7: f != null;
|
|
case 8: f != null ? f.downloaded : 0.0;
|
|
case 9: f != null ? f.progress : 0.0;
|
|
default: throw "Unreachable";
|
|
}
|
|
#else
|
|
return null;
|
|
#end
|
|
}
|
|
}
|