forked from LeenkxTeam/LNXSDK
Leenkx Network Torrent
This commit is contained in:
102
leenkx/Sources/leenkx/network/torrent/TorrentFile.hx
Normal file
102
leenkx/Sources/leenkx/network/torrent/TorrentFile.hx
Normal file
@ -0,0 +1,102 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.MetaInfo.TorrentFileEntry;
|
||||
import leenkx.network.torrent.storage.Storage.IStorage;
|
||||
|
||||
class TorrentFile {
|
||||
public var name:String;
|
||||
public var path:String;
|
||||
public var length:Int;
|
||||
public var offset:Int;
|
||||
|
||||
var storage:IStorage;
|
||||
public var torrent:Torrent;
|
||||
|
||||
public function new(entry:TorrentFileEntry, storage:IStorage, ?torrent:Torrent) {
|
||||
this.path = entry.path;
|
||||
this.name = entry.path.split("/").pop();
|
||||
this.length = entry.length;
|
||||
this.offset = entry.offset;
|
||||
this.storage = storage;
|
||||
this.torrent = torrent;
|
||||
}
|
||||
|
||||
public var downloaded(get, never):Float;
|
||||
function get_downloaded():Float {
|
||||
if (torrent == null || torrent.pieces == null) return 0;
|
||||
var pl = torrent.meta.pieceLength;
|
||||
return Math.min(havePieces() * (pl:Float), length);
|
||||
}
|
||||
|
||||
public var progress(get, never):Float;
|
||||
function get_progress():Float {
|
||||
return length > 0 ? downloaded / length : 0;
|
||||
}
|
||||
|
||||
function havePieces():Int {
|
||||
var pl = torrent.meta.pieceLength;
|
||||
var first = Std.int(offset / pl);
|
||||
var last = length > 0
|
||||
? Std.int((offset + length - 1) / pl) : first;
|
||||
var n = 0;
|
||||
for (i in first...last + 1) {
|
||||
if (torrent.pieces.have[i]) n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public function select(?priority:Int):Void {
|
||||
if (torrent == null) return;
|
||||
torrent.select(offset, offset + length - 1, priority);
|
||||
}
|
||||
|
||||
public function deselect(?priority:Int):Void {
|
||||
if (torrent == null) return;
|
||||
torrent.deselect(offset, offset + length - 1, priority);
|
||||
}
|
||||
|
||||
public function critical():Void {
|
||||
if (torrent == null) return;
|
||||
torrent.critical(offset, offset + length - 1);
|
||||
}
|
||||
|
||||
public var type(get, never):String;
|
||||
function get_type():String {
|
||||
var i = name != null ? name.lastIndexOf(".") : -1;
|
||||
var ext = i >= 0 ? name.substr(i + 1).toLowerCase() : "";
|
||||
return switch (ext) {
|
||||
case "mp4" | "m4v": "video/mp4";
|
||||
case "webm": "video/webm";
|
||||
case "mkv": "video/x-matroska";
|
||||
case "mov": "video/quicktime";
|
||||
case "avi": "video/x-msvideo";
|
||||
case "mp3": "audio/mpeg";
|
||||
case "ogg": "audio/ogg";
|
||||
case "ogv": "video/ogg";
|
||||
case "flac": "audio/flac";
|
||||
case "wav": "audio/wav";
|
||||
case "aac": "audio/aac";
|
||||
case "jpg" | "jpeg": "image/jpeg";
|
||||
case "png": "image/png";
|
||||
case "gif": "image/gif";
|
||||
case "webp": "image/webp";
|
||||
case "svg": "image/svg+xml";
|
||||
case "txt" | "log" | "md": "text/plain";
|
||||
case "html" | "htm": "text/html";
|
||||
case "css": "text/css";
|
||||
case "json": "application/json";
|
||||
case "pdf": "application/pdf";
|
||||
case "zip": "application/zip";
|
||||
default: "application/octet-stream";
|
||||
}
|
||||
}
|
||||
|
||||
public function getBytes():Bytes {
|
||||
return storage.read(offset, length);
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return getBytes().toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user