Skip to content

Commit

Permalink
Merge pull request #45 from redSpoutnik/api-upload-subtitle
Browse files Browse the repository at this point in the history
Add Post Subtitle to API
  • Loading branch information
dkanada authored Sep 12, 2020
2 parents 16cb430 + d3b18b8 commit 69fdf63
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,65 @@ class ApiClient {
});
}

uploadItemSubtitle(itemId, language, isForced, file) {
if (!itemId) {
throw new SyntaxError('Missing itemId');
}

if (!language) {
throw new SyntaxError('Missing language');
}

if (typeof isForced !== 'boolean') {
throw new TypeError('Parameter isForced must be a boolean.');
}

if (!file) {
throw new SyntaxError('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`);

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];

this.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
*/
Expand Down

0 comments on commit 69fdf63

Please sign in to comment.