Skip to content

Commit

Permalink
WIP: Add Synology downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
vlacour97 committed Jan 30, 2023
1 parent eead5fc commit 460d251
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/src/downloader/downloader-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import File from "../file/file";
import {DownloadFile} from "../file/download-file";

export enum DownloaderType {
FILESYSTEM = 'filesystem'
FILESYSTEM = 'filesystem',
SYNOLOGY_DS = 'synology_ds',
}

export default interface DownloaderInterface {
Expand Down
54 changes: 54 additions & 0 deletions app/src/downloader/synology-ds-downloader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Downloader from "./decorator/downloader";
import DownloaderInterface, {DownloaderType} from "./downloader-interface";
import {ServiceParamType} from "../dependency-injection/param-provider";
import {DownloadEvent, DownloadFile} from "../file/download-file";
import SynologyDsClient from "./synology/synology-ds-client";
import EventEmitter from "events";
import File from "../file/file";

@Downloader(
DownloaderType.SYNOLOGY_DS,
[
{
id: SynologyDsClient.name,
type: ServiceParamType.INJECTABLE_SERVICE
}
]
)
export default class SynologyDsDownloader implements DownloaderInterface {
private readonly client: SynologyDsClient;

constructor(client: SynologyDsClient) {
this.client = client;
}

initialize(): void {
}

getDownloadFile(file: File): DownloadFile {
const eventEmitter = new EventEmitter();
return new DownloadFile(file, () => {
this.onDownload(eventEmitter, file);
}, eventEmitter);
}

private async onDownload(eventEmitter: EventEmitter, file: File) {
await this.client.launchDownload(file.link);
eventEmitter.emit(DownloadEvent.START_DOWNLOAD);

const interval = setInterval(async () => {
try {
if ('finished' === await this.client.getStatus(file.link)) {
eventEmitter.emit(DownloadEvent.DONE);
clearInterval(interval);
}
} catch (error) {
eventEmitter.emit(DownloadEvent.ERROR, error);
clearInterval(interval);
}
}, 3000);
}

close(): void {
}
}
89 changes: 89 additions & 0 deletions app/src/downloader/synology/synology-ds-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Service from "../../dependency-injection/decorator/service";
import {ServiceParamType} from "../../dependency-injection/param-provider";
import axios from "axios";

const urlencode = require('urlencode')

@Service(
SynologyDsClient.name,
[
{
id: 'SYNOLOGY_ENDPOINT',
type: ServiceParamType.ENVIRONMENT_VARIABLE
},
{
id: 'SYNOLOGY_USERNAME',
type: ServiceParamType.ENVIRONMENT_VARIABLE
},
{
id: 'SYNOLOGY_PASSWORD',
type: ServiceParamType.ENVIRONMENT_VARIABLE
},
]
)
export default class SynologyDsClient {
private readonly endpoint: string;
private readonly username: string;
private readonly password: string;

constructor(endpoint: string, username: string, password: string) {
this.endpoint = endpoint;
this.username = username;
this.password = password;
}

private getUrlParams (params) {
return '?' + Object
.keys(params)
.map(key => `${key}=${urlencode(params[key])}`)
.join('&')
}

private getUrl (uri: string, params: object = {}) {
const defaultParams = { username: this.username, password: this.password }
const host = this.endpoint;

return host + uri + this.getUrlParams({ ...defaultParams, ...params })
}

async launchDownload(fileUrl: string): Promise<void> {
await axios.post(
this.getUrl(
'/webapi/DownloadStation/task.cgi',
{
api: 'SYNO.DownloadStation.Task',
version: 1,
method: 'create',
uri: fileUrl
}
)
)
}

async getStatus(fileUrl: string): Promise<string> {
const response = await axios.post(
this.getUrl(
'/webapi/DownloadStation/task.cgi',
{
api: 'SYNO.DownloadStation.Task',
version: 1,
method: 'list',
additional: 'file'
}
)
)
console.log(response);

// @ts-ignore
for (let task of response.data.tasks) {

// @ts-ignore
if (fileUrl === task.additional.detail.uri) {
// @ts-ignore
return task.status;
}
}

return 'unknow'
}
}
4 changes: 3 additions & 1 deletion app/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AllDebridDebrider from "./debrider/all-debrid-debrider";
import FilesystemDownloader from "./downloader/filesystem-downloader";
import PushoverNotifier from "./notifier/pushover-notifier";
import StdoutNotifier from "./notifier/stdout-notifier";
import SynologyDsDownloader from "./downloader/synology-ds-downloader";

/**
* Declaration of services
Expand All @@ -16,7 +17,8 @@ const services = {
AllDebridDebrider
},
downloader: {
FilesystemDownloader
FilesystemDownloader,
SynologyDsDownloader,
},
notifier: {
PushoverNotifier,
Expand Down

0 comments on commit 460d251

Please sign in to comment.