2020-11-09 18:15:30 +01:00
|
|
|
import path from 'path';
|
|
|
|
import { createWriteStream } from 'fs-extra';
|
2020-07-25 02:02:37 +02:00
|
|
|
import { container } from '../../core/container';
|
2021-01-04 22:11:14 +01:00
|
|
|
import { IDialog } from '../dialog/i-dialog';
|
2020-11-09 18:15:30 +01:00
|
|
|
import { II18nTranslator } from '../i18n/i-i18n-translator';
|
2020-07-25 02:02:37 +02:00
|
|
|
import { answer } from '../ipc/annotations/answer';
|
|
|
|
import { INhentaiApi } from './i-nhentai-api';
|
2020-11-09 18:15:30 +01:00
|
|
|
import { IFavorite } from './nhentai';
|
2020-07-25 02:02:37 +02:00
|
|
|
|
|
|
|
export class NhentaiIpcController implements IIpcController {
|
2020-11-09 18:15:30 +01:00
|
|
|
private readonly nhentaiApi: INhentaiApi;
|
2020-07-25 02:02:37 +02:00
|
|
|
|
2020-11-09 18:15:30 +01:00
|
|
|
private readonly translator: II18nTranslator;
|
|
|
|
|
2021-01-04 22:11:14 +01:00
|
|
|
private readonly dialog: IDialog;
|
|
|
|
|
|
|
|
private constructor(nhentaiApi: INhentaiApi, translator: II18nTranslator, dialog: IDialog) {
|
2020-07-25 02:02:37 +02:00
|
|
|
this.nhentaiApi = nhentaiApi;
|
2020-11-09 18:15:30 +01:00
|
|
|
this.translator = translator;
|
2021-01-04 22:11:14 +01:00
|
|
|
this.dialog = dialog;
|
2020-07-25 02:02:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:15:30 +01:00
|
|
|
@answer(IpcChannel.NHENTAI_SAVE_FAVORITES)
|
|
|
|
public async nhentaiSaveFavorites(): Promise<void> {
|
2021-01-04 22:11:14 +01:00
|
|
|
const result = await this.dialog.selectFolder({
|
2020-11-09 18:15:30 +01:00
|
|
|
title: this.translator.t('Select torrent file save location'),
|
|
|
|
});
|
|
|
|
|
2021-01-04 22:12:37 +01:00
|
|
|
if (result.canceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:15:30 +01:00
|
|
|
const favoritesStream = await this.nhentaiApi.getFavorites();
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
favoritesStream.on('data', (favorite: IFavorite) => {
|
|
|
|
const writable = createWriteStream(path.resolve(result.filePaths[0], favorite.name));
|
|
|
|
favorite.torrentFile.pipe(writable);
|
|
|
|
});
|
2020-07-25 02:02:37 +02:00
|
|
|
|
2021-01-04 23:40:15 +01:00
|
|
|
favoritesStream.once('end', resolve);
|
2020-11-09 18:15:30 +01:00
|
|
|
});
|
2020-07-25 02:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get(): NhentaiIpcController {
|
2020-08-08 18:23:29 +02:00
|
|
|
const nhentaiApi: INhentaiApi = container.get('nhentai-api');
|
2020-11-09 18:15:30 +01:00
|
|
|
const translator: II18nTranslator = container.get('i18n-translator');
|
2021-01-04 22:11:14 +01:00
|
|
|
const dialog: IDialog = container.get('dialog');
|
|
|
|
return new NhentaiIpcController(nhentaiApi, translator, dialog);
|
2020-07-25 02:02:37 +02:00
|
|
|
}
|
|
|
|
}
|