|
|
@ -1,6 +1,8 @@ |
|
|
|
import { MiddlewareMethod, RouteRegisterFactory } from "@autoplex/webserver"; |
|
|
|
import { IpcConnectionError } from "@autoplex/ipc"; |
|
|
|
import { MiddlewareMethod, RouteRegisterFactory, Status, respond, } from "@autoplex/webserver"; |
|
|
|
import Application from "../../../Application"; |
|
|
|
import TorrentIpc from "../../TorrentIpc"; |
|
|
|
import { torrent } from "../middleware/torrent"; |
|
|
|
|
|
|
|
export default function register(factory: RouteRegisterFactory<MiddlewareMethod<void>, Application>, |
|
|
|
app: Application) |
|
|
@ -18,74 +20,67 @@ export default function register(factory: RouteRegisterFactory<MiddlewareMethod< |
|
|
|
/** |
|
|
|
* Get the torrent list |
|
|
|
*/ |
|
|
|
factory.get("/list", async (request, reply) => { |
|
|
|
factory.get("/list", async (_, reply) => { |
|
|
|
let torrentList = await ipc.list(); |
|
|
|
reply.send({ result: torrentList, status: "Success" }); |
|
|
|
respond(reply, Status.Ok, torrentList); |
|
|
|
}); |
|
|
|
|
|
|
|
/** |
|
|
|
* Fetch the details of all torrents |
|
|
|
*/ |
|
|
|
factory.get("/details", async (_, reply) => { |
|
|
|
let details = await ipc.details(); |
|
|
|
respond(reply, Status.Ok, details); |
|
|
|
}); |
|
|
|
|
|
|
|
/** |
|
|
|
* Fetch the details of a torrent |
|
|
|
*/ |
|
|
|
factory.get("/details/:infoHash", async (request, reply) => { |
|
|
|
let infoHash: string = (<any>request.params)["infoHash"]; |
|
|
|
if (infoHash === undefined) { |
|
|
|
reply.status(400); |
|
|
|
reply.send({ status: "Bad request" }); |
|
|
|
return; |
|
|
|
} |
|
|
|
try { |
|
|
|
let details = await ipc.details(infoHash); |
|
|
|
reply.send({ result: details, status: "Success" }); |
|
|
|
} catch(e) { |
|
|
|
reply.status(404); |
|
|
|
reply.send({ status: "Not found" }); |
|
|
|
} |
|
|
|
factory.get("/details/:torrentId", [torrent], async (request, reply) => { |
|
|
|
let details = request.middlewareParams.torrent; |
|
|
|
respond(reply, Status.Ok, details); |
|
|
|
}); |
|
|
|
|
|
|
|
/** |
|
|
|
* Add a torrent to the client |
|
|
|
*/ |
|
|
|
factory.post("/add", async (request, reply) => { |
|
|
|
let downloadPath = (<any>request.query)["downloadPath"] ?? undefined; |
|
|
|
let result: string[] = []; |
|
|
|
if (request.isMultipart()) { |
|
|
|
let files = request.files(); |
|
|
|
for await (let file of files) { |
|
|
|
let torrentFile = await file.toBuffer(); |
|
|
|
let infoHash = await ipc.add(torrentFile); |
|
|
|
let infoHash = await ipc.add(torrentFile, downloadPath); |
|
|
|
result.push(infoHash); |
|
|
|
} |
|
|
|
} else { |
|
|
|
let links = (<any>request.body)?.link ?? null; |
|
|
|
if (links === null) { |
|
|
|
reply.status(400); |
|
|
|
reply.send({ status: "Bad request" }); |
|
|
|
return; |
|
|
|
return respond(reply, Status.BadRequest); |
|
|
|
} |
|
|
|
for (let link of (Array.isArray(links) ? links : [links])) { |
|
|
|
let infoHash = await ipc.add(link); |
|
|
|
let infoHash = await ipc.add(link, downloadPath); |
|
|
|
result.push(infoHash); |
|
|
|
} |
|
|
|
} |
|
|
|
reply.send({ result: result, status: "Success" }); |
|
|
|
respond(reply, Status.Ok, result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
/** |
|
|
|
* Remove a torrent from the client |
|
|
|
*/ |
|
|
|
factory.delete("/remove/:infoHash", async (request, reply) => { |
|
|
|
let infoHash: string = (<any>request.params)["infoHash"]; |
|
|
|
if (infoHash === undefined) { |
|
|
|
reply.status(400); |
|
|
|
reply.send({ status: "Bad request" }); |
|
|
|
return; |
|
|
|
} |
|
|
|
try { |
|
|
|
await ipc.remove(infoHash); |
|
|
|
reply.send({ status: "Success" }); |
|
|
|
} catch(e) { |
|
|
|
reply.status(404); |
|
|
|
reply.send({ status: "Not found" }); |
|
|
|
} |
|
|
|
/** |
|
|
|
* Remove a torrent from the client |
|
|
|
*/ |
|
|
|
factory.delete("/remove/:torrentId", [torrent], async (request, reply) => { |
|
|
|
let torrent = request.middlewareParams.torrent; |
|
|
|
try { |
|
|
|
ipc.remove(torrent.infoHash); |
|
|
|
} catch(e) { |
|
|
|
if (e instanceof IpcConnectionError) { |
|
|
|
return respond(reply, Status.InternalServerError); |
|
|
|
} |
|
|
|
return respond(reply, Status.Gone); |
|
|
|
} |
|
|
|
respond(reply, Status.Ok); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |