From 25795ddabfa6bfe5e06f63babf55dd311ed86595 Mon Sep 17 00:00:00 2001 From: MadKarma <100418457+madkarmaa@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:17:06 +0100 Subject: [PATCH] feat: added `isVideoSuitableForUpload` method --- package.json | 1 + src/index.js | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 9286eaf..26da818 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "mime-types": "^2.1.35" }, "devDependencies": { + "jsdoc": "^4.0.2", "jsdoc-to-markdown": "^7.1.0" } } diff --git a/src/index.js b/src/index.js index adeadb0..634cfdd 100644 --- a/src/index.js +++ b/src/index.js @@ -25,6 +25,8 @@ axios.interceptors.response.use( * Client to interact with the `streamable.com` API * * `.login()` or `.createAccount()` methods **must** be called before performing any other operations! + * + * @class */ class StreamableClient { #loggedIn = false; @@ -71,6 +73,7 @@ class StreamableClient { /** * Check if the user is logged in + * * @returns {Promise} */ async isLoggedIn() { @@ -79,6 +82,7 @@ class StreamableClient { /** * Get the currently logged in user's data + * * @returns {Promise} The user's data */ async getUserData() { @@ -88,6 +92,7 @@ class StreamableClient { /** * Get the current user's plan's data + * * @returns {Promise} The user's current plan's data */ async getPlanData() { @@ -97,6 +102,7 @@ class StreamableClient { /** * Get the current user's videos data + * * @returns {Promise} The current user's videos data */ async getAllVideosData() { @@ -114,12 +120,9 @@ class StreamableClient { return (await axios.get(endpoints.VIDEO(shortcode), { headers: this.#headers })).data; } - async #willReachUploadLimits(video_size) { - if (!(await this.isLoggedIn())) return console.error('You must be logged in to use this method!'); - } - /** * Upload a video from a given url + * * @param {URL | String} url The url of the video file to upload * @returns {Promise} The uploaded video's data */ @@ -172,6 +175,7 @@ class StreamableClient { /** * Delete a video from the user's account + * * @param {String} shortcode The shortcode of the video * @returns {Promise} */ @@ -185,6 +189,7 @@ class StreamableClient { /** * Delete all videos from the user's account + * * @returns {Promise} */ async deleteAllVideos() { @@ -234,13 +239,15 @@ class StreamableClient { /** * Upload a local video + * * @param {String} videoPath The path to the video file * @returns {Promise} The uploaded video's data */ async uploadLocalVideo(videoPath) { videoPath = path.resolve(videoPath); + const isVideoValid = await this.isVideoSuitableForUpload(videoPath); - if (!lookup(videoPath).startsWith('video')) return console.error('Please provide a video file!'); + if (!isVideoValid.isValid) return console.error(isVideoValid.reason); const { size: videoSize } = fs.statSync(videoPath); @@ -311,6 +318,28 @@ class StreamableClient { ) ).data; } + + /** + * Check if a local file is suitable for upload based off the current plan's limitations + * + * @param {String} videoPath The path to the video file + * @return {Promise<{ reason: String, isValid: boolean }>} + */ + async isVideoSuitableForUpload(videoPath) { + videoPath = path.resolve(videoPath); + + if (!lookup(videoPath).startsWith('video')) return { reason: 'Not a video', isValid: false }; + + const { size: videoSize } = fs.statSync(videoPath); + const { plan_max_length, plan_max_size } = await this.getUserData(); + + if (videoSize > convert(plan_max_size).from('Gb').to('b')) return { reason: 'Video too large', isValid: false }; + + if ((await getVideoDurationInSeconds(videoPath)) > plan_max_length) + return { reason: 'Video too long', isValid: false }; + + return { reason: '', isValid: true }; + } } module.exports = StreamableClient;