From 7c4e6ba0694f12ff0562b1992370668f63b6edaf Mon Sep 17 00:00:00 2001 From: redSpoutnik <15638041+redSpoutnik@users.noreply.github.com> Date: Fri, 8 May 2020 22:40:22 +0200 Subject: [PATCH 1/3] Add Post Subtitle to API --- src/apiClient.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/apiClient.js b/src/apiClient.js index bec67c85..2b131d0b 100644 --- a/src/apiClient.js +++ b/src/apiClient.js @@ -2031,6 +2031,68 @@ class ApiClient { }); } + uploadItemSubtitle(itemId, language, isForced, file) { + if (!itemId) { + throw new Error('null itemId'); + } + + if (!language) { + throw new Error('null language'); + } + + if (!(typeof isForced === 'boolean')) { + throw new Error('Invalid isForced value.'); + } + + if (!file) { + throw new Error('File must be a subtitle file.'); + } + + const format = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase(); + + if (!['sub', 'srt', 'vtt', 'ass', 'ssa'].includes(format)) { + throw new Error('Invalid subtitle format.'); + } + + let url = this.getUrl(`Videos/${itemId}/Subtitles`); + + const instance = this; + + return new Promise((resolve, reject) => { + const reader = new FileReader(); + + reader.onerror = () => { + reject(); + }; + + reader.onabort = () => { + reject(); + }; + + // Closure to capture the file information. + reader.onload = (e) => { + // Split by a comma to remove the url: prefix + const data = e.target.result.split(',')[1]; + + instance + .ajax({ + type: 'POST', + url, + data: { + language: language, + format: format, + isForced: isForced, + data: data + } + }) + .then(resolve, reject); + }; + + // Read in the image file as a data URL. + reader.readAsDataURL(file); + }); + } + /** * Gets the list of installed plugins on the server */ From 73fbde0a6a268033c62bdb41df60000921a73571 Mon Sep 17 00:00:00 2001 From: redSpoutnik <15638041+redSpoutnik@users.noreply.github.com> Date: Sun, 10 May 2020 18:23:18 +0200 Subject: [PATCH 2/3] Clean code smell --- src/apiClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apiClient.js b/src/apiClient.js index 2b131d0b..0d7ba23d 100644 --- a/src/apiClient.js +++ b/src/apiClient.js @@ -2040,7 +2040,7 @@ class ApiClient { throw new Error('null language'); } - if (!(typeof isForced === 'boolean')) { + if (typeof isForced !== 'boolean') { throw new Error('Invalid isForced value.'); } From d3b18b882584b9e6fc6fea775dcc58b7b91ea5bb Mon Sep 17 00:00:00 2001 From: redSpoutnik <15638041+redSpoutnik@users.noreply.github.com> Date: Mon, 1 Jun 2020 15:59:40 +0200 Subject: [PATCH 3/3] uploadItemSubtitle: update error messages, remove unnecessary 'this' reference --- src/apiClient.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/apiClient.js b/src/apiClient.js index 0d7ba23d..5fb00bbb 100644 --- a/src/apiClient.js +++ b/src/apiClient.js @@ -2033,19 +2033,19 @@ class ApiClient { uploadItemSubtitle(itemId, language, isForced, file) { if (!itemId) { - throw new Error('null itemId'); + throw new SyntaxError('Missing itemId'); } if (!language) { - throw new Error('null language'); + throw new SyntaxError('Missing language'); } if (typeof isForced !== 'boolean') { - throw new Error('Invalid isForced value.'); + throw new TypeError('Parameter isForced must be a boolean.'); } if (!file) { - throw new Error('File must be a subtitle file.'); + throw new SyntaxError('File must be a subtitle file.'); } const format = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase(); @@ -2056,8 +2056,6 @@ class ApiClient { let url = this.getUrl(`Videos/${itemId}/Subtitles`); - const instance = this; - return new Promise((resolve, reject) => { const reader = new FileReader(); @@ -2074,8 +2072,7 @@ class ApiClient { // Split by a comma to remove the url: prefix const data = e.target.result.split(',')[1]; - instance - .ajax({ + this.ajax({ type: 'POST', url, data: {