From b6fd750b7d324a16ad8904cdd5a30615e1e0ced0 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Tue, 25 Jan 2022 20:58:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=9D=E8=AF=95=E6=94=AF=E6=8C=81DSM7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../clients/synologyDownloadStation/init.js | 104 +++++++++--------- 1 file changed, 54 insertions(+), 50 deletions(-) diff --git a/resource/clients/synologyDownloadStation/init.js b/resource/clients/synologyDownloadStation/init.js index dfd84a296..c42ac1eef 100644 --- a/resource/clients/synologyDownloadStation/init.js +++ b/resource/clients/synologyDownloadStation/init.js @@ -1,5 +1,6 @@ /** * @see https://global.download.synology.com/download/Document/DeveloperGuide/Synology_Download_Station_Web_API.pdf + * @backport https://github.com/ronggang/PT-Plugin-Plus/blob/48c2d42a1d05c129c0abbbecf653b1b7d88a8a8e/src/resource/btClients/src/clients/synologyDownloadStation.ts */ (function ($, window) { class Client { @@ -7,7 +8,6 @@ init(options) { this.options = options; this.sessionId = ""; - this.version = 2; if (this.options.address.substr(-1) == "/") { this.options.address = this.options.address.substr(0, this.options.address.length - 1); } @@ -18,7 +18,7 @@ */ getSessionId() { return new Promise((resolve, reject) => { - let url = `${this.options.address}/webapi/auth.cgi?api=SYNO.API.Auth&version=${this.version}&method=login&account=${encodeURIComponent(this.options.loginName)}&passwd=${encodeURIComponent(this.options.loginPwd)}&session=DownloadStation&format=sid`; + let url = `${this.options.address}/webapi/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=${encodeURIComponent(this.options.loginName)}&passwd=${encodeURIComponent(this.options.loginPwd)}&session=DownloadStation&format=sid`; $.ajax({ url, timeout: PTBackgroundService.options.connectClientTimeout, @@ -83,8 +83,8 @@ /** * 添加种子链接 - * @param {*} options - * @param {*} callback + * @param {*} options + * @param {*} callback */ addTorrentFromUrl(options, callback) { if (!this.sessionId) { @@ -102,64 +102,68 @@ }) return; } - // let path = [`${this.options.address}/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task`, - // `version=${this.version}`, - // `method=create`, - // `_sid=${this.sessionId}`, - // `uri=` + encodeURIComponent(options.url), - // `destination=` + encodeURIComponent(options.savePath) - // ]; - // $.ajax({ - // url: path.join("&"), - // timeout: PTBackgroundService.options.connectClientTimeout, - // dataType: "json" - // }).done((result) => { - // console.log(result) - // callback(result) - // }).fail(() => { - // callback({ - // status: "error", - // msg: "服务器连接失败" - // }) - // }) - - PTBackgroundService.requestMessage({ - action: "getTorrentDataFromURL", - data: options.url - }) - .then((result) => { - let formData = new FormData(); - formData.append("_sid", this.sessionId); - formData.append("api", "SYNO.DownloadStation.Task"); - formData.append("version", this.version); - formData.append("method", "create"); - - if (options.savePath) { - let savePath = options.savePath + ""; - // 去除路径最后的 / ,以确保可以正常添加目录信息 - if (savePath.substr(-1) == "/") { - savePath = savePath.substr(0, savePath.length - 1); + + let postData = { + _sid: this.sessionId, + api: 'SYNO.DownloadStation2.Task', + method: 'create', + version: 2, + create_list: false + } + + if (options.savePath) { + let savePath = options.savePath + ""; + // 去除路径最后的 / ,以确保可以正常添加目录信息 + if (savePath.substr(-1) == "/") { + savePath = savePath.substr(0, savePath.length - 1); + } + postData.destination = `"${savePath || ''}"`; + } + + if (options.url.startWith('magnet:')) { + postData.type = '"url"'; + postData.url = [options.url]; + + this.addTorrent(postData, options, callback); + } else { + postData.type = '"file"'; + postData.file = ['torrent']; + + let formData = new FormData(); + Object.keys(postData).forEach((k) => { + let v = postData[k]; + if (v !== undefined) { + if (Array.isArray(v)) { + v = JSON.stringify(v); } - formData.append("destination", savePath) + formData.append(k,v); } + }); - formData.append("file", result, "file.torrent") - this.addTorrent(formData, options, callback); + PTBackgroundService.requestMessage({ + action: "getTorrentDataFromURL", + data: options.url }) - .catch((result) => { - callback && callback(result); - }); + .then((result) => { + formData.append("file", result, "file.torrent") + + this.addTorrent(formData, options, callback); + }) + .catch((result) => { + callback && callback(result); + }); + + } } addTorrent(formData, options, callback) { $.ajax({ - url: `${this.options.address}/webapi/DownloadStation/task.cgi`, + url: `${this.options.address}/webapi/entry.cgi`, timeout: PTBackgroundService.options.connectClientTimeout, type: "POST", processData: false, contentType: false, - method: "POST", data: formData, dataType: "json" }).done((result) => { @@ -202,4 +206,4 @@ } // 添加到 window 对象,用于客户页面调用 window.synologyDownloadStation = Client; -})(jQuery, window) \ No newline at end of file +})(jQuery, window)