49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
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';
|
|
|
|
export class NhentaiIpcController implements IpcController {
|
|
private readonly nhentaiApi: NhentaiApiInterface;
|
|
|
|
private readonly translator: I18nTranslatorInterface;
|
|
|
|
private readonly dialog: DialogInterface;
|
|
|
|
private constructor(nhentaiApi: NhentaiApiInterface, translator: I18nTranslatorInterface, dialog: DialogInterface) {
|
|
this.nhentaiApi = nhentaiApi;
|
|
this.translator = translator;
|
|
this.dialog = dialog;
|
|
}
|
|
|
|
@answer(IpcChannel.NHENTAI_SAVE_FAVORITES)
|
|
public async nhentaiSaveFavorites(): Promise<void> {
|
|
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);
|
|
});
|
|
}
|
|
|
|
public get(): NhentaiIpcController {
|
|
const nhentaiApi: NhentaiApiInterface = container.get('nhentai-api');
|
|
const translator: I18nTranslatorInterface = container.get('i18n-translator');
|
|
const dialog: DialogInterface = container.get('dialog');
|
|
return new NhentaiIpcController(nhentaiApi, translator, dialog);
|
|
}
|
|
}
|