|
|
@ -1,80 +1,16 @@ |
|
|
|
import { Socket } from "net"; |
|
|
|
import Application from "../../Application"; |
|
|
|
import { IpcClientService } from "@autoplex/microservice"; |
|
|
|
import { env } from "@autoplex/utils"; |
|
|
|
import { IpcClient } from "@autoplex-api/torrent"; |
|
|
|
|
|
|
|
interface IResponse { |
|
|
|
response?: any, |
|
|
|
error?: string | Error |
|
|
|
} |
|
|
|
|
|
|
|
export interface ITorrent { |
|
|
|
name: string, |
|
|
|
infoHash: string, |
|
|
|
progress: number, |
|
|
|
state: TorrentState |
|
|
|
} |
|
|
|
|
|
|
|
export interface ISerializedFile { |
|
|
|
path : string; |
|
|
|
size : number; |
|
|
|
downloaded: number; |
|
|
|
progress : number; |
|
|
|
selected : boolean; |
|
|
|
} |
|
|
|
|
|
|
|
export interface ISerializedTorrent { |
|
|
|
name : string; |
|
|
|
infoHash : string; |
|
|
|
downloaded : number; |
|
|
|
uploaded : number; |
|
|
|
ratio : number; |
|
|
|
size : number; |
|
|
|
downloadSpeed: number; |
|
|
|
uploadSpeed : number; |
|
|
|
numPeers : number; |
|
|
|
progress : number; |
|
|
|
path : string; |
|
|
|
state : TorrentState; |
|
|
|
files : ISerializedFile[]; |
|
|
|
} |
|
|
|
|
|
|
|
export enum TorrentState { |
|
|
|
Ready = 0x1, |
|
|
|
Paused = 0x2, |
|
|
|
Done = 0x4 |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* A custom error type for torrent client connection errors |
|
|
|
*/ |
|
|
|
export class TorrentClientConnectionError extends Error { |
|
|
|
constructor(...args: any[]) { |
|
|
|
super(...args); |
|
|
|
Object.setPrototypeOf(this, TorrentClientConnectionError.prototype); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* The torrent client IPC service |
|
|
|
*/ |
|
|
|
export default abstract class TorrentClientIpc extends IpcClientService<Application> |
|
|
|
export default abstract class TorrentClientIpc extends IpcClient<Application> |
|
|
|
{ |
|
|
|
/** |
|
|
|
* The path to the socket file |
|
|
|
*/ |
|
|
|
public readonly SOCKET_PATH = env("TORRENT_CLIENT_IPC_SOCKET"); |
|
|
|
|
|
|
|
/** |
|
|
|
* Install the event handlers for the IPC socket |
|
|
|
*/ |
|
|
|
protected installSocketEventHandlers(socket: Socket) { |
|
|
|
socket.on("connect", () => this.onConnect()); |
|
|
|
socket.on("error", (error: any) => this.onError(error)); |
|
|
|
socket.on("disconnect", () => this.onDisconnect()); |
|
|
|
socket.on("destroy", () => this.onDestroy()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Install the message event handlers |
|
|
|
*/ |
|
|
@ -86,54 +22,4 @@ export default abstract class TorrentClientIpc extends IpcClientService<Applicat |
|
|
|
* Invoked when a torrent has finished downloading |
|
|
|
*/ |
|
|
|
protected async onTorrentFinished(infoHash: string) {} |
|
|
|
|
|
|
|
// Methods -------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
|
* Add a torrent to the client |
|
|
|
* @param torrent Magnet URI or file buffer |
|
|
|
*/ |
|
|
|
protected async add(torrent: string | Buffer, downloadPath?: string) { |
|
|
|
let response = await this.request("add", { torrent, downloadPath }); |
|
|
|
if (response.error) { |
|
|
|
throw new Error("Failed to add torrent"); |
|
|
|
} |
|
|
|
return <string>response.data; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Remove a torrent from the client |
|
|
|
* @param torrent Torrent info hash |
|
|
|
*/ |
|
|
|
protected async remove(torrent: string) { |
|
|
|
let response = await this.request("remove", torrent); |
|
|
|
if (response.error) { |
|
|
|
throw new Error("Failed to remove torrent"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get a list of all torrents in the client |
|
|
|
*/ |
|
|
|
protected async list() { |
|
|
|
let response = await this.request("list"); |
|
|
|
if (response.error) { |
|
|
|
console.error(response.error); |
|
|
|
throw new Error("Failed to obtain torrent list"); |
|
|
|
} |
|
|
|
return <ITorrent[]>response.data; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get full details of each of the provided torrents |
|
|
|
* @param torrentIds Array of torrent info hashes |
|
|
|
*/ |
|
|
|
protected async details(...torrentIds: string[]) { |
|
|
|
let response = await this.request("details", torrentIds); |
|
|
|
if (response.error) { |
|
|
|
console.error(response.error); |
|
|
|
throw new Error("Failed to retrieve torrent details"); |
|
|
|
} |
|
|
|
return <ISerializedTorrent[]>response.data; |
|
|
|
} |
|
|
|
} |