import path from 'path'; import { createWriteStream } from 'fs-extra'; import { container } from '../../core/container'; import type { DialogInterface } from '../dialog/dialog-interface'; import { answer } from '../ipc/annotations/answer'; import type { SourceGetterInterface } from '../source/source-getter-interface'; export class NhentaiIpcController implements IpcController { private readonly nhentaiApi: NhentaiApiInterface; private readonly nhentaiSourceGetter: SourceGetterInterface; private readonly translator: I18nTranslatorInterface; private readonly dialog: DialogInterface; private constructor( nhentaiApi: NhentaiApiInterface, nhentaiSourceGetter: SourceGetterInterface, translator: I18nTranslatorInterface, dialog: DialogInterface ) { this.nhentaiApi = nhentaiApi; this.nhentaiSourceGetter = nhentaiSourceGetter; this.translator = translator; this.dialog = dialog; } @answer(IpcChannel.NHENTAI_SAVE_FAVORITES) public async nhentaiSaveFavorites(): Promise { const result = await this.dialog.selectFolder({ title: this.translator.t('Select torrent file save location'), }); if (result.canceled) { return; } const favoritesStream = await this.nhentaiApi.getFavorites(); return new Promise((resolve) => { favoritesStream.on('data', (favorite: Nhentai.Favorite) => { const writable = createWriteStream(path.resolve(result.filePaths[0], favorite.name)); favorite.torrentFile.pipe(writable); }); favoritesStream.once('end', resolve); }); } @answer(IpcChannel.NHENTAI_GET_WORK) public async nhentaiGetWork({ galleryId }: { galleryId: string }): Promise { const work = await this.nhentaiSourceGetter.find(galleryId); return work; } public get(): NhentaiIpcController { const nhentaiApi: NhentaiApiInterface = container.get('nhentai-api'); const nhentaiSourceGetter: SourceGetterInterface = container.get('nhentai-source-getter'); const translator: I18nTranslatorInterface = container.get('i18n-translator'); const dialog: DialogInterface = container.get('dialog'); return new NhentaiIpcController(nhentaiApi, nhentaiSourceGetter, translator, dialog); } }