-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters