From f4db19cc11f1bcdaf6482c8b212d83a01dfc6266 Mon Sep 17 00:00:00 2001 From: MBR#0001 Date: Sun, 23 Jul 2023 21:34:24 +0200 Subject: [PATCH 1/2] Add ability to upload hearing-impaired subs --- .../subtitleuploader/subtitleuploader.js | 28 +++++++++++++++++-- .../subtitleuploader.template.html | 4 +++ src/strings/en-us.json | 3 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/components/subtitleuploader/subtitleuploader.js b/src/components/subtitleuploader/subtitleuploader.js index 90e04b16ddb..5bfc27011fb 100644 --- a/src/components/subtitleuploader/subtitleuploader.js +++ b/src/components/subtitleuploader/subtitleuploader.js @@ -1,5 +1,7 @@ import escapeHtml from 'escape-html'; +import { getSubtitleApi } from '@jellyfin/sdk/lib/utils/api/subtitle-api'; +import { toApi } from 'utils/jellyfin-apiclient/compat'; import dialogHelper from '../../components/dialogHelper/dialogHelper'; import ServerConnections from '../ServerConnections'; import dom from '../../scripts/dom'; @@ -75,7 +77,20 @@ function setFiles(page, files) { reader.readAsDataURL(file); } -function onSubmit(e) { +function getStringFromFile(file) { + return new Promise(function (resolve, reject) { + const reader = new FileReader(); + reader.onload = (e) => { + // Split by a comma to remove the url: prefix + const data = e.target.result.split(',')[1]; + resolve(data); + }; + reader.onerror = reject; + reader.readAsDataURL(file); + }); +} + +async function onSubmit(e) { const file = currentFile; if (!isValidSubtitleFile(file)) { @@ -89,8 +104,17 @@ function onSubmit(e) { const dlg = dom.parentWithClass(this, 'dialog'); const language = dlg.querySelector('#selectLanguage').value; const isForced = dlg.querySelector('#chkIsForced').checked; + const isHearingImpaired = dlg.querySelector('#chkIsHearingImpaired').checked; + + const subtitleApi = getSubtitleApi(toApi(ServerConnections.getApiClient(currentServerId))); + + const data = await getStringFromFile(file); + const format = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase(); - ServerConnections.getApiClient(currentServerId).uploadItemSubtitle(currentItemId, language, isForced, file).then(function () { + subtitleApi.uploadSubtitle({ + itemId: currentItemId, + uploadSubtitleDto: { Data: data, Language: language, IsForced: isForced, Format: format, IsHearingImpaired: isHearingImpaired } + }).then(function () { dlg.querySelector('#uploadSubtitle').value = ''; loading.hide(); hasChanges = true; diff --git a/src/components/subtitleuploader/subtitleuploader.template.html b/src/components/subtitleuploader/subtitleuploader.template.html index ba43e004114..7febd1e7ac3 100644 --- a/src/components/subtitleuploader/subtitleuploader.template.html +++ b/src/components/subtitleuploader/subtitleuploader.template.html @@ -31,6 +31,10 @@

${HeaderAddUpdateSubtitle}

${LabelIsForced} +
diff --git a/src/strings/en-us.json b/src/strings/en-us.json index 10639d58efb..7ac3e3bd53b 100644 --- a/src/strings/en-us.json +++ b/src/strings/en-us.json @@ -1715,5 +1715,6 @@ "Unreleased": "Not yet released", "LabelTonemappingMode": "Tone mapping mode", "TonemappingModeHelp": "Select the tone mapping mode. If you experience blown out highlights try switching to the RGB mode.", - "Unknown": "Unknown" + "Unknown": "Unknown", + "LabelIsHearingImpaired": "For hearing impaired (SDH)" } From eccfd5316d4d7393ed523884f11f7c3a8b97af2c Mon Sep 17 00:00:00 2001 From: MBR#0001 Date: Wed, 27 Sep 2023 16:42:31 +0200 Subject: [PATCH 2/2] Move function to util --- .../subtitleuploader/subtitleuploader.js | 16 ++-------------- src/utils/file.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 src/utils/file.ts diff --git a/src/components/subtitleuploader/subtitleuploader.js b/src/components/subtitleuploader/subtitleuploader.js index 5bfc27011fb..6cd3f1ff254 100644 --- a/src/components/subtitleuploader/subtitleuploader.js +++ b/src/components/subtitleuploader/subtitleuploader.js @@ -15,6 +15,7 @@ import '../../elements/emby-button/emby-button'; import '../../elements/emby-select/emby-select'; import '../formdialog.scss'; import './style.scss'; +import { readFileAsBase64 } from 'utils/file'; let currentItemId; let currentServerId; @@ -77,19 +78,6 @@ function setFiles(page, files) { reader.readAsDataURL(file); } -function getStringFromFile(file) { - return new Promise(function (resolve, reject) { - const reader = new FileReader(); - reader.onload = (e) => { - // Split by a comma to remove the url: prefix - const data = e.target.result.split(',')[1]; - resolve(data); - }; - reader.onerror = reject; - reader.readAsDataURL(file); - }); -} - async function onSubmit(e) { const file = currentFile; @@ -108,7 +96,7 @@ async function onSubmit(e) { const subtitleApi = getSubtitleApi(toApi(ServerConnections.getApiClient(currentServerId))); - const data = await getStringFromFile(file); + const data = await readFileAsBase64(file); const format = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase(); subtitleApi.uploadSubtitle({ diff --git a/src/utils/file.ts b/src/utils/file.ts new file mode 100644 index 00000000000..b85178e2f20 --- /dev/null +++ b/src/utils/file.ts @@ -0,0 +1,15 @@ +/** + * Reads and returns the file encoded in base64 + */ +export function readFileAsBase64(file: File): Promise { + return new Promise(function (resolve, reject) { + const reader = new FileReader(); + reader.onload = (e) => { + // Split by a comma to remove the url: prefix + const data = (e.target?.result as string)?.split?.(',')[1]; + resolve(data); + }; + reader.onerror = reject; + reader.readAsDataURL(file); + }); +}