53 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-10-11 17:34:26 +08:00
import QbConfigDto from "../entity/dto/QbConfigDto";
import {tryLogin, get, post, updateQbInfo, getQbInfo} from '../util/QbApiUtil';
2022-12-02 11:04:17 +08:00
import GlobalConfigService from "./GlobalConfigService";
import GlobalConfig from "../entity/po/GlobalConfig";
2023-10-11 17:34:26 +08:00
import BtListItemDto from "../entity/dto/BtListItemDto";
2022-12-02 11:04:17 +08:00
class QbService {
2023-07-28 20:12:34 +08:00
/**
*
* @param body
*/
2023-10-11 17:34:26 +08:00
static async saveAddress(body: QbConfigDto): Promise<QbConfigDto> {
if (body.address.endsWith("/")) {
body.address = body.address.substring(0, body.address.length - 1);
}
await GlobalConfigService.insertOrReplace(new GlobalConfig("qbConfig", JSON.stringify(body), "qb config"));
2023-07-28 20:12:34 +08:00
updateQbInfo(body);
2023-10-11 17:34:26 +08:00
body.valid = await tryLogin();
body.version = body ? (await get("/app/version", null)) : null;
return body;
2023-07-28 20:12:34 +08:00
}
2023-10-21 11:31:29 +08:00
a
2023-07-28 20:12:34 +08:00
/**
*
*/
2023-10-21 11:31:29 +08:00
static async getConfig(): Promise<QbConfigDto> {
2023-07-28 20:12:34 +08:00
return getQbInfo();
}
2023-10-21 11:31:29 +08:00
/**
* get torrents list from qb
*/
2023-10-11 17:34:26 +08:00
static async getBtList(): Promise<Array<BtListItemDto>> {
let res = await get("/api/v2/torrents/info?category=&sort=added_on", null);
return res;
}
/**
*
*/
2023-07-28 20:12:34 +08:00
static async init() {
2023-10-11 17:34:26 +08:00
let config = await GlobalConfigService.getVal("qbConfig");
let qbInfo: QbConfigDto = config == null ? {} : JSON.parse(config);
2023-07-28 20:12:34 +08:00
updateQbInfo(qbInfo);
qbInfo.valid = await tryLogin();
2023-10-11 17:34:26 +08:00
qbInfo.version = qbInfo.valid ? (await get("/app/version", null)) : null;
return qbInfo;
2023-07-28 20:12:34 +08:00
}
2022-12-02 11:04:17 +08:00
}
export default QbService;